有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法从Eclipse和JDBC连接到SQL Server express 2012

我尝试连接到安装在我自己机器上的SQL Server express 2012。 在Ms SQL的属性中,名称为:BGHFVUI535V2\SQLEXPRESS

在Java中,我有:

try
{
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    databaseName = "dblfa1;integratedSecurity=true";
    String url = "jdbc:sqlserver://BGHFVUI535V2\SQLEXPRESS;user=mysa;password=mypsw;databaseName=dbmy";
    Connection con = DriverManager.getConnection(url);

}
catch (Exception e)
{
    e.printStackTrace();
}

我得到一个例外:

com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host BGHFVUI535V2, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.  For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:3589)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:1225)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:972)
    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 ClassStart.main(ClassStart.java:34)

在c#中,我有连接字符串:

string connectionString = "Server=BGHFVUI535V2\\SQLEXPRESS;Database=dbmy;User Id=mysa;Password=mypsw;"; 

它是有效的


共 (1) 个答案

  1. # 1 楼答案

    试试这个

    String url = "jdbc:sqlserver://localhost;user=MyUserName;password=*****;";
    

    来自MSDN网站Link