简体   繁体   English

JDBC 驱动程序问题 (SQL Server 18)

[英]Driver issue with JDBC (SQL Server 18)

java.sql.SQLException: No suitable driver found

I've been having a lot of difficulty with the JDBC on SQL Server.我在 SQL Server 上使用 JDBC 遇到了很多困难。 My goal here is to unearth the exact cause of the issue/error at hand.我的目标是找出手头问题/错误的确切原因。 I don't think it has anything to do with the driver itself;我认为这与驱动程序本身没有任何关系; I added mssql-jdbc-8.2.0jre13.jar to the referenced library for this project as well as adding jdk-13.0.1 under "installed JRE's" to ensure compatibility with the driver.我将mssql-jdbc-8.2.0jre13.jar添加到该项目的引用库中,并在“已安装的 JRE”下添加jdk-13.0.1以确保与驱动程序的兼容性。 Lastly, I added the line最后,我添加了这一行

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

but I'm still getting the same issue.但我仍然遇到同样的问题。 Is it the URL itself that's causing the issue?是 URL 本身导致了问题吗? I'm not sure if that's the right URL I should be using.我不确定这是否是我应该使用的正确 URL。 The database that I'm currently trying to access is premade and has the server name我当前尝试访问的数据库是预先制作的,并且具有服务器名称

occam-dbserver.cs.qc.cuny.edu\\dbclass,21433 . occam-dbserver.cs.qc.cuny.edu\\dbclass,21433

Must this be in the URL?这必须在 URL 中吗? Must it be something like一定是这样的

jdbc:sqlserver://occam-dbserver.cs.qc.cuny.edu:21433;Northwinds2019TSQLV5 . jdbc:sqlserver://occam-dbserver.cs.qc.cuny.edu:21433;Northwinds2019TSQLV5

package JBDC;

import java.sql.*;

public class Driver {

public static void main(String[] args) throws SQLException {

    Connection myConn = null;
    Statement myStmt = null;
    ResultSet myRs = null;
    String url = "jdbc:sqlserver:localhost:1433;Northwinds2019TSQLV5";
    String user = "student";
    String password = "abc";
    try {

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        myConn = DriverManager.getConnection(url, user , password);

        myStmt = myConn.createStatement();

        myRs = myStmt.executeQuery("SELECT *\r\n" + 
                "FROM [HumanResources].[Employee]");

        while (myRs.next()) {
            System.out.println(myRs.getString("EmployeeFirstName") + ", " + myRs.getString("EmployeeLastName"));
        }
    }
    catch (Exception exc) {
        exc.printStackTrace();
    }
    finally {
        if (myRs != null) {
            myRs.close();
        }

        if (myStmt != null) {
            myStmt.close();
        }

        if (myConn != null) {
            myConn.close();
        }
    }
}

}

The specific error I'm getting is我得到的具体错误是

java.sql.SQLException: No suitable driver found for jdbc:sqlserver:localhost;Northwinds2019TSQLV5
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at JDBC.Driver.main(Driver.java:19)

The .jar file is loaded on this line: .jar 文件在这一行加载:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

So that's not your problem.所以这不是你的问题。 Compare your url比较你的网址

jdbc:sqlserver:localhost:1433;Northwinds2019TSQLV5

to the example from the Docs文档中的示例

 jdbc:sqlserver://<server>:<port>;databaseName=AdventureWorks;user=<user>;password=<password>

So perhaps:所以也许:

String url = "jdbc:sqlserver://localhost:1433;databaseName=Northwinds2019TSQLV5";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM