与JDBC连接到mssql失败

时间:2018-06-01 07:35:27

标签: sql-server jdbc

我正在使用以下方式检查与eclipse和sql的连接,我正在使用带有Windows身份验证的默认数据库

import java.sql.*;
public class TestConnection {

    static final String dbUrl = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=master";
//master is the name of default database of sql server
    public static void main(String[] args) {

        try {
            Connection conn = DriverManager.getConnection(dbUrl);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }
        // TODO Auto-generated method stub

        System.out.println("Done.");

    }

}

我使用的是sql server 2008 R2 我收到错误“连接链接失败”

错误

com.microsoft.sqlserver.jdbc.SQLServerException:与主机MYPC的连接,命名实例sqlexpress失败。错误:“java.net.UnknownHostException:MYPC”。验证服务器和实例名称,并检查没有防火墙阻止到端口1434的UDP流量。对于SQL Server 2005或更高版本,请验证SQL Server Browser服务是否正在host.Done上运行

at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:228)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:5465)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:2151)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1903)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1762)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1077)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:623)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at TestConnection.main(TestConnection.java:15)

0 个答案:

没有答案