【问题标题】:Can not connect to ms sql server using windows authentication on remote system无法在远程系统上使用 Windows 身份验证连接到 ms sql server
【发布时间】:2013-09-23 11:57:10
【问题描述】:

我试图连接到安装在远程系统上的 ms sql server 2008。但它显示错误。以下是我尝试过的方式

import java.io.File;
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;
import java.sql.ResultSet;  
import java.sql.Statement;  


public class mssql {  
    public static void main(String[] args) {  
        try {  
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();

            Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.220:1433;databaseName=sales;integratedSecurity=true;");
            if(!(connection==null))
            {
                System.out.println("connected");
            }

//            


        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  

这是我遇到的错误

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.1.220, port 1433 has failed. Error: "connect timed out. Verify the connection properties. 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:190)
    at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
    at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at mssql.main(mssql.java:14)

此问题的任何解决方案

【问题讨论】:

  • 可以通过SQL Server Management Studio或者“telnet 192.168.1.220 1433”访问远程服务器吗?
  • @Rainer.R 说无法打开连接
  • @Rainer.R 我的 Windows 防火墙阻止了它,现在我可以远程登录了。但我无法连接到 sql server
  • "连接超时。验证连接属性。确保 SQL Server 实例正在主机上运行并且在端口接受 TCP/IP 连接。确保到端口的 TCP 连接未被防火墙阻止。"

标签: java sql sql-server jdbc


【解决方案1】:

对于TCP 连接,2008 SQL 实例是否配置为listening

开始、Microsoft SQL Server 2008、配置工具、SQL Server 配置管理器 SQL Server 网络配置
[实例名称]
的协议 应该列出四个项目:

  • 共享内存
  • 命名管道
  • TCP/IP
  • 威盛

对于您的环境,哪些应该启用,哪些应该禁用?大多数设置都要求启用共享内存和 TCP/IP,其他设置则禁用。

【讨论】:

  • 这应该是评论,而不是答案。
【解决方案2】:

尝试从命令行连接:

sqlcmd -S 192.168.1.220 -E

另外,检查一下:

  • 是否配置了服务器防火墙(允许端口 1433 或 sqlservr.exe);
  • 您的实例名称是否正确?如果您使用的是 SQL Server express,则服务器名称为 192.168.1.220\sqlexpress;
  • 是否为服务器启用了 TCP 连接(请参阅 jdev 的回答);
  • 是否为服务器启用了远程连接(可以在 SQL Server Management Studio、服务器属性、连接中更改);
  • 端口 1433 是否正确?默认情况下,只有未命名的实例使用静态端口 1433。其他实例使用动态端口并需要 SQL Server Browser 才能发现。

【讨论】:

    【解决方案3】:
    public class mssql {  
        public static void main(String[] args) {  
            try {  
                //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); modify
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    
                Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.220:1433;databaseName=sales;integratedSecurity=true;");
                if(!(connection==null))
                {
                    System.out.println("connected");
                }
    
    //            
    
    
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }  
    

    那你看看这个帖子:

    JDBC: Simple MSSql connection example not working

    【讨论】:

    • 它显示这个错误 java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run( Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass( Unknown Source) 在 java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method)
    • 那你看看这个帖子:stackoverflow.com/questions/12523865/…
    【解决方案4】:

    -首先让你的 sql server 从配置管理器接收 tcp 连接。 - 将 sqljdbc 库添加到项目中。 - 添加库的 .dll 文件作为 vm 参数 ex: -Djava.library.path=...... - 以下面的代码为例:

     String connectionUrl = "jdbc:sqlserver://localhost;" +
         "databaseName=Timesheet;integratedSecurity=true;";
        Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;
      try {
    
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         con = DriverManager.getConnection(connectionUrl);
    
    
         String SQL = "select top 100 * from paycal";
         stmt = con.createStatement();
         rs = stmt.executeQuery(SQL);
         int i =0;
    
         while (rs.next()) {
    
            jTable1.setValueAt(rs.getString("dayname"), i, 0);
            jTable1.setValueAt(rs.getString("dater"), i, 1);
            i++;
    
         }
    

    【讨论】:

      猜你喜欢
      • 2014-07-15
      • 2023-03-07
      • 2013-05-07
      • 2013-09-29
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多