gpt4 book ai didi

java.sql.Exception : No Suitable Driver Found for jdbc:sqlserver. ..使用集成安全性

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

我已阅读各种帖子,但无法使我的 java 连接与 SQL Server 2014 Express 正常工作:

代码似乎无法找到驱动程序。

我将文件 mssql-jdbc-6.2.1.jre8.jar 放置在应用程序构建文件夹中。我尝试了各种有关添加 CLASSPATH 的选项,但没有成功,但似乎这对于最新版本的驱动程序来说并不是必需的。

我没有使用 IDE

            //===================================================================== 

import java.sql.*;

public class connectURL {

public static void main(String[] args) {


System.out.println("----------------------------------Start Connection---" + "\r\n");



// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver:LocalHost:1433;" +
"databaseName=stocksandshares;integratedSecurity=true;";

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

try {
// Establish the connection.

con = DriverManager.getConnection(connectionUrl);

// Create and execute an SQL statement that returns some data.
String SQL = "SELECT TOP 10 * FROM Person.Contact";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);

// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
}

// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}

finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}

最佳答案

您的连接字符串不正确。

你错过了开头的两个正斜杠。

您的连接字符串应如下所示:

jdbc:sqlserver://LocalHost:1433;databaseName=stocksandshares;integratedSecurity=true;

参见this Microsoft documentation page解释如何构建 SQL Server 连接字符串。

帮自己一个忙,开始使用 IDE。

关于java.sql.Exception : No Suitable Driver Found for jdbc:sqlserver. ..使用集成安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46329948/

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