Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

mysql

jsp

jdbc

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.

like image 704
Amit Nandan Periyapatna Avatar asked Dec 19 '22 17:12

Amit Nandan Periyapatna


1 Answers

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.

like image 86
Standin.Wolf Avatar answered Jan 13 '23 21:01

Standin.Wolf