Amit Nandan Periyapatna Amit Nandan Periyapatna - 1 year ago 159
Java Question

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 {
Connection conn = DriverManager.getConnection(url,userName,password);
out.print("Connection estd");
Statement st = conn.createStatement();
st.executeQuery("insert into table_name values(2, 'testing');");
} 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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download