在java中构建一个sql连接url

时间:2014-10-24 16:31:11

标签: java sql-server jdbc mssql-jdbc

所以我试图通过java连接到网络上的数据库,我很难获得与服务器的连接。我相信我的问题在于连接网址。我已经尝试了一些不同的东西来测试它,但还没有任何工作,所以希望有人可以帮助我。我得到的错误是:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.1.16, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
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 dbTest.db.dbConnect(db.java:23)
at dbTest.testConnection.main(testConnection.java:11)

虽然取决于我使用的连接URL有时会出现java.net.sockettimeoutexception错误。这是我目前用来尝试连接的代码:

        try
    {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Properties props = new Properties();
        props.setProperty("user","sa");
        props.setProperty("password","");
        Connection conn = DriverManager.getConnection("jdbc:sqlserver://192.168.1.16;databaseName=ECCS",props);
        //MSSQL01\\ECLAIMS_DATA
        System.out.println("connected");            
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

注释掉的MSSQL01 \ ECLAIMS_DATA是我尝试连接的服务器和数据库的名称,我也尝试过使用它。关于一切都需要的地方我只是有点失落。我试过在连接字符串中包含用户名和密码,我尝试过使用getConnection(url,username,password)。我还不确定databaseName是否需要去哪里。任何人都可以帮助我吗?

0 个答案:

没有答案