java - 与 MS SQL 服务器的连接错误

标签 java sql-server

为什么我无法连接到 MS SQL 服务器?

依赖:

<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>mssql-jdbc</artifactId>
  <version>6.1.5.jre8-preview</version>
</dependency>

Java 代码:

import java.sql.*;

try { 
      // Load the SQLServerDriver class, build the 
      // connection string, and get a connection 

      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
      String connectionUrl = "jdbc:sqlserver://192.168.1.35\\SERVER-SQL;" + 
                             "database=MainDataBase;" + 
                             "user=user;" + 
                             "password=rdthnb137"; 
      Connection con = DriverManager.getConnection(connectionUrl); 
      System.out.println("Connected.");

      // Create and execute an SQL statement that returns some data. 

      String SQL = "SELECT * from history";  
      Statement stmt = con.createStatement();  
      ResultSet rs = stmt.executeQuery(SQL);

      // Iterate through the data in the result set and display it. 

      while (rs.next()) {  
        System.out.println(rs.getString(1) + " " + rs.getString(2));  
      }
    }  
    catch(Exception e) { 
      System.out.println(e.getMessage()); 
      System.exit(0);  
    } 

The connection to the host 192.168.1.35, named instance server-sql failed. Error : "java.net.SocketTimeoutException: Receive timed out". Verify the server and in stance names and check that no firewall is blocking UDP traffic to port 1434. F or SQL Server 2005 or later, verify that the SQL Server Browser Service is runni ng on the host.

Image of setup of port of a copy of the server

我是从本地机器连接的。防火墙已关闭。

已安装带有 Service Pack 4 更新的 MS SQL Server 2000。

当使用Connection con = DriverManager.getConnection( "jdbc:sqlserver://192.168.1.35;database=MainDataBase;", "user", "rdthnb137");错误:

22, 2017 1:49:58 PM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogi n WARNING: ConnectionID:1 ClientConnectionId: 6684e1dc-9950-4082-8fa9-73d5bf4b38b6 Server major version:8 is not supported by this driver. SQL Server version 8 is not supported by this driver. ClientConnectionId:6684e1d c-9950-4082-8fa9-73d5bf4b38b6

最佳答案

代替 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

尝试:

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

而不是

"jdbc:sqlserver://192.168.1.35\\SERVER-SQL;" +"database=MainDataBase;"

尝试:

jdbc:microsoft:sqlserver://192.168.1.35\\SERVER-SQL:1433;DatabaseName=MainDataBase

关于java - 与 MS SQL 服务器的连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42946222/

相关文章:

java - 选取文件后未找到文件

如果不存在,则连接并插入 SQL

与第一和第二高薪水相关的SQL查询

c# - 将 ASP.NET 网站连接到 SQL 数据库

sql-server - 无法从 Visual Studio 连接到 Azure SQL 数据库

java - 由 : java. lang.NoClassDefFoundError : Could not initialize class com. jayway.restassured.RestAssured 引起

java - 在 Android 上合并两个 WAVE 文件(连接)

java - 如何限制 NestedScrollView 内 RecyclerView 的高度

java - XML 单元测试 - 打开 XML - 检查它是否有效 - 检查某些标签是否存在于 XML 中

sql server合并多个数据集不重复数据