由于某些原因,当我尝试创建一个将id,name和idno插入mysql数据库的Web服务时,下面的代码不会执行。我添加了MYSQL JDBC驱动程序 - MYSQL连接器库,但是我收到此错误“严重:java.sql.SQLException:没有为jdbc找到合适的驱动程序:mysql:// localhost:3306 / web”。 我已经通过了一些人的答案,但似乎没有得到答案。可能是什么原因?任何人吗?
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();
}
}
}
答案 0 :(得分:0)
我刚刚加入了Class.forName("com.mysql.jdbc.Driver");
,但它确实有效。
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();
}
}
}