我正在尝试使用 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)
当我将网址更改为
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 进行连接,并且运行良好。但它通过代码失败了。
我不确定我哪里出错了。我无法通过代码建立成功的连接。请帮我。