aspose file tools*
The moose likes JDBC and the fly likes  problem with MySQL driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark " problem with MySQL driver " Watch " problem with MySQL driver " New topic
Author

problem with MySQL driver

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
I just installed mySql and apprently when trying to test it with a standalone application I get an error.

the problem is here:
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
String url = "jdbc:mysql://localhost/test";

What is missing? I tried somthing like:
String url = "jdbc:mysql://localhost:3306/test";

and other...but nothing works

username/password is fine. I can build tables on test with MySQLCC

any idea?

my code is this:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Test3
{

public static void main(String[] args)
{
Connection conn = null;
//open connection to db
try{

String userName = "root";
String password = "letmein";

Class.forName ("com.mysql.jdbc.Driver").newInstance ();
String url = "jdbc:mysql://localhost/test";
conn = DriverManager.getConnection (url, userName, password);

System.out.println("Database connection established");

}catch(SQLException sqlex)
{
System.out.println("SQL EXCEPTION");
} catch(Exception ex)
{
System.out.println("GLOBAL EXCEPTION");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}//main
}
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
It would help, if you provided the error message(s).
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Oops:

System.out.println(ex.getMessage());

and the err is:

com.mysql.jdbc.Driver
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
It sounds like you need to add the MySQL driver to your classpath when you compile your program(Test3).
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
ok, how do i do that?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

Peter,
You actually need the driver when you run the program, not when you compile it. It compiles without any error messages, right? Then at runtime, the program realizes it can't find the driver.

Windows:
java -classpath ".;c:\\path\of\driver" *.java

UNIX:
java -classpath "."/path/of/driver" *.java

Note that if you are running from within an IDE, the instructions are a bit different. If this is the case, let us know what IDE you are using.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
thank you Jeanne,
My IDE is Eclipse
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Jeanne, is correct. You should only need to add the MySQL driver when you try to run your application. My mistake.

I am completely new to Eclipse, so bare with me.

I believe on the main tool bar, there is a "Run" option. From there if you choose "Run..." and then choose the run as "Java Application" configuration, then click the "New" button, At that point your program name(Test3) should appear and also it should bring up a tabbed menu. At that point you should see a "CLASSPATH" tabbed menu option. From there you should be able to browse your filesystem and add the MySQL driver to your Application.

Like I said I am new to Eclipse there may be alternative ways of doing this.

I hope this helps or you may try out the IDEs and other Tools Forum

Thanks
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

Peter,
Since this is an IDE's question, I'm moving it to that forum.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem with MySQL driver