0

This seems to be a common problem, but I could not find a working solution. I've looked through dozens of thread and have been working with my teacher. I am trying to connect to MYSQL using JDBC. I'm also using a tomcat server and running xubuntu 12.04. I am getting ClassNotFound exception.

I've tried the JDBC mysql-connector-java.jar driver placed in both /tomact/lib and /usr/share/java and have manually built paths in each instance. I've also tried adding paths through deploy assembly. I've tried using EXPORT CLASSPATH. Nothing has worked. I cannot get this exception to stop being thrown. Does anyone have other solutions I can try?

my code:

public static Connection getConnection() {
    if(connection!=null){
        return connection;
    }else{
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String dbURL = "jdbc:mysql://144.91.20.136:3306/jewelryInventoryGallison";
            String username = "javaee";
            String password = "mills2012";

            connection = DriverManager.getConnection(dbURL, username, password);
            return connection;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

test class:

public static void main(String[] args) throws Exception{ 
      Connection conn = DatabaseUtils.getConnection();
      Statement st = conn.createStatement();
      ResultSet rs = st.executeQuery("SELECT * FROM Inventory");
      while(rs.next()) {
       System.out.println(rs.getString("name"));
      }

 }

and trace:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at utils.DatabaseUtils.getConnection(DatabaseUtils.java:19)
at utils.DatabaseUtils.main(DatabaseUtils.java:34)

Exception in thread "main" java.lang.NullPointerException at utils.DatabaseUtils.main(DatabaseUtils.java:35)

your help will be greatly appreciated!

Ravi
  • 30,829
  • 42
  • 119
  • 173
BooBailey
  • 540
  • 1
  • 9
  • 31
  • 1
    May be you are missing drivers ...! This may be help you http://dev.mysql.com/downloads/connector/j/ – Chella Dec 13 '12 at 04:53
  • thanks Chella, but I have the drivers. I'm sorry my post wasn't clear. that was what I meant by JDBC jar – BooBailey Dec 13 '12 at 04:59
  • @user1857898 either accept any answer or let us know more about your problem else you will get close vote – Ravi Dec 13 '12 at 06:05
  • I'm sorry coder, I was away from my computer. I also didn't see your post when I refreshed the page. I do have the library you are referring to. I put down all I know. I'm sorry I wasn't more explicit. – BooBailey Dec 13 '12 at 06:48

5 Answers5

2

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

You missing MySQL driver library. Since, you are using Mysql database, make sure you have included mysql-connector-java.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.8-dmr</version>
</dependency>
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • Coders, I'm sorry it took me so long to get back to you. I was AFK. I do have that library to which you referred. I'm sorry, I didn't call it by its full name in original post. I have researched a lot of thread that had me move that library to different locations, build paths to it in several ways. nothing worked. So I am looking for an alternative solution – BooBailey Dec 13 '12 at 06:56
  • @user1857898 did add the library, which i have mentioned in my post. What is the problem now ?? You are getting same error or different?? – Ravi Dec 13 '12 at 08:01
2

Putting the MySQL-connector-java-5.1.6.jar in the Tomcat/lib directory finally worked for me. Shouldn't have to do this so I suspect my Tomcat installation isn't optimum and deploying the war file might be problematic

1

Version Mismatch could cause this error. I myself was stuck in this error for days. Please check the version of jdbc connector. Latest connectors require java 1.8. In my case my tomcat version didn't support the connector version i added so please check for that also.

PS : Tomcat - 8.0.32 ; mysql-connector - 5.1.30 with java 1.7 works (Y)

Sagar Sahni
  • 394
  • 5
  • 16
0

modify 'common.loader' property in the 'catalina.properties' file in the tomcat 'conf' folder as shown below

common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar

Also add 'mysql-connector-java-5.0.8-bin.jar' in the tomcat 'lib' folder

Fathah Rehman P
  • 8,401
  • 4
  • 40
  • 42
0

Eventhough connector is configured in classpath tomcat doesn't load that correctly. so paste the mysql-connector-jar file in lib folder under WEB_INF also and try running again.

enter image description here

srihitha
  • 371
  • 2
  • 14