简体   繁体   中英

MySQL Connection Error in Java - com.mysql.jdbc.Driver

I have been trying to connect my java application to a MySQL database and have used the following lines of code:

import java.sql.*; 
public class AcceptValues extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

             String url = "jdbc:mysql://localhost:3306/db_name";
             String driver = "com.mysql.jdbc.Driver";
             String userName = "root";
             String password = "";
             try {
             Class.forName(driver);
             Connection conn = DriverManager.getConnection(url,userName,password);
             out.print("Connection estd");
             Statement st = conn.createStatement();
             st.executeQuery("insert into table_name values(2, 'testing');");
             }
             conn.close();
             } catch (Exception e) {
                 out.print("Error : " +e.getMessage());
             }
        }
}

I have also set the classname to mysql-connector-java-5.1.29-bin.jar which I downloaded from the mysql website. But I still am not able to connect to the database using the above lines of code and is throwing the exeption com.mysql.jdbc.Driver as an error.

I'm new to java development and any help would be deeply appreciated. Thank you.

Add the mysql-connector-java-5.1.29-bin.jar in the WEB-INF/lib folder. Its not enough to just add the jar in your built path. Even I had the same issue at the start.

Here is you answer,

right click on project properties --> Java Build Path --> Libraries --> add Jar to your project(which you already downloaded)

don't right you connection information in servlet file create separate file for connection

call the class whenever you need

import java.sql.*;
public class DBConn {

private String url = "jdbc:mysql://localhost:3306/test";
private String driver = "com.mysql.jdbc.Driver";
private String userName = "root";
private String password = "root";
private Connection con = null;

private void getConnection() {

     try {
         Class.forName(driver);
         if(con == null){
             con = DriverManager.getConnection(url,userName,password);
         }
         System.out.print("Connection estd");
     }catch (Exception e) {
         System.out.print("Error : " +e.getMessage());
    }
}


/**for desktop application */
public static void main(String[] arg){

    DBConn con = new DBConn();
    con.getConnection();
  }
}

You can also getconnection in your sevelet using following code

import java.sql.*; 
public class AcceptValues extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

   DBConn con = new DBConn();
   con.getConnection();
}
}

this sample code but you can make it more efficient this is just sample,

please mention full error/exception information

the exeption com.mysql.jdbc.Driver as an error.

i met this problem, in most case, there are two reasons behind this problem. one is lack of the mysql jar file, the two is losing sight of starting the the mysql service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM