无法使用测试java程序建立与SQL Server 2008的数据库连接

时间:2016-12-08 18:03:48

标签: sql-server jdbc

我正在研究Core Java Volume 2高级功能。我必须连接到任何数据库才能在Database Programming章节中执行源代码。我可能无法分享整个源代码,可能是因为版权问题。但基本上我尝试使用简单的TestDb java程序连接到SQL Server,并且连接属性(如DriverName,DB Url,用户名和密码)位于db.properties文件中,如下所示:

#jdbc.drivers=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433    ;databaseName=COREJAVA;int‌​egratedSecurity=true"
jdbc.username="sa"
jdbc.password="123456"

连接代码存在于getConnection()函数中,如下所示:

public static Connection getConnection() throws SQLException, IOException
{
    Properties props = new Properties();
    try (InputStream in = Files.newInputStream(Paths.get("database.properties")))
    {
        props.load(in);
    }
    String drivers = props.getProperty("jdbc.drivers");
    if (drivers != null) System.setProperty("jdbc.drivers", drivers);
    String url = props.getProperty("jdbc.url");
    String username = props.getProperty("jdbc.username");
    String password = props.getProperty("jdbc.password");

    return DriverManager.getConnection(url, username, password);
}

由于这个stackpost,我先修复了TCP的错误 JDBC connection failed, error: TCP/IP connection to host failed

但是现在当我使用cmd中的以下命令运行程序时:

java -cp .;sqljdbc4.jar test.TestDB

我得到以下stacktrace:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '"sa"'. ClientConnectionId:2986bb7f-4f57-4932-b4c1-614ab3684ee0
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
        at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
        at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
        at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
        at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at test.TestDB.getConnection(TestDB.java:68)
        at test.TestDB.runTest(TestDB.java:35)
        at test.TestDB.main(TestDB.java:19)

我已提到Java connection to a SQL Server Database: Login failed for user 'sa'

我的IPALL端口设置为1433,在Server Configuration Manager中启用了TCP / IP,我可以使用带有'sa'帐户的SQL Server管理工作室正常登录。我正在使用微软网站上最新的sqljdbc4.jar。

1 个答案:

答案 0 :(得分:1)

@PordThompson谢谢你!我从用户名和密码制作中删除了双引号

 If ActiveControl.Name = txtMyMutilineTextBox.Name Then Exit Sub 

 e.SuppressKeyPress = True
 SelectNextControl(ActiveControl, True, True, True, True)

现在它正在连接到数据库!