Unable to establish database connection to SQL Server 2008 using java in Eclipse IDE

53,461

Solution 1

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" 

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

Thanks again for your efforts to help me :)

Solution 2

It took me a while to sort this issue out, but you have to head into the SQL Server Configuration Manager application. When that's loaded, expand the [SQL Native Client 11.0 Configuration] > Client Protocols.

Enable all three (Shared Memory, TCP/IP, and Named Pipes), if they aren't already. Then click on TCP/IP and ensure that the default port is 1433.

If you have a 32bit system or version of SQL Server installed, do the same in the SQL Native Client 11.0 Configuration (32it) menu, enabling Shared Memory, TCP/IP, and Named Pipes and setting the default port to 1433.

Then click open the [SQL Server Network Configuration] or (32bit if applicable), and select the [Protocols for "YOURSERVERNAME"]. Ensure again that Shared Memory, TCP/IP, and Named Pipes are all enabled.

Then click on the [TCP/IP] Protocol Name, then select the [IP Addresses] tab at the top of the new popup window. For IP1 ensure that Active is YES; Enabled is YES (this is No by default, even if it is Active); and set the TCP Port to 1433 (tbh I don't know if you have to do this step, but I did and it worked!!); my TCP Dynamic Ports are set to 0 and I didn't change any of the IP addresses;

I did the same thing for IP10, which has IP: 127.0.0.1, which is the local machine. I also scrolled down to the bottom of the page, and set IPAll TCP Ports to 1433, (dynamic ports is 49163). Then you need to Apply all changes, close the properties window, and click on SQL Server Services in SQL Server Configuration Manager, and restart all running Servers. This should do it :D

Solution 3

Catch example:

    String url = "jdbc:sqlserver://localhost:1433/databaseName";
    String username = "user";
    String password = "pass";

Connection connection = DriverManager.getConnection(url, username, password);

1433 is default port. Dont use '\' in url.

Share:
53,461
Diya
Author by

Diya

Updated on October 21, 2020

Comments

  • Diya
    Diya over 3 years

    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"

    Code:

    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)
    

    When I change the url to

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

    I get the below error:

    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)
    

    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.

  • Diya
    Diya over 11 years
    I have changed the url string to String url = "jdbc:sqlserver://localhost/openview"; But it still gives this error: The TCP/IP connection to the host localhost/openview, port 1433 has failed. Error: "null. 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.".
  • Branislav Lazic
    Branislav Lazic over 11 years
    @Diya You forgot to add port number (1433). It should be: String url = "jdbc:sqlserver://localhost:1433/openview"
  • Diya
    Diya over 11 years
    I used your code but I still get this error: The TCP/IP connection to the host localhost/openview, port 1433 has failed. Error: "null. 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.". The instance name is "OVOPS". Should that not be specified explicitly in the url? I tried with string url final String DBPATH="jdbc:sqlserver://localhost/openview;instance=OVOPS;"‌​; in your code. But that too doesnt help :(
  • Diya
    Diya over 11 years
    When I added port number explicitly, it says "The port number 1433/openview is not valid."
  • Branislav Lazic
    Branislav Lazic over 11 years
    @Diya Sorry Diya I gave my best.
  • grachol
    grachol over 11 years
    @Diya Maybe you have wrong driver? I mean, url is wrong for this driver: url = "jdbc:microsoft:sqlserver://localhost:1433"; support.microsoft.com/kb/313100
  • Diya
    Diya over 11 years
    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;Databa‌​seName=openview;inte‌​gratedSecurity=true" and added the location of sqljdbc_auth.dll in java.library.path. It worked! Thanks again for your efforts to help me :)
  • Name is Nilay
    Name is Nilay over 11 years
    @Diya- Sorry, I did not get u...where exactly did u put "sqljdbc_auth.dll" file...??
  • Diya
    Diya over 11 years
    Add -Djava.library.path="C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\auth\x64" (which is location of sqljdbc_auth.dll) to Run configurations -> Arguments -> VM Arguments
  • Doug Morrow
    Doug Morrow over 10 years
    This question is a year old. This is not very constructive at this point. Your experience could certainly be useful on some newer questions. You might even browse around to find some good answers to help you improve your formatting.
  • Dani
    Dani over 5 years
    In my case, I realized that the SQL Server Browser service was not running