Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with MySQL driver

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help, if you provided the error message(s).
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops:

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

and the err is:

com.mysql.jdbc.Driver
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you need to add the MySQL driver to your classpath when you compile your program(Test3).
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, how do i do that?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Jeanne,
My IDE is Eclipse
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
Since this is an IDE's question, I'm moving it to that forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic