java - 无法在 Eclipse IDE 中使用 java 建立与 SQL Server 2008 的数据库连接

标签 java eclipse sql-server-2008

我正在尝试在 Eclipse IDE 中使用 Java 代码连接到 HP Operations Manager 数据库。 我能够通过 Microsoft SQL Server Management Studio 2008 成功连接,但通过代码失败。 我已经安装了“Microsoft JDBC Driver 4.0 for SQL Server”

代码:

import java.sql.*;

public class ConnectDatabase {

    Connection dbConnection = null;

    String dbName = "openview";
    String serverip="10.105.219.102";
    String serverport="1433";
    String url = "jdbc:sqlserver://"+serverip+"\\OVOPS;databaseName="+dbName+"";
    String userName = "HPOM-QA-WIN\\Administrator"; 
    String password = "Nbv12345";
    final String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    Statement statement = null;
    ResultSet rs = null;
    int updateQuery = 0;

    public Connection getConnection() {

        System.out.println(url);
        try{
             Class.forName(driverName).newInstance();

             dbConnection = DriverManager.getConnection(url,userName,password);
             System.out.println(DriverManager.getDrivers());

             statement = dbConnection.createStatement();

             String QueryString = "select Id from openview.dbo.OV_MS_Message where OriginalServiceId like '{FaultDn[1]}'";

             updateQuery = statement.executeUpdate(QueryString);

             if(updateQuery!=0){
                System.out.println("success" + updateQuery);
             }
             statement.close();
             dbConnection.close();
          }catch (Exception e){
              e.printStackTrace();
         }
         return dbConnection;

     }

     public static void main(String[] args) {

        ConnectDatabase cDB = new  ConnectDatabase();
        cDB.getConnection();

    }

 }

执行此代码时出现以下错误:

jdbc:sqlserver://10.105.219.102\OVOPS;databaseName=openview com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host 10.105.219.102, named instance ovops failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.  For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:3589)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:1225)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:972)
    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 com.ucs.test.ConnectDatabase.getConnection(ConnectDatabase.java:27)
    at com.ucs.test.ConnectDatabase.main(ConnectDatabase.java:51)

当我将 url 更改为

String url = "jdbc:sqlserver://"+serverip+"\\OVOPS:"+serverport+";databaseName="+dbName+"";

我收到以下错误:

jdbc:sqlserver://10.105.219.102\OVOPS:1433;databaseName=openview com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'HPOM-QA-WIN\Administrator'. ClientConnectionId:f1d323b7-9998-418c-b2a2-f2a7bd7b9b04
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
    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 com.ucs.test.ConnectDatabase.getConnection(ConnectDatabase.java:27)
    at com.ucs.test.ConnectDatabase.main(ConnectDatabase.java:51)

我已在 Windows 防火墙中明确添加入站规则以允许 1434 端口上的 UPD 流量,然后禁用防火墙。但我仍然收到此错误。 此处提供的凭据用于使用 Microsoft SQL Server Management Studio 进行连接,并且运行良好。但是它通过代码失败了。

我不确定我哪里出错了。我无法通过代码建立成功的连接。请帮助我。

最佳答案

嘿,谢谢大家的回复。最后我能够解决这个问题。问题出在 url 和 auth dll 上。将 url 更改为

"jdbc:sqlserver://10.105.219.102:1433;instance=OVOPS;DatabaseName=openview;integratedSecurity=true" 

并在 java.library.path 中添加了“sqljdbc_auth.dll”的位置。有效!

再次感谢您为我提供的帮助:)

关于java - 无法在 Eclipse IDE 中使用 java 建立与 SQL Server 2008 的数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12283034/

相关文章:

java - mockito 拒绝将 TypeSafeMatcher 与通用方法 API 配对

java数据库对象级安全性

eclipse - 无法实例化 [org.springframework.web.context.support.XmlWebApplicationContext]

java - 在 Eclipse 中读取 Assets

asp.net - 文本框应该只接受日期

java - 通过 Java 向 POM.xml 文件添加注释

java - Google Dataflow 作业在 eclipse 上运行时工作正常,但当我使用 mvn 编译它并使用 java -cp 命令运行文件时出现错误

java - 仅显示来自 eclipse 中某些文件夹的警告

c# - 在不考虑区域设置的情况下将日期插入 SQL

mysql - group by 子句不分组