I am beginner programmer, using Eclipse IDE to play with servlets. I am trying to establish connection between my Java program and a MySQL database, I have added the mysql-connector jar file to the project library, and it worked when I tried with a normal Java program, but when I try to do it with the servlet it is throwing java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver. Here I am attaching the details.
The code which got executed and retrieved the data.
package learn.http_servlets.form.jdbc_connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBCConnection {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");
String query = "show tables";
PreparedStatement pstmnt = con.prepareStatement(query);
ResultSet rs = pstmnt.executeQuery();
while(rs.next()) {
System.out.println("list of tables :"+rs.getString(1));
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
The one which is throwing error
package learn.http_servlets.form;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class Register extends HttpServlet {
public void procesRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("user_name");
String password = request.getParameter("user_password");
String email = request.getParameter("user_email");
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/register", "root", "root");
out.println("connection established sucessfully");
// String query = "insert into user(name, email, password) values(?,?,?)";
//
// PreparedStatement pstmnt = connection.prepareStatement(query);
// pstmnt.setString(1, name);
// pstmnt.setString(2, email);
// pstmnt.setString(3, password);
//
// pstmnt.executeUpdate();
//
// pstmnt.close();
}
catch(Exception e ) {
e.printStackTrace();
out.println("<h1>error occured!!</h1>");
}
finally {
if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
out.println("<h3>"+name+" "+email+" "+password+"</h3>");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
procesRequest(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
procesRequest(request, response);
}
}
I have downloaded the latest version of mysql-jdbc connecter and tried to fix it by adding it to project library. My intention is to store the data retrieved from a form using servlets to MySQL database.