首页 新闻 会员 周边 捐助

JAVA连接SQL Server 2012出错!求助!

0
[已解决问题] 解决于 2019-07-13 15:15

import java.sql.Connection;
import java.sql.DriverManager;

public class Test {

public static void main(String[] args) {

    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        System.out.println("加载驱动成功");
        String url ="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=CYWL";
        String username ="sa";
        String password ="1234";
        Connection conn = DriverManager.getConnection(url,username,password);
        System.out.println("连接数据库成功");
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.print("连接失败");
    }
}

}

=======================================================
加载驱动成功
com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'sa' 登录失败。
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/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at Test.main(Test.java:14)
连接失败

JCpaul的主页 JCpaul | 菜鸟二级 | 园豆:202
提问于:2019-07-11 17:16
< >
分享
最佳答案
0

先断点看看

奖励园豆:5
刘下来 | 小虾三级 |园豆:919 | 2019-07-11 18:08
其他回答(1)
0

没有开启sa的登录吧,需要设置下就行

2012 | 园豆:21645 (高人七级) | 2019-07-12 08:19

已解决,是端口问题。

支持(0) 反对(0) JCpaul | 园豆:202 (菜鸟二级) | 2019-07-13 15:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册