如何设置SQL Server连接URL?

时间:2014-09-08 21:16:02

标签: java sql-server jdbc

我在连接到我在电脑上为小应用程序创建的SQL Server数据库时遇到问题。该服务器被称为“INSPIRONN5110-D'”。我使用Windows身份验证登录到服务器。已经从microsoft下载驱动程序并将其内容复制到程序文件文件夹,并将路径(C:\ Program Files \ Microsoft JDBC Driver 4.0 for SQL Server \ sqljdbc_4.0 \ enu \ sqljdbc4.jar)添加到路径和类路径变量中(不确定使用哪一个)仍然会出现此错误。

Creating Connection.
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host INSPIRONN5110-D, 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.".
    .....

这是我尝试连接数据库的方法。

private Connection connectToDB() {

        String connectionUrl = "jdbc:sqlserver://INSPIRONN5110-D; databaseName=MemoryHelp;";
        Connection con = null;
        try{
//          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("Creating Connection.");
            con = DriverManager.getConnection(connectionUrl);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
//      } catch (ClassNotFoundException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
        } finally{

            System.out.println("Connection created.");

        }


        return con;
    }

尝试将连接URL更改为" jdbc:sqlserver:// localhost;的databaseName = MemoryHelp;"

立即收到此错误:

Creating Connection.
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. 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.".
    ....

终于解决了。必须转到SQL Server配置管理器并启用到服务器的tcp / ip连接,然后将sqljdbc_auth.dll文件从驱动程序的文件夹复制到system32文件夹,并使用以下连接URL可以使其工作:< / p>

String connectionUrl = "jdbc:sqlserver://localhost;databaseName=MemoryHelp;integratedSecurity=true";

1 个答案:

答案 0 :(得分:0)

你没有提供服务器的ip或主机名,也没有提供端口。您需要提供这些以便连接到服务器。

在您的情况下,如果服务器与应用程序在同一台计算机上,那么它应该是: JDBC:SQLSERVER://本地主机:3306

端口3306只是我假设您使用的默认端口,这取决于您托管服务器的端口。