-1

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 = "Test@gmail.com";
    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();
    }
}
Sarath Kumar
  • 1,136
  • 1
  • 18
  • 41
  • what about this line " Class.forName("com.mysql.jdbc.Driver").newInstance();"? have you tried it just in case? – bichito Mar 28 '17 at 00:21
  • I was just about to ask the same. – Coop Mar 28 '17 at 00:23
  • Not driver related but just FYI: String name = (String) request.getParameter("process"); Should this be "name"? – tronmcp Mar 28 '17 at 00:27
  • 1
    Please see [how to compare strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Once you fix that let us know what else is broken. – stdunbar Mar 28 '17 at 01:35
  • @stdunbar it's working fine. The only problem is jdbc connection. – Sarath Kumar Mar 28 '17 at 06:00
  • 1
    @efekctive `newInstance()` is entirely unnecessary (except if you are using a Connector/J of +/- 15 years ago with a bug). – Mark Rotteveel Mar 28 '17 at 07:41
  • Did you deploy the driver as part of your webapplication, or in the tomcat `lib` folder? In the first case you need to explicitly load the driver once using `Class.forName("com.mysql.jdbc.Driver")`, otherwise - assuming it is a JDBC 4+ driver - it will be loaded automatically. – Mark Rotteveel Mar 28 '17 at 07:42
  • I know but trying it is really easy and rules out being caught by the obvious – bichito Mar 28 '17 at 12:56
  • 1
    I meant the Class.forName(...) with out the newInstance(). Sorry – bichito Mar 28 '17 at 13:48
  • @MarkRotteveel I deployed as part of web application. My only question it's working with Main method. If I tried from ajax it's throws error. I don't why people down voted. Without understanding the question – Sarath Kumar Mar 28 '17 at 15:29
  • @efekctive your correct. Post a answer. Problem solved. – Sarath Kumar Mar 28 '17 at 16:21
  • 1
    It is working from main, because then the driver is on the initial classpath and will get detected and loaded automatically by `DriverManager`, when the driver is part of your web application deployment (instead of in the tomcat lib folder), then the automatic driver loading doesn't work because of the class-loader hierarchy restricting the visibility of the driver so `DriverManager` can't automatically load it. That said: in web applications you usually don't use `DriverManager` to create connections, but a `DataSource` instead (usually with a connection pool). – Mark Rotteveel Mar 28 '17 at 17:41

1 Answers1

1

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

bichito
  • 1,406
  • 2
  • 19
  • 23