Reputation: 207
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();
}
}
}
Upvotes: 0
Views: 2686
Reputation: 207
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();
}
}
}
Upvotes: 0