【问题标题】:SQL Server jdbc connectionSQL Server jdbc 连接
【发布时间】:2013-04-13 21:43:18
【问题描述】:

我在 Windows 身份验证中在 localhost 上运行此代码 sn-p 但出现以下错误,但我已经在我的类路径中添加了 sqljdbc4 jar 在从 Eclipse 运行时,我还在构建路径中添加了 jar

import java.io.*;
import java.sql.*;
import java.util.GregorianCalendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

class  Cms_truncate
{
    public static void main(String[] args) 
    {
         Calendar cal = new GregorianCalendar();

         //String name="cmscim";
                 Connection conn = null;

         String url = "jdbc:sqlserver://localhost\SQLEXPRESS;databasename=yatin";
         String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
         String userName = ""; 
         String password = "";
         Statement stmt;
         try
         {

         Class.forName(driver);//.newInstance();
         conn = DriverManager.getConnection(url,userName,password);
         String query = "truncate table cim";
         stmt = conn.createStatement();
         int flag = stmt.executeUpdate(query);
         System.out.println("flag = "+flag); 
         conn.close();
        System.out.println("");
         } catch (Exception e) {
         e.printStackTrace();
         }

    }
}

错误:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. 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 Cms_truncate.main(Cms_truncate.java:28)

请帮帮我。

【问题讨论】:

  • 连接被拒绝:连接。验证连接属性。确保 SQL Server 实例正在主机上运行并在端口接受 TCP/IP 连接。确保到端口的 TCP 连接没有被防火墙阻止。 - 就是这样,检查实例是否在指定端口运行并且它正在接受连接请求,检查凭据是否正确!跨度>
  • 别忘了检查防火墙!

标签: java sql-server-2005 jdbc


【解决方案1】:

我认为是配置问题。

Go to Run -> SQL Server Configuration Manger

Expand -> SQL Server *version_no* Network Configuration

Then click on - "Protocol for SQLEXPRESS"

如果您发现TCP/IP 协议的状态为Disabled,那么您需要enable

此外,您还需要重新启动服务: 打开 cmd > 输入 Services,然后向下滚动到 SQL Server(MSSQLServer),然后重新启动服务

【讨论】:

    【解决方案2】:

    首先,当您在 URL 中使用 SQL 实例名称时,您必须指定 //[serverName[\\instanceName][:portNumber]]databaseName

    例如:jdbc:sqlserver://localhost\\SQLEXPRESS:1433;databasename=yatin

    一旦完成下一步:-

    1. 转到所有程序-> 搜索 SQL Server 配置管理器。打开它
    2. 在左侧窗格中展开 SQL Server 网络配置
    3. 在大多数情况下,单击实例名称的协议是 SQLEXPRESS 的协议
    4. 单击 TCP IP。 TCP IP 属性窗口将打开
    5. 选择已启用的属性并将所有属性监听为“YES”
    6. 导航到 IP 地址选项卡并检查 ip 所有部分 TC 动态端口是否为 1433,如果不是则设置它
    7. 然后应用就ok了

    8. 现在转到左侧窗格中 SQL Server 配置管理器下的 SQL Server 服务部分。点击它

    9. 在大多数情况下,您会看到数据库引擎正在运行一个实例名称为 SQLEXPRESS 的数据库引擎,右键单击它并停止它,然后重新启动它。

    现在重新运行代码

    【讨论】:

    • 小修正。在这里,您提到要更改“TCP 动态端口”。但实际上,我们必须更改 TCP 端口,而不是 TCP 动态端口。快乐编码。
    【解决方案3】:

    根据微软文档

    String url = "jdbc:sqlserver://localhost\SQLEXPRESS:[your_port_tcp];databasename=yatin";
    

    https://msdn.microsoft.com/pt-br/library/ms378428(v=sql.110).aspx

    【讨论】:

      【解决方案4】:

      您忘记提及实例名称。见Building the Connection URL解释如何在sqlserver中使用连接url

      如果你没有提到完整的服务器名,那么你会得到类似的错误

      com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost , port 1433 has failed. Error: "Connection refused: connect. 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.".
      

      我的服务器名称V_UDAY\FRAMEWORK,我删除了URL中的FRAMEWORK实例(在Java代码中)并执行,然后出现如下错误。

      com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host V_UDAY, port 1433 has failed. Error: "Connection refused: connect. 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(DriverManager.java:571)
      at java.sql.DriverManager.getConnection(DriverManager.java:215)
      at com.testsql.SqlServerDatabaseConnectionUsingJava.getLocalConnection(SqlServerDatabaseConnectionUsingJava.java:28)
      at com.testsql.SqlServerDatabaseConnectionUsingJava.main(SqlServerDatabaseConnectionUsingJava.java:18)
      Exception in getLocalConeection() The TCP/IP connection to the host V_UDAY, port 1433 has failed. Error: "Connection refused: connect. 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.".
      

      下图显示了服务器名称slash分隔

      执行下面的Java程序需要sqljdbc4。下载sqljdbc4 jar 或从Microsoft JDBC Driver 4.0 for SQL Server

      使用 Java 连接到 sqlserver 的示例。这里JSF 是数据库名称。

      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.SQLException;
      
      public class SqlServerDatabaseConnectionUsingJava {
          private static Connection connection = null;
          //1.jdbc driver name
          private static String SQL_JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
          // 2. Database URL, V_UDAY\FRAMEWORK is ServerName and JSF is DataBase name
          private static String URL = "jdbc:sqlserver://V_UDAY;instanceName=FRAMEWORK;databaseName=JSF";
          //3.Database credentials
          private static String USERNAME = "udaykiran";//UserName
          private static String PASSWORD = "Pa55word";//Password
      
      public static void main(String[] args) {
          getLocalConnection();
      }
      
      public static Connection getLocalConnection() {
          try {
              Class.forName(SQL_JDBC_DRIVER);// Register jdbc driver
      
              System.out.println("****Connect to Database****");
      
              //4. open a connection
              connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
      
              System.out.println("DataBase connect to: "+ connection.getMetaData().getDriverName());
              System.out.println("URL: "+ connection.getMetaData().getURL());
      
              setConnectionClose();
              System.out.println("Database Connection Closed");
          } catch (Exception e) {
              e.printStackTrace();
              System.err.println("Exception in getLocalConeection() "+e.getMessage());
          }
          return connection;
      }
      
      public static void setConnectionClose() throws SQLException {
          if (connection != null) {
              connection.close();
          }
      }
      
      }
      

      注意:如果您已删除实例名称,请在连接 url 时提及实例名称 FRAMEWORK

      【讨论】:

        【解决方案5】:

        此错误不是“身份验证错误”,而是“连接被拒绝”导致的“连接错误”。这意味着您需要指定正确的端口号。您需要检查您的 SQL Server 配置并更新您的连接字符串。

        根据MSDN docs,连接字符串应如下所示:

        jdbc:sqlserver://localhost:<insert_proper_port_here>\SQLEXPRESS;databasename=yatin
        

        您没有提供用户名或密码,一旦您弄清楚端口号,您可能需要这样做。有关详细信息,请参阅参考文档。

        【讨论】:

          猜你喜欢
          • 2017-10-17
          • 2014-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-04
          • 2019-04-14
          • 1970-01-01
          相关资源
          最近更新 更多