4

Sory for my weak english, I want to connect mysql connection with java, I added the mysql-connector.jar but class not found error still continue. Here is the screenshot of build path.

What should I do for the connect mysql

enter image description here

Here is the code

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


try{


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

                conn = DriverManager.getConnection(DB_URL,USER,PASS);


                String sql1 = "INSERT INTO test (name) VALUES(?)";
                stmt = conn.prepareStatement(sql1);

                stmt.setString(1, okey);
                }

              // execute insert SQL stetement
                stmt .executeUpdate();


                stmt.close();
              conn.close();
           }catch(SQLException se){
              //Handle errors for JDBC

              se.printStackTrace();
           }catch(Exception e){
              //Handle errors for Class.forName
              e.printStackTrace();

           }finally{

              try{
                 if(stmt!=null)
                    stmt.close();
              }catch(SQLException se2){
              }// nothing we can do
              try{
                 if(conn!=null)
                    conn.close();
              }catch(SQLException se){

              }//end finally try
           }//end try
Muhammet Demir
  • 1,995
  • 5
  • 21
  • 42
  • This is clearly a classpath problem.... You should have "Driver" inside "com.mysql.jdbc" package and that package must be, in turn, in the jar "mysql-connector-java-5.1.53-bin.jar"...... check your References libraries... – Victor Jun 11 '15 at 17:39
  • 1
    You shouldn't even need to call `Class.forName("com.mysql.jdbc.Driver");` since JDBC v4 which this library implements. – Pshemo Jun 11 '15 at 17:40
  • 1
    How do you run this code? Is it standalone application, or are you deploying it on server? – Pshemo Jun 11 '15 at 17:51
  • deploying on my vpn server – Muhammet Demir Jun 11 '15 at 17:53
  • 1
    Do you have this library in your classpath on that vpn server? In other words, does machine which runs this code have access to mysql connector jar? – Pshemo Jun 11 '15 at 17:55
  • I don't know my server how to access mysql connector jar @Pshemo – Muhammet Demir Jun 11 '15 at 18:15
  • 2
    Then you will have to learn it. It is hard to help you if we don't even know how exactly you are running your code, or how do you deploy it. – Pshemo Jun 11 '15 at 18:19
  • @mdemir Is your entire app that single class? Or is that just a fragment of a much bigger piece of code? – Paul Jun 11 '15 at 18:40
  • A 7 line program with his config would produce same results – AsConfused Jun 11 '15 at 18:45
  • @AsConfused "A 7 line program..." The nature of the overall code may indicate the right way to deploy the connector jar. If it is a one class, standalone test program Drew's steps 1-3 may work. But the OP seems to be suggesting it needs to be deployed to the 'vpn server' which might indicate that fragment is embeded in a webapp, or some such, if so this will likely determine where the connector jar needs to be deployed. – Paul Jun 11 '15 at 19:06

2 Answers2

2

I don't know what IDE you are using there, but... are you sure you shouldn't have added the connector jar under 'Libraries' instead of 'Order and Export'? Also, is the connector jar available to your deployed application (server container, EJB server, whatever) and not just available to you IDE?

Strictly speaking you probably don't need the connector jar to be known to your IDE: it will know about the JDBC interfaces and classes from java.sql.* (assuming you have configured it to know about the JDK it is to use) and that is all it should need to be able to compile your JDBC code. However, at runtime your application will need the classes from the connector jar available. How you achieve this depends in what kind of application you are building (standalone, webapp etc) and where you are deploying it (webserver, EJB container, etc).

Paul
  • 3,009
  • 16
  • 33
  • If you are not sure then don't post it as answer but ask OP about it in comment. Also IDE looks like Eclipse and judging by available options (up, down, top, button, select/deselect all) OP couldn't add this library via this tab, so (s)he most probably added it via Libraries tab. – Pshemo Jun 11 '15 at 17:39
  • IDE is Eclipse as Pshemo say, and I already added jar file Libraries tab – Muhammet Demir Jun 11 '15 at 17:45
  • Your problem is probably not your IDE but the classpath of your deployed app (whatever it is that when running is throwing the exception you are seeing). What type of application is it? How is it deployed? – Paul Jun 11 '15 at 17:47
  • "I don't know" and "strictly speaking probably" sound a bit uncertain. – AsConfused Jun 11 '15 at 18:34
2

Putting the connector jar file in the "WEB-INF/lib" folder will work.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • Could you please add a little more description about the explanation you provide? – abarisone Apr 26 '16 at 10:05
  • you simply put the jar file containing mysql Driver class (your connector.jar or mysql.jar file) in the "lib" folder of "WEB-INF". the class path of this lib folder is already set in IDE and you wont even need to build path for the file. your project must run fine then. – Vijay Devda Apr 27 '16 at 05:21
  • Would like to know why it works when adding the connector.jar to web-inf but not in the project libraries. This solution works (Thanks) but if someone asks me why i wouldn't have something to say about. – Kil jeaden Dec 10 '19 at 16:16