gpt4 book ai didi

java - 无法通过java连接到SQL Server 2014

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:11 25 4
gpt4 key购买 nike

问题已更新(不再手动加载DriverManager,而是使用getConnection()方法):

package guii;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
* This program demonstrates how to establish database connection to Microsoft
* SQL Server.
* @author www.codejava.net
*
*/
public class JdbcSQLServerConnection {

public static void main(String[] args) {

Connection conn = null;

try {

String dbURL = "jdbc:sqlserver://ASUS\\YES:1433";
String user = "TestingUser";
String pass = "12345";
conn = DriverManager.getConnection(dbURL, user, pass);
if (conn != null) {
DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
System.out.println("Driver name: " + dm.getDriverName());
System.out.println("Driver version: " + dm.getDriverVersion());
System.out.println("Product name: " + dm.getDatabaseProductName());
System.out.println("Product version: " + dm.getDatabaseProductVersion());
}

} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}

问题是此代码产生的异常。我无法找出抛出该特定异常的原因。

用户名、密码和服务器名称经过仔细检查,绝对正确。

当前抛出此异常:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'TestingUser'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2532)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1917)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1061)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at guii.JdbcSQLServerConnection.main(JdbcSQLServerConnection.java:25)

最佳答案

故障排除说明

  1. 再次使用integratedSecurity=true

  2. 检查 system32 文件夹中是否有 sqljdbc_auch.dll查看this link确保您使用正确的版本(32 位与 64 位)确保 sqljdbc_auch.dll 位于 Windows 系统路径

  3. 检查服务器实例的属性是否设置正确:访问权限,确保它确实监听端口 1433,...查看this link并且不要只查看已接受的答案,还要查看其他两个答案。

  4. 确保您的防火墙不会阻止任何连接

  5. 检查您的Java版本是否与您使用的jdbc版本匹配。sqljdbc4.2需要Java 1.8,jdbc4.1需要java 1.7。 look here

关于java - 无法通过java连接到SQL Server 2014,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35906256/

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