连接到MS SQL服务器时出错

时间:2017-03-22 08:25:28

标签: 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);  
    } 
  

与主机192.168.1.35的连接,命名实例server-sql失败。错误   :&#34; java.net.SocketTimeoutException:接收超时&#34;。验证服务器并在   站点名称并检查没有防火墙阻止UDP流量到端口1434   或SQL Server 2005或更高版本,验证SQL Server Browser服务是否为runni   在主持人身上。

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   ñ   警告:ConnectionID:1 ClientConnectionId:6684e1dc-9950-4082-8fa9-73d5bf4b38b6    此驱动程序不支持服务器主要版本:8。   此驱动程序不支持SQL Server版本8。 ClientConnectionId:6684e1d   C-9950-4082-8fa9-73d5bf4b38b6

4 个答案:

答案 0 :(得分:1)

而不是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

答案 1 :(得分:0)

这对我有用:

try {

    Class.forName("com.mysql.jdbc.Driver");

    try {

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Raumfahrt", "root", "");

    String insertStrM ="INSERT INTO Raumfahrt.Mitarbeiter VALUES(?,?,?,?)";

    PreparedStatement prestmt = con.prepareStatement(insertStrM);

    prestmt.setInt(1, m.getmID());
    prestmt.setString(2, m.getMname());
    prestmt.setString(3, m.getMname());
    prestmt.setInt(4, m.getMabt());

    prestmt.executeUpdate();

    }catch (SQLException e) {

        System.out.print("error");
        e.printStackTrace();
    }

    }catch (ClassNotFoundException e) {

        e.printStackTrace();
    }

答案 2 :(得分:0)

我知道1433是默认端口,但您可以在ip addr 192.168.1.35:1433之后在连接网址中指定此端口号并尝试一下吗?

答案 3 :(得分:0)

<dependency>
    <groupId>net.sourceforge.jtds</groupId>
    <artifactId>jtds</artifactId>
    <version>1.3.1</version>
</dependency>

    Connection conn = null;
    ResultSet rs = null;
    String url = "jdbc:jtds:sqlserver://192.168.1.20;instance=SERVER-SQL;DatabaseName=MainDataBase";
    String driver = "net.sourceforge.jtds.jdbc.Driver";
    String userName = "sa";
    String password = "sa";
    try {
      Class.forName(driver);
      conn = DriverManager.getConnection(url, userName, password);
      System.out.println("Connected to the database!!! Getting table list...");
      DatabaseMetaData dbm = conn.getMetaData();
      rs = dbm.getTables(null, null, "%", new String[] { "TABLE" });
      while (rs.next()) { System.out.println(rs.getString("TABLE_NAME")); }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        conn.close();
        rs.close();
    }