0

I have an issue in which I can't make MySQL request through java because the driver cannot be initialized as it can't be found. There is question already asked that attempted at solving a similar issue but after trying many solution presented nothing made it.

The issue comes from the line Class.forName(JDBC_DRIVER); where an attempt to initialize com.mysql.jdbc.Driver is made. I have a ClassNotFoundException there. According to answers to question already asked, I should simply download JAR file MySQL Connector driver, and add it to my project classpath by importing it: I created a folder named lib in my classpath and added the mysqlc-connector.x.x.x.jar file to it and refreshed my project.

Here is my code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class FirstExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/ChatWorld";

   //  Database credentials
   static final String USER = "root";
   static final String PASS = "********";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //STEP 2: Register JDBC driver
      Class.forName(JDBC_DRIVER);

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //STEP 4: Execute a query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();
      String sql;
      sql = "SELECT * FROM account";
      ResultSet rs = stmt.executeQuery(sql);

      //STEP 5: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         int id  = rs.getInt("id");
         String username = rs.getString("username");
         String email = rs.getString("email");
         String birthdate = rs.getString("birthdate");

         //Display values
         System.out.print("ID: " + id);
         System.out.print(", Username: " + username);
         System.out.print(", Email: " + email);
         System.out.println(", Birthdate: " + birthdate);
      }
      //STEP 6: Clean-up environment
      rs.close();
      stmt.close();
      conn.close();
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");
}//end main
}//end FirstExample

I tried many searches on Internet, and all sites I went on all recommended on importing MySQL connector library and their solution seem to be ruling out the issue according to other users, except in my case as it looks like, but I don't know what is going wrong for me.

I am using Eclipse as an IDE.

Let's just point out I am new to java, I am a self-learner so I have not learned every subtle point of java already and I might have missed other important point in my configuration.

Thanks for your help in advance to anyone.

P.S.: I hesitated to post a comment to some answerers or ask a new question, but thought my issue could be more specific.

Community
  • 1
  • 1
cram2208
  • 416
  • 6
  • 14
  • Have you included `mysql-driver jar file` ? – Atul Sharma Jun 25 '16 at 12:16
  • http://www.java2s.com/Code/Jar/c/Downloadcommysqljdbc515jar.htm Import this if not included.. – Atul Sharma Jun 25 '16 at 12:18
  • I have downloaded http://dev.mysql.com/downloads/connector/j/ Mysql connector jar file. That's it. Isn't it that file you talk about ? – cram2208 Jun 25 '16 at 12:18
  • Download the above Jar mysql-jdbc jar and add it.. Or to check Try importing com.mysql.jdbc.Driver .. if this gives error means jar is missing.. – Atul Sharma Jun 25 '16 at 12:20
  • Can you please explain more clearly the way you added the library to your project classpath? –  Jun 25 '16 at 12:20
  • @Nikos I created a folder named lib in my classpath and added the mysqlc-connector.x.x.x.jar file to it and refreshed my project and retry compiling. – cram2208 Jun 25 '16 at 12:26
  • 4
    " I created a folder named lib in my classpath and added the mysqlc-connector.x.x.x.jar file to it and refreshed my project" ---- that is not enough. When you see it in Eclipse project explorer right click it select "Build Path" > "Add to Build Path" – c0der Jun 25 '16 at 13:00
  • You can't create a folder in your classpath, because the classpath is a list of jar files either specified in an environment variable, in the commandline, inside the jar file itself or in your IDE. – Mark Rotteveel Jun 26 '16 at 07:06
  • @c0der Even though the question was marked as duplicate, you still found the one single step I was missing which was adding the JAR file to build path, thank you a lot for that. I had previous instructions read and all were perfectly stating to add it to classpath/buildpath which kind of got me lost. Now I was shown the light to my problem. +1 – cram2208 Jun 27 '16 at 23:39
  • @cram2208 I am glad it help. Thank for the +1 – c0der Jun 29 '16 at 07:05
  • @Mark Rotteveel I don't think this is an exact duplicate. The question is similar but the problem and the solution are different. – c0der Jun 29 '16 at 07:10
  • @c0der To quote from the highest voted answer on the duplicate: _"If you're using an IDE like Eclipse or Netbeans, then you can add it to the classpath by adding the JAR file as Library to the Build Path in project's properties. "_ seems to me exactly the solution you mention. – Mark Rotteveel Jun 29 '16 at 07:13
  • @ Mark Rotteveel I don't think so, because I guess OP thought that he already added the jar to the class path. I will let the OP comment on if the answers to the "duplicate" question solved his problem. – c0der Jun 29 '16 at 07:19
  • @Mark Rotteveel I think that though this question was marked as duplicate that it could still very well be an independent question that need specific answer. I understood the principle of connecting java to mysql db. I had not understood I had to add a library to buildpath which was my misstep and wouldn't have noticed without this specific answer from c0der. – cram2208 Jun 29 '16 at 09:30
  • I disagree with that assessment, the answer I reference contains a full set of steps to fix these problems, that is why it is the canonical answer for these type of problems with MySQL + JDBC. If someone disagrees with me, they are free to vote to reopen, but I don't think that is going to happen. – Mark Rotteveel Jun 29 '16 at 10:21

0 Answers0