gpt4 book ai didi

java - 在Java中使用SQL Driver连接到远程SQL Server

转载 作者:行者123 更新时间:2023-12-02 12:04:57 25 4
gpt4 key购买 nike

要点:

  • 使用 SQL Server 2005
  • 使用 JDK 1.8
  • JDK 1.8 不再支持 JDBC-ODBC 桥
  • 我的库中同时有 Microsoft SQLJDBC 4.0 和 6.2

我尝试用 Java 连接到远程 SQL Server 已经有一段时间了,但仍然无法连接。我希望使用我创建的 DSN 进行连接。 JDK 1.8 使用的新驱动程序需要一个我在下面创建的连接 URL。下面代码中的所有内容看起来都是正确的。但我仍然收到错误:

SEVERE: Java Runtime Environment (JRE) version 1.8 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0. java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.8 is not supported by this driver.
Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.

这是我的代码:

String connectionUrl = "jdbc:sqlserver://InterfaceDSN" +  
"databaseName=DB_Local;Trusted_Connection=True;integratedSecurity=true";

// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);

String SQL = "SELECT TOP 10 * FROM Person.Contact";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);

while (rs.next()) {
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
}catch (Exception e)
{
e.printStackTrace();
}

总结:知道我拥有什么和需要什么,是否有另一种方法可以连接到数据库,考虑到我将 JDK 1.8 与 SQL Server 2005 一起使用,我已经没有选择了。

最佳答案

发生了什么

正如消息所说:

java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.8 is not supported by this driver.
Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.

这是因为JDBC4.0有很多变化,意味着您需要使用更新版本的驱动程序。您无法在新版本上运行旧驱动程序。

如何修复

仅使用 6.2 或版本 6.2.2.jre8应该可以解决这个问题。

额外信息

在 JDBC 4.0 (Java 1.6+) 之后您不需要以下代码

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

关于java - 在Java中使用SQL Driver连接到远程SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46958310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com