连接到sql server时端口号不正确

时间:2014-03-14 17:19:02

标签: java netbeans jdbc driver

我正在尝试从Netbeans连接到SQL Server,如下所示。我在我的Libraries forlder项目中有sqljdbc4.jar。

try{
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    connRemoteforGlobal = java.sql.DriverManager.getConnection("jdbc:sqlserver://xx.xx.x.xxx:1433/test",RemoteSQLServerUser,RemoteSQLServerPass);

    if(connRemoteforGlobal != null)
    {
        System.out.println("Connection Successful !");
    }
}
catch(SQLException ex2){
    ex2.printStackTrace();
    System.out.println("Error Trace in Connection : " + ex2.getMessage());
}

收到以下错误:

netbeans或我的连接中是否还需要其他设置?端口号?

Error Trace in Connection : The port number 1433/test is not valid.
com.microsoft.sqlserver.jdbc.SQLServerException: The port number 1433/test is not valid.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:691)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at mypackage.myclass.call(myclass.java:408)
    at mypackage.myclass.call(myclass.java:25)
    at javafx.concurrent.Task$TaskCallable.call(Task.java:1259)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

1 个答案:

答案 0 :(得分:11)

应该是:

jdbc:sqlserver://xx.xx.x.xxx:1433;databaseName=Test

格式为:

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

http://technet.microsoft.com/en-us/library/ms378428.aspx

查看http://technet.microsoft.com/en-us/library/ms378988.aspx的属性。无论如何,1433似乎是默认的端口号,因此没有必要指定它。