Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Java and mySQL - ClassNotFoundException: com.mysql.jdbc.Driver

Posted on 2008-11-19
Medium Priority
Last Modified: 2013-11-24
Hi Guys, i've actually been researching this for 11 hours now!! No Joy.
Basically i am creating a program to connect to database and return values - easy
I am using Netbeans and i have mySQL server installed on my local machine, when i run the file in netbeans, it works! It connects to the DB and returns values.

The problem comes when i go outside of Netbeans. If i create a webpage and load the applet through that, it just doesnt work, i get the error
"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver".

In Netbeans i added this file to the Libraries mysql-connector-java-5.1.7-bin.jar. I build and works fine in netbeans, browse to the html, open it and it doesn't work.
I have also uploaded it on a TomCat 5.5 server and i get the same response. I must be doing something wrong.
Please help!!!!

 try {
        Connection con = null;
        try {
// Load the JDBC driver
            String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
            Statement stmt =null;

                    // MySQL MM JDBC driver
// Create a connection to the database
    /** Creates a new instance of DBUtil */
            String url = "jdbc:mysql://localhost/test?autoReconnect=true";

            String username = "root";
            String password = "password";
            con = DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException e) {
        } }
    catch (SQLException e) {j);
Question by:dajoebomb
  • 6
  • 5
LVL 86

Expert Comment

ID: 22999951
You need to put the driver jar in the Tomcat lib directory

Author Comment

ID: 22999996
Thanks for your reply,
I am really new to this, i logged on to the server and in /comon/lib/ there was V3.1 of the JDBC driver. I put the V5.1 in anyway, but it doesn't work. Is this the directory that you meant?

Author Comment

ID: 23000183
Please someone help me, I am actually going insane!!!
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Expert Comment

ID: 23000291
Try also putting the jar into the WEB-INF/lib directory of your webapp

Author Comment

ID: 23000411
No I am afraid that doesn't work either.
My folders under ROOT are:

in the body of the index, i have <applet code="org.me.hello.MyApplet" archive="HelloApplet.jar" width="400px" height="400px"/>

Am i missing anything or have anything in the wrong place?
Thanks once again CEHJ
LVL 86

Expert Comment

ID: 23000453
Hang on a minute - are you saying that the applet is doing the JDBC? If so, the driver jar has to be specified in the applet's archive tag too

Author Comment

ID: 23000477
Yeah the applet is doing the JDBC. I later on going to connect clients to the server and recieve information from the database.

How do I specify it in the applets archieve tag?
LVL 28

Expert Comment

ID: 23000498
>String url = "jdbc:mysql://localhost/test?autoReconnect=true";  
Maybe try adding standard port.
String url = "jdbc:mysql://localhost:3306/test?autoReconnect=true";    
LVL 86

Accepted Solution

CEHJ earned 440 total points
ID: 23000501
Something like
<applet code="org.me.hello.MyApplet" archive="HelloApplet.jar,mysql-connector-java-5.0.4-bin.jar" width="400px" height="400px"/> 

Open in new window


Author Closing Comment

ID: 31518534
Thank you so much, i have now got rid of that problem!!
Would you know it its brought up another error though -com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up

Giving up souds good, but it needs to be done, thanks again CEHJ!
LVL 86

Expert Comment

ID: 23002507
The db must be on the same host as the applet's loaded from or it will need to be signed

Author Comment

ID: 23003418
Yeah, I believe it is, when i do it on my machine where mySQL is running, it returns the same error as when i do it on the Tomcat server.
I read that it might be something to do with pools??
I'm going to look into that next

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question