File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes JDBC to connect to MYSQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC to connect to MYSQL" Watch "JDBC to connect to MYSQL" New topic
Author

JDBC to connect to MYSQL

And Green
Greenhorn

Joined: Sep 28, 2007
Posts: 26
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

Joined: Jul 21, 2007
Posts: 58
Check your DBMS documentation, some have default user-names and/or passwords, if not, call DriverManager.getConnection(URL, "", "");
And Green
Greenhorn

Joined: Sep 28, 2007
Posts: 26
Thats fantastic mate, worked first time. I'd spent five hours on the MySQL forum last night trying to figure this one out. Priceless.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC to connect to MYSQL