由于某些原因,当我尝试创建将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();
     }



    }
}

最佳答案

我只包含了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();
     }



    }
}

10-01 03:45
查看更多