2

This is my first post so if I get anything wrong I apologise!

I am using Java in Eclipse to create tables in a MySQL Database. Everything has been working fine until it inexplicably stopped working the other day. My code runs but nothing happens to the database due to an error when I try to initialise the connection. The code actually runs to the end, the error is only viewable when I step through the code.

This is the constructor of my TableCreator class:

public TableCreator() throws SQLException {
        host = "jdbc:mysql://localhost:3306/test";
        dbName = "root";
        dbPass = "pass";
        conn = DriverManager.getConnection(host, dbName, dbPass);
        query = conn.createStatement();
        initialise();
        System.out.println("Success.");
    }

The error occurs on the line 'conn = DriverManager.getConnection(host, dbName, dbPass);'.

The stack looks like this just after I have attempted to execute this line:

TableCreator (1) [Java Application] 
    biz.cogitare.gpsperformancetool.TableCreator at localhost:53389 
        Thread [main] (Suspended)   
            Driver(NonRegisteringDriver).connect(String, Properties) line: 306  
            DriverManager.getConnection(String, Properties, Class<?>) line: not available   
            DriverManager.getConnection(String, String, String) line: not available 
            TableCreator.<init>() line: 23  
            TableCreator.main(String[]) line: 49    
        Daemon Thread [Abandoned connection cleanup thread] (Running)   
    C:\Program Files\Java\jre1.8.0_45\bin\javaw.exe (9 Jul 2015 12:27:27)

I have spent many hours searching the internet for help on how to fix this but so far I have been unsuccessful.

Any help would be greatly appreciated.

Thanks!

Anonymous Coward
  • 3,140
  • 22
  • 39
rob9021
  • 23
  • 1
  • 5
  • If the code runs until the end, there's no error. An exception would be thrown and propagated out of the method. Are you swallowing a stacktrace where you're calling `new TableCreator();`? – Kayaman Jul 09 '15 at 11:43
  • You talk about an error in your question, but you don't show any exception or stacktrace of the error. Please include it in your question. – Mark Rotteveel Jul 09 '15 at 12:33
  • Apologies if I was unclear, I was perhaps using wrong terminology. There is no error. The code runs to the end and "Success." is written on the Console. The problem is that 'conn = DriverManager.getConnection(host, dbName, dbPass);' hasn't executed properly and so the actions I perform to the database in my 'initialise()' method do not happen. – rob9021 Jul 09 '15 at 13:01
  • 'Driver(NonRegisteringDriver).connect(String, Properties) line: 306' appears when I attempt to execute the getConnection statement. – rob9021 Jul 09 '15 at 13:02

2 Answers2

1

Several things can cause such behaviour.

Check the driver is loaded.

If your JDBC driver is earlier than 4.0 you need to ensure the driver is loaded with

Class.forName("com.mysql.jdbc.Driver")

Or a similar string depending on the particular driver you are using.

Maybe you were doing another operation which loaded the driver before executing this code and that is why it did work. And maybe you are not doing that operation and that is why this code fails despite being the same code.

Check the driver file is in the classpath.

If you get ClassNotFoundException it means the JVM has not been able to locate your class. If you get LinkageError it means there is a version conflict. Get the most recent driver and reinstall it.

Check the driver is properly imported in eclipse.

Follow this instructions to import it : How to import a jar in Eclipse

If everything fails, reinstall.

Uninstall java, remove eclipse, re-install everything.

Insert the driver in the correct fashion and started a new project and copy your classes over.

Community
  • 1
  • 1
Anonymous Coward
  • 3,140
  • 22
  • 39
  • Thank you for your response but unfortunately this solution does not fix my problem. My JDBC driver is more recent than 4.0. If I add this line of code to my code the 'Driver(NonRegisteringDriver).connect(String, Properties)' problem simply happens early when I try to execute this new line. – rob9021 Jul 09 '15 at 14:19
  • That is progress. It means that the problem is not in the server or aptempt to stablish connection neither in your code. It means it cannot load the driver. This is could be caused by the file mysql-connector-java-?.?.?-bin.jar being missing. – Anonymous Coward Jul 09 '15 at 15:45
  • Hi Jose, The mysql-connector-java is not missing, I have version 5.1.35 in the class path. I am currently attempting to update it to the latest version, however, to see if this fixes the problem. I shall let you know how I get on. – rob9021 Jul 10 '15 at 09:30
  • It did not work unfortunately. I removed all connectors and imported the latest connector 5.1.36. Still getting the same problem however. Interestingly, I ran the code without a connector imported, and exactly the same thing happened - the code ran successfully but nothing happened to the database due to a non registering driver... – rob9021 Jul 10 '15 at 10:04
  • If Class.forName is failing then it throws one out of 3 possible exceptions : LinkageError , ExceptionInInitializerError or ClassNotFoundException. Which one are you getting? try { Class.forName("com.mysql.jdbc.Driver"); } catch ( Exception ex) { ex.printStackTrace(); } Also, may you post a link to the precise driver you are using? – Anonymous Coward Jul 10 '15 at 11:24
  • Class.forName is not failing in that sense. It doesn't throw any exceptions and the code seemingly executes successfully to the end, I realise that it hasn't worked however when I see no change to my DB. When I step through the code the line 'Driver(NonRegisteringDriver).connect(String, Properties)' is shown in the debug view as I try to connect to the database - this is the problem, it is not an exception being thrown on execution. – rob9021 Jul 10 '15 at 14:02
  • The driver I am using is 'mysql-connector-java-5.1.35-bin.jar', which has been downloaded from http://dev.mysql.com/downloads/connector/j/ and I have imported it by copying it directly into my Java\jre1.8.0_45\lib\ext folder. Could this be the problem? I read somewhere that adding it to the classpath in this way can cause it to break when a java update is performed, as the update installs a new virtual machine. – rob9021 Jul 10 '15 at 14:02
  • Indeed, you should not mess with the JDK installation. Put the jar inside the folder for your project and use eclipse to import the jar into the project as in : http://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse – Anonymous Coward Jul 10 '15 at 15:07
  • It's fixed! I uninstalled java, removed eclipse, re-installed everything, inserted the driver in the correct fashion and started a new project and copied my classes over and now it works! Thanks very much for your help, much appreciated. – rob9021 Jul 13 '15 at 09:24
  • Glad to be of help. I've edited my answer to include the steps we followed including the one which finally worked so that others who may face the same situation in the future can use it to solve it. Please accept it if you think it is the right answer to your question. – Anonymous Coward Jul 13 '15 at 10:36
0

If you are exporting the project as runnable jar,please make sure that you export mysql driver.jar
and as all above answers if you need to add
Class.forName("com.mysql.jdbc.Driver");
before
conn = DriverManager.getConnection(host, dbName, dbPass);

If it not working, please share with us the stacktrace

Ali Helmy
  • 784
  • 6
  • 18
  • Hi Ali, thanks for your response. It doesn't throw any exceptions and the code seemingly executes successfully to the end, therefore no stack trace, I realise that it hasn't worked however when I see no change to my DB. When I step through the code the line 'Driver(NonRegisteringDriver).connect(String, Properties)' is shown in the debug view as I try to connect to the database - this is the problem, it is not an exception being thrown on execution. – rob9021 Jul 10 '15 at 14:18
  • The driver I am using is 'mysql-connector-java-5.1.35-bin.jar', which has been downloaded from dev.mysql.com/downloads/connector/j and I have imported it by copying it directly into my Java\jre1.8.0_45\lib\ext folder. Could this be the problem? I read somewhere that adding it to the classpath in this way can cause it to break when a java update is performed, as the update installs a new virtual machine. – rob9021 Jul 10 '15 at 14:18
  • As i can see that you are using eclipse , why not export the project as runnable and select **Package required libraries into generated JAR** option instead of copying the driver into JAVA classpath – Ali Helmy Jul 10 '15 at 15:17
  • It's fixed! I uninstalled java, removed eclipse, re-installed everything, inserted the driver in the correct fashion and started a new project and copied my classes over and now it works! Thanks very much for your help, much appreciated. – rob9021 Jul 13 '15 at 09:24