Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC to connect to MYSQL

 
And Green
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Tarek Khojah
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check your DBMS documentation, some have default user-names and/or passwords, if not, call DriverManager.getConnection(URL, "", "");
 
And Green
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats fantastic mate, worked first time. I'd spent five hours on the MySQL forum last night trying to figure this one out. Priceless.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic