无法在Eclipse IDE中使用java建立与SQL Server 2008的数据库连接 [英] Unable to establish database connection to SQL Server 2008 using java in Eclipse IDE

查看:195
本文介绍了无法在Eclipse IDE中使用java建立与SQL Server 2008的数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用Eclipse IDE中的Java代码连接到HP Operations Manager数据库。
我可以通过Microsoft SQL Server Management Studio 2008连接成功,但是通过代码失败。
我已经安装了用于SQL Server的Microsoft JDBC驱动程序4.0

I am trying to connect to HP Operations Manager Database using Java code in Eclipse IDE. I am able to connect successfully through Microsoft SQL Server Management Studio 2008 but it fails through code. I have installed "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();

    }

 }

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

I get the following error when I execute this code:

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进行连接,它的工作效果非常好。但是通过代码失败。

I have explicitly added an inbound rule in windows firewall to allow UPD traffic on 1434 port, then disabled the firewall. But I still get this error. The credentials provided here are used for connection using Microsoft SQL Server Management Studio and it works perfectly fine. But it fails through the code.

我不知道我在哪里错了。我无法通过代码建立成功的连接。请帮助我。

I am not sure where I am going wrong. I am unable to establish a successful connection through the code. Please help me.

推荐答案

最后我能够解决这个问题。问题是与url和auth dll。将网址更改为

Hey Thanks all for your responses. Finally I was able to resolve the issue. The problem was with the url and auth dll. Changed the url to

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

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

and added the location of "sqljdbc_auth.dll" in java.library.path. It worked!

再次感谢您的努力,帮助我:)

Thanks again for your efforts to help me :)

这篇关于无法在Eclipse IDE中使用java建立与SQL Server 2008的数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆