Forums Register Login

JDBC to connect to MYSQL

+Pie Number of slices to send: Send
I have successfully installed My SQL on my machine and have been given the following code to load the driver's which works to a certain extent:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class LoadDriver
{
public static void main (String [] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Driver Loaded");
}
catch (Exception ex)
{
System.out.println("Failure");
}
try
{
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost/menagerie");
System.out.println("Connection = " + conn);
}

catch (SQLException ex)
{
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
}

The java is compiled in a file on my desktop but when I run it, whilst running MySQL, its output is:
Driver Loaded
SQLException: Access denied for user ''@'localhost(using password:no)
SQLState:28000
VendorError: 1045.

I can get into SQL ok by using a password but JDBC is another matter.I realise this is more of a MYsql issue but the MySQL forum is not at all helpful on tis issue despite it being an apparently frequent problem.

Any hints appreciated, apologies if it's something obvious I've missed!
+Pie Number of slices to send: Send
Check your DBMS documentation, some have default user-names and/or passwords, if not, call DriverManager.getConnection(URL, "", "");
+Pie Number of slices to send: Send
Thats fantastic mate, worked first time. I'd spent five hours on the MySQL forum last night trying to figure this one out. Priceless.
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1630 times.
Similar Threads
sql exception thrown
Retrieving image from database - getBlob?
unable to establish a connection betwen JDBC and MySQL server using a java class.
another JDBC driver question
do i need odbc to run java applications - reg connection
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:14:05.