gpt4 book ai didi

用户与 SQL Server 登录的 Java 连接失败

转载 作者:行者123 更新时间:2023-12-01 22:34:15 26 4
gpt4 key购买 nike

我想连接到 sql server 2017

我尝试使用 sql server management studio 连接到 sql server,没有出现错误

enter image description here

我执行一些查询,例如:

 SELECT fullname
FROM [AssetInst.Integ].[dbo].[Employee]

我现在的问题是使用java hibernate进行连接。

我使用sqljdbc4.jarjdk 1.6

首先我尝试使用这个 java 类来测试连接:

public static void main(String[] args)
{

try
{

DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());

Connection con = DriverManager.getConnection("jdbc:sqlserver://192.168.0.12\\MSSQLSERVER2017:1433;databaseName=AssetInst.Integ","Test_user","P@ssw0rd123@");


String query =" select * FROM [AssetInst.Integ].[dbo].[Employee]";
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while(resultSet.next())
{
System.out.print("fullname: " + resultSet.getString("fullname"));
}
}catch(Exception ex)
{
ex.printStackTrace();
}
}

但我有这个错误:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'Test_user'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
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:2529)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1905)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1893)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1045)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at com.dq.hibernate.generator.Snippet.main(Snippet.java:27)

最佳答案

这对我来说是工作

DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
connectionMsSQl = DriverManager.getConnection(jdbc:sqlserver:/192.168.0.12;instanceName=AssetInst.Integ, "Test_user", "P@ssw0rd123@");

检查名称和密码。试试这个希望它对你有用

关于用户与 SQL Server 登录的 Java 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58535141/

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