Class not found exception com.mysql.jdbc.driver even i added jar file

Sory for my weak english, i want to connect to mysql connection from java, i added mysql-connector.jar but class not found, error continues. Here is a screenshot of the build path.

What should I do to 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

      

+3


source to share


2 answers


I don't know which IDE you are using there, but ... are you sure you shouldn't add the jar in Libraries instead of Order and Export? Also, is the jar available for the deployed application (server container, EJB server, whatever), not just the IDE available to you?



Strictly speaking, you probably shouldn't use a connecting jar that your IDE will know about: it will know about the JDBC interfaces and classes from java.sql.*

(assuming you've configured it to know the JDK it should use) and that's it. what it takes to be able to compile your JDBC code. However, at runtime, your application will need classes from an available connection jar. How you achieve this depends on what kind of application you are building (standalone, webapp, etc.) and where you deploy it (web server, EJB container, etc.).

+2


source


Place the jar connector file in the "WEB-INF / lib" folder.



+1


source







All Articles