0

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();
        }
    }
}

output of the above program

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);
        
    }
}

This is the project structure, here you can find the mysql-connecter in reference libraries directory

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Always search Stack Overflow thoroughly before posting. – Basil Bourque Mar 30 '23 at 22:55
  • Make sure you have configured your project to copy the library to the WEB-INF/lib folder of your WAR. As an aside, in "normal" web applications, you wouldn't use `DriverManager` like this, you would use a pre-configured `javax.sql.DataSource` backed by a connection pool. Also, this style of servlets is pretty "old school". – Mark Rotteveel Apr 01 '23 at 07:30
  • @BasilBourque yeah I've searched before and nothing helped me – Manche Umesh Apr 02 '23 at 11:09
  • Finally I have found a video on YouTube and it helped me to resolve this issue, It was solved by adding mysql-connector jar file to project properties > deployment assembly. – Manche Umesh Apr 02 '23 at 11:16

0 Answers0