Results 1 to 5 of 5
  1. #1
    prfalco is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    You need to change your line of code

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

    Java 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

  3. #3
    prfalco is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    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.

  4. #4
    JAdmin is offline Member
    Join Date
    Jan 2008
    Posts
    20
    Rep Power
    0

    Default

    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 www.javaadvice.com

  5. #5
    prfalco is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default 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 !

Similar Threads

  1. Replies: 4
    Last Post: 03-31-2010, 01:08 PM
  2. mysql driver problem
    By mokingsu in forum JDBC
    Replies: 4
    Last Post: 01-17-2008, 06:27 PM
  3. PostgreSQL driver
    By bugger in forum JDBC
    Replies: 3
    Last Post: 01-11-2008, 10:45 AM
  4. Help with JDBC driver
    By Daniel in forum JDBC
    Replies: 2
    Last Post: 07-03-2007, 09:16 PM
  5. Oracle Driver
    By softy in forum JDBC
    Replies: 1
    Last Post: 07-02-2007, 01:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •