在 java 中找不到适合 jdbc 的驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14621003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:51:35  来源:igfitidea点击:

No suitable driver found for jdbc in java

javasql-serverjdbcconnection

提问by sahar

Im new in sql server, and want to create connection between java and sql server. my connection code is:

我是 sql server 的新手,想在 java 和 sql server 之间建立连接。我的连接代码是:

 public static void main(String[] args) {

    Connection con;
       try {

       String connectionUrl = "jdbc:sqlserver://HELLO-PC:1433; databaseName=Attendance Teachers;";

        con = DriverManager.getConnection(connectionUrl, "", "");
        System.out.println("connected");
        java.sql.Statement st = con.createStatement();    
      }
        catch (SQLException ex) {

        Logger.getLogger(AttendanceTeachers.class.getName()).log(Level.SEVERE, null, ex);
    }

my server name is 'HELLO-PC' an i also add sqljdbc.jar. i see error:

我的服务器名称是“HELLO-PC”,我还添加了 sqljdbc.jar。我看到错误:

Feb 01, 2013 11:24:46 AM attendance.teachers.AttendanceTeachers main
SEVERE: null
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://HELLO-PC:1433;   databaseName=Attendance Teachers;
at java.sql.DriverManager.getConnection(DriverManager.java:604)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at attendance.teachers.AttendanceTeachers.main(AttendanceTeachers.java:30)

I realy need help.thanks.

我真的需要帮助。谢谢。

回答by Bohemian

Your problem is not in the code, it's that the jar file containing the JDBC driver class for sqlserver is not in the classpath when you execute your program.

您的问题不在于代码,而是当您执行程序时,包含 sqlserver 的 JDBC 驱动程序类的 jar 文件不在类路径中。

Visit the appropriate sqlserver web site (google) and download the JDBC driver jar and put it in your build path if executing in an IDE, or in the same directory as your program jar if executing from the command line.

访问相应的 sqlserver 网站 (google) 并下载 JDBC 驱动程序 jar,如果在 IDE 中执行,请将其放在构建路径中,如果从命令行执行,则将其放在与程序 jar 相同的目录中。

回答by java_hfchina

First, you need to do this:

首先,你需要这样做:

private final String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

try {
    Class.forName(dbDriver).newInstance(); // use java reflection to load the database driver
} catch (Exception ex) {
    System.out.println("failure");
}

回答by Nirdesh Sharma

This issue will normally occur when you are using a older verison of JDBC driver if you are working with JDBC 4.0

如果您使用的是 JDBC 4.0,则当您使用旧版本的 JDBC 驱动程序时,通常会发生此问题

When you download the SQlserver JDBC driver from the Microsoft website, you will get two jar files: sqljdbc4.jar and sqljdbc.jar.

当您从 Microsoft 网站下载 SQlserver JDBC 驱动程序时,您将获得两个 jar 文件:sqljdbc4.jar 和 sqljdbc.jar。

1.sqljdbc.jar: Provides support for JDBC 3.0 specification

1.sqljdbc.jar:提供对JDBC 3.0规范的支持

2.sqljdbc4.jar: Provides support for JDBC 4.0 specification

2.sqljdbc4.jar:提供对JDBC 4.0规范的支持

So.check with sqljdbc4.jar

So.check 与 sqljdbc4.jar