• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

problem with MySQL driver

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, how do i do that?
 
author & internet detective
Posts: 40685
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Jeanne,
My IDE is Eclipse
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 40685
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,
Since this is an IDE's question, I'm moving it to that forum.
 
Jeanne Boyarsky
author & internet detective
Posts: 40685
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,
Craig's approach works, but I find the following easier:

Right click the project containing your class and choose properties. Edit the java build path to include the driver.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all.
I will try this and let you know.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is quite simple, you have to download the jconnector from MySQL web site.
After unzipping the file, you have to add in your classpath jconnector.jar
or add it in Eclipse classpath option.
enjoy yourself !!!
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
excellent, it works
 
reply
    Bookmark Topic Watch Topic
  • New Topic