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

55,583

Solution 1

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.

Solution 2

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

Share:
55,583
Amit Nandan Periyapatna
Author by

Amit Nandan Periyapatna

BY DAY: Software Developer at HomeSpotter developing Real Estate data solutions for clients in the USA and Canada. BY NIGHT: Open Source Web Application Developer who loves to explore new technologies and features among the vast array of free frameworks. HOBBIES: Badminton, BOOK WORM and Foodie!

Updated on August 24, 2020

Comments

  • Amit Nandan Periyapatna
    Amit Nandan Periyapatna almost 4 years

    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.

  • Jules
    Jules over 6 years
    Or, better stilll, learn how to use maven, gradle, or some other build management tool that will do all this for you.