Forums Register Login

problem with MySQL driver

+Pie Number of slices to send: Send
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
}
+Pie Number of slices to send: Send
It would help, if you provided the error message(s).
+Pie Number of slices to send: Send
Oops:

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

and the err is:

com.mysql.jdbc.Driver
+Pie Number of slices to send: Send
It sounds like you need to add the MySQL driver to your classpath when you compile your program(Test3).
+Pie Number of slices to send: Send
ok, how do i do that?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
thank you Jeanne,
My IDE is Eclipse
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Peter,
Since this is an IDE's question, I'm moving it to that forum.
crispy bacon. crispy 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 1229 times.
Similar Threads
developing jsp page using cewolf
connecting to mysql, with java, on a mac
MySql connection issue
Help - - - ClassNotFoundException: com.mysql.jdbc.Driver
problem with MySQL driver
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:29:09.