SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed.

匿名 (未验证) 提交于 2019-12-03 01:41:02

问题:

I am running Ubuntu 16.04 and using SQL Server Express and Oracle Java 9:

$ java --version java 9.0.1 Java(TM) SE Runtime Environment (build 9.0.1+11) Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode) 

I have installed SQL Server jdbc driver (sqljdbc_6.2.2.0_enu.tar.gz in https://www.microsoft.com/en-us/download/details.aspx?id=55539)

/home/t/program_files/RDBMS/JDBC/mssqlserverjdbc_6.0/enu/jre8/sqljdbc42.jar 

I wrote some simple code to test using the driver :

// JDBC driver name and database URL                                                                                                                                        String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // static final String DB_URL = "jdbc:mysql://localhost/"; // connect to a DBMS                                                                                             String DB_URL = "jdbc:microsoft:sqlserver://localhost/STUDENTS"; // connect to a specific database in a DBMS                                                                //  Database credentials                                                                                                                                                    String USER = "SA"; String PASS = "password";  //Register JDBC driver                                                                                                                                                      try{     Class.forName(JDBC_DRIVER); }catch(Exception e){ //Handle errors for Class.forName                                                                                                                          e.printStackTrace(); }      //Open a connection                                                                                                                                                     System.out.println("Connecting to database...");  try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); // connect to DBMS, not to a DB                                                                          Statement st = conn.createStatement()){  }catch(SQLException se){ //Handle errors for JDBC                                                                                                                               se.printStackTrace(); } 

Compilation is fine:

$ javac BasicOperations.java 

Running isn't.

$ java -cp .:/home/t/program_files/RDBMS/JDBC/mssqlserverjdbc_6.0/enu/jre8/sqljdbc42.jar BasicOperations Connecting to database... com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost/STUDENTS, port 1433 has failed. Error: "localhost/STUDENTS. Verify the connection pro\ perties. 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.".         at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)         at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:242)         at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2369)         at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:551)         at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1963)         at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)         at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)         at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)         at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)         at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:678)         at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)         at BasicOperations.main(BasicOperations.java:46) 

How shall I solve the problem? Is it correct that I should do equivalence to https://stackoverflow.com/a/12430561/1224441 on Ubuntu? Because I am using SQL Server Express on Ubuntu, I only know how to access it on terminal using sqlcmd.

Thanks.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!