Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-03-2008, 12:01 AM
Member
 
Join Date: Jan 2008
Posts: 7
prfalco is on a distinguished road
can't register a MySQL driver
Hello:
I have this code to connect to a MySQL database:

package paquetedb;

import java.sql.*;

public class PruebaDB {

public static void main(String[] args) {
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance ();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","xyz001");
if (!con.isClosed()){
System.out.println("conexión exitosa a MySQL");
}
}catch(Exception e){
System.err.println("Excepción: " + e.getMessage());
}finally{
try{
if (con != null)
con.close();
} catch(SQLException e){}
}
}

}

When it gets to "Class.forName("com.mysql.jdbc.Driver").newInstanc e();"
it always gets to the catch(exception e) indicating that there's something wrong when trying to load the driver.
I'm using NetBeans 6.0 and in the services tab, I connect to the database without any problem, indicating that my database server is running ok.
I've been looking around in the net and seems everyone is writing the some code to do this task.
What could be the problem?
Thank you.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-03-2008, 12:36 AM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
You need to change your line of code

Code:
Class.forName("com.mysql.jdbc.Driver").newInstance ();
to

Code:
Class.forName("com.mysql.jdbc.Driver");
Your original code is trying to instantiate an instance of the driver and demands that it has a no argument constructor - which I do not believe it does have - but whether it has or not is beside the point as all you need to do is to cause the class to be loaded ( the new line of code does that) JDBC drivers have static blocks of code that register themselves with the DriverManager when they are loaded into the JVM, this means that the getConnection call will find them. Sorry went on a bit there

If it still fails then it means the JDBC driver is not available on the classpath
__________________
-- Hope that helps
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-03-2008, 04:42 AM
Member
 
Join Date: Jan 2008
Posts: 7
prfalco is on a distinguished road
I did the change and still fails so the JDBC driver is not available on the classpath, how do I do that? I mean, what should I add to my classpath?
Thank you.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-03-2008, 07:28 AM
Member
 
Join Date: Jan 2008
Posts: 20
JAdmin is on a distinguished road
You need to have MySQL related files in your classpath. Download mysql-connector-java-3.1.14-bin.jar and add to the classpath.

Hope this helps!
__________________
Sincerely, Your friends at
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-04-2008, 12:13 AM
Member
 
Join Date: Jan 2008
Posts: 7
prfalco is on a distinguished road
It worked!
Hello:
I Downloaded mysql-connector-java-3.1.14-bin.jar (from www.mysql.com/downloads/api-jdbc-dev.hatml) and added to the classpath (I work with NetBeans 6.0, so I did the last thing right clicking on my project, then selected properties, then in the Properties Dialog, selected the run tab and finally added the .jar file. So it works.
Thank you very much !
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql driver problem mokingsu Database 4 01-17-2008 06:27 PM
PostgreSQL driver bugger Database 3 01-11-2008 10:45 AM
Help with JDBC driver Daniel Database 2 07-03-2007 09:16 PM
Oracle Driver softy Database 1 07-02-2007 01:20 AM
ClassNotFoundException com.mysql.jdbc.Driver Heather Database 2 06-30-2007 03:42 PM


All times are GMT +3. The time now is 10:14 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org