【问题标题】:java.sql.SQLException: No suitable driver found for jdbc:mysqljava.sql.SQLException:找不到适合 jdbc:mysql 的驱动程序
【发布时间】:2016-11-28 06:53:11
【问题描述】:

由于某些原因,当我尝试创建 Web 服务以将 id、name 和 idno 插入 mysql 数据库时,以下代码不会执行。我添加了 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();
     }



    }
}

【问题讨论】:

  • 如何你添加了 mysql 连接器?你是如何部署它的?
  • 在库中,添加库,然后我在 netbeans IDE 上选择了 JDBC Lib。
  • 你是如何部署它的?您应该能够在 netbeans 中运行它,但要在您的服务器上运行它,通常需要在服务器上安装
  • 我到底应该安装什么?我只是在清理和构建后单击部署。

标签: java mysql web-services jdbc


【解决方案1】:

我刚刚添加了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();
     }



    }
}

【讨论】:

  • 听起来您将驱动程序部署为 Web 应用程序的一部分,而不是安装在应用程序服务器中。
  • 请注意,对于现代 JDBC 驱动程序 Class.forName 应该不是必需的。
猜你喜欢
  • 2015-09-22
  • 2017-08-26
  • 1970-01-01
  • 2012-07-30
  • 2014-06-09
  • 2020-05-21
  • 2017-10-17
  • 2015-10-02
  • 2014-08-05
相关资源
最近更新 更多