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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22411691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:35:14  来源:igfitidea点击:

Port number is not correct while connecting to sql server

javanetbeansjdbcdriver

提问by

I am trying to connect to SQL Server as follows from Netbeans. I have sqljdbc4.jar in my Libraries forlder of the project.

我正在尝试从 Netbeans 连接到 SQL Server,如下所示。我在项目的库文件夹中有 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());
}

And getting the following error:

并收到以下错误:

Is there any additional settings required in netbeans or in my connection? Port number?

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)

回答by LuckyLuke

It should be:

它应该是:

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

This is the format:

这是格式:

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/ms378428.aspx

Look at http://technet.microsoft.com/en-us/library/ms378988.aspxfor properties. Anyway, 1433 seems to be the default port number so it is not necessary to specify it.

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