关于Java连接SQLServer中Login failed for user 'sa'.

搞了一天了,还是没有解决
由于需要用到SQLServer,数据库登录方式是Sqlserver和Windows混合登录,在Microsoft SQL Server Management Studio中可以用sa账户登录,但是在Java代码中登录不了,这是为什么?求大牛指点:
[code="java"]import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBtest {
public static void main(String[] args){
String URL="jdbc:sqlserver://192.168.88.201:1433;databaseName=master";
String USERNAME="sa";
String PASSWORD="123";
String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(conn);
}

}
[/code]
代码没有问题,也能用Microsoft SQL Server Management Studio中可以用sa账户登录,就是用Java连不上/抛异常是:
[code="java"]com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'.
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(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DBtest.main(DBtest.java:15)[/code]

2个回答

是不是密码不对,你在建一个用户,用另外一个用户试试

sql server的版本多少?各个版本的连接驱动有一点差异,你百度一下,仔细核对下。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐