0

I am writing servlet to connect with mysql database using mysqlconnector java but it is giving error

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I have tried same with core java using the same driver it is connecting to mysql and query is also running but using servlet it is not working.

my code is for servlet is:

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;





public class RegisterServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String n = request.getParameter("userName");
        String p = request.getParameter("password");
        String e = request.getParameter("email");
        String c = request.getParameter("course");

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = null;
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/phpmyadmin/saiiff","root", "");
            PreparedStatement ps =  conn
                    .prepareStatement("insert into eemeze values(?,?,?,?)");

            ps.setString(1, n);
            ps.setString(2, p);
            ps.setString(3, e);
            ps.setString(4, c);

            int i = ps.executeUpdate();
            if (i > 0)
                out.print("You are successfully registered...");

        } catch (Exception e2) {
            System.out.println(e2);
        }

        out.close();    
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I think mysql library is not included... – Vishnu T S Dec 04 '17 at 10:45
  • mysql driver is there I have added using build path add external libraries..the same code is running from core java and data is also inserted but using servlet it is not working – manafi group Dec 04 '17 at 10:47
  • Did you check if jdbc connector jar was included in web library? That is was the server able to access it? Are you using eclipse? – Harsh Dec 04 '17 at 10:51
  • yes it is there it is working if i remove port no 3306 from connection driver manager – manafi group Dec 04 '17 at 11:02

1 Answers1

-1
  1. Download the mysql jar from http://www.java2s.com/Code/JarDownload/com.mysql/com.mysql.jdbc_5.1.5.jar.zip
  2. And Build on classpath
  • I already have jar but the problem is with port no. if i remove port no 3306 it is working fine – manafi group Dec 04 '17 at 11:43
  • Why suggest an ancient version of the driver from an untrusted source? MySQL Connector/J 5.1.5 is from 2007, the latest version is 5.1.45 from 30 November 2017. A trusted download link would be https://dev.mysql.com/downloads/connector/j/ On top of that, this wouldn't even solve this specific problem. – Mark Rotteveel Dec 04 '17 at 16:34