Search code examples
javamysqlservletsjdbcsqlexception

JDBC:MySql connection is not working in Ajax request -Java


I have exactly same function in both Main method and other method JDBC connection is working fine. If I call the other function it throws error java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/wine:

I have included MySql Driver in library [Netbeans];

processRequest method:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String process = (String) request.getParameter("process");
    String name = (String) request.getParameter("process");
    String phone=(String) request.getParameter("phone");
    String email = (String) request.getParameter("email");
    String pwd = (String) request.getParameter("pwd");
    PrintWriter out = response.getWriter();
    out.println("Hello");
   signup(out,process,name,email,phone,pwd);

}

signup method:

private static int signup(PrintWriter out,String process,String name,String email,String phone,String pwd){
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wine", "root", "");


        out.println("Process Not Found");
        if (process == "signup") {

            String query = "INSERT INTO user(name,phone,email,password,role) VALUES(?,?,?,?,1)";
            stmt = con.prepareStatement(query);
            stmt.setString(1, name);
            stmt.setString(2, phone);
            stmt.setString(3, email);
            stmt.setString(4, pwd);

            stmt.execute();

        } else {
            out.println("Process Not Found");
        }
    } catch (SQLException e) {
        // do something appropriate with the exception, *at least*:

        out.println(e);
        e.printStackTrace();
        return 0;
    }
    return 1;
}

Main Method :

public static void main(String[] args) {

    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    String process = "signup";
    String name = "Test";
    String phone="45885";
    String email = "[email protected]";
    String pwd = "dkjsdh";

    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wine", "root", "");



        if (process == "signup") {

            String query = "INSERT INTO user(name,phone,email,password,role,status) VALUES(?,?,?,?,1,1)";
            stmt = con.prepareStatement(query);
            stmt.setString(1, name);
            stmt.setString(2, phone);
            stmt.setString(3, email);
            stmt.setString(4, pwd);

            stmt.execute();

        } else {
            System.out.println("Process Not Found");
        }
    } catch (SQLException e) {
        // do something appropriate with the exception, *at least*:

        System.out.println(e);
        e.printStackTrace();
    }
}

Solution

  • There are two options that can be tried:

    Class.forName("com.mysql.jdbc.Driver").newInstance() //older bug
    

    and

    Class.forName("com.mysql.jdbc.Driver");
    

    In the comment I pasted the older bug solution when I meant to paste the second one.

    Either way I am glad that it worked out for you