0
votes

For some reasons the code below does not execute when I try to create a web service to insert id, name and idno into a mysql database. I have added MYSQL JDBC Driver - MYSQL connector library but I get this error "Severe: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/web". I have gone through some answers of people but does not seem to get an answer. What could be the cause? Anyone?

package com.database.www;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

@WebService(serviceName = "database")
public class database {


    @WebMethod(operationName = "hello")
    public void hello(@WebParam() int id, String name, String idno ) {

        try (

                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web", "root", "");
                Statement stm = conn.createStatement();) {



            String insert = "INSERT INTO `web` " + "VALUES ("+id+", '"+name+"','"+idno+"' )";
            int exc = stm.executeUpdate(insert);
            System.out.println("The SQL Command is: " + insert);
            System.out.println("Inserted Successfullly!");
        }
     catch (SQLException e){
         e.printStackTrace();
     }



    }
}
1
How have you added the mysql connector? And how are you deploying this? - Elliott Frisch
In the libraries, add library then I picked JDBC Lib on netbeans IDE. - bmm
And how are you deploying it? You should be able to run it in netbeans, but to run it on your server it typically needs to be installed on the server. - Elliott Frisch
What exactly should I install? I am just clicking deploy after clean and build. - bmm

1 Answers

0
votes

I just included Class.forName("com.mysql.jdbc.Driver"); and it worked.

package com.database.www;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;


@WebService(serviceName = "database")
public class database {


    @WebMethod(operationName = "hello")
    public void hello(@WebParam() int id, String name, String idno ) throws ClassNotFoundException {

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

        try (

                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web", "root", "");
                Statement stm = conn.createStatement();) {



            String insert = "INSERT INTO `web` " + "VALUES ("+id+", '"+name+"','"+idno+"' )";
            int exc = stm.executeUpdate(insert);
            System.out.println("The SQL Command is: " + insert);
            System.out.println("Inserted Successfullly!");
        }
     catch (SQLException e){
         e.printStackTrace();
     }



    }
}