cancel
Showing results for 
Search instead for 
Did you mean: 

How to connect to My HDB Server using JDBC

Former Member
0 Kudos

Hi Experts,

I am trying to connect to my SAP HANA DB Server with following code.

package connection;

import java.sql.*;

public class ServerCon {

   public static void main(String[] argv) {

      Connection connection = null;

      try {         

          try{

              Class.forName("com.sap.db.jdbc.Driver");

          }catch (Exception e) {

              e.printStackTrace();

          }

        

         connection = DriverManager.getConnection(

            "jdbc:sap://xx.xxx.xxx.xxx:30015/","SAP PID as user name","SAP DB Password");   

      } catch (SQLException e) {

          e.printStackTrace();

         System.err.println("Connection Failed. User/Passwd Error?");

         return;

      }

      if (connection != null) {

         try {

            System.out.println("Connection to HANA successful!");

            Statement stmt = connection.createStatement();

            ResultSet resultSet = stmt.executeQuery("Select 'hello world' from dummy");

            resultSet.next();

            String hello = resultSet.getString(1);

            System.out.println(hello);

       } catch (SQLException e) {

          System.err.println("Query failed!");

       }

     }

   }

}

Using the above code i am getting the following exception:

com.sap.db.jdbc.exceptions.JDBCDriverException: SAP DBTech JDBC: Cannot connect to jdbc:sap://xx.xxx.xxx.xxx:30015/ [Cannot connect to host xx.xxx.xxx.xxx:30015 [Connection refused: connect], -813.].

    at com.sap.db.jdbc.exceptions.SQLExceptionSapDB.createException(SQLExceptionSapDB.java:230)

    at com.sap.db.jdbc.exceptions.SQLExceptionSapDB.createException(SQLExceptionSapDB.java:214)

    at com.sap.db.jdbc.exceptions.SQLExceptionSapDB.generateDatabaseException(SQLExceptionSapDB.java:197)

    at com.sap.db.jdbc.Driver.connect(Driver.java:192)

    at java.sql.DriverManager.getConnection(Unknown Source)

    at java.sql.DriverManager.getConnection(Unknown Source)

    at connection.ServerCon.main(ServerCon.java:14)

Could you please let me know what could be the issue here? i dont know i am giving correct host name and port also. I have taken host name from my eclipse as follows:

Thank you very much in advance..

Accepted Solutions (0)

Answers (1)

Answers (1)

MarkusTolksdorf
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Kishore,

Connection refused means that the host can be reached but the specified port is not accepting requests. So either host is wrong or the HANA DB is listening on a different port. If you know the instance number ##, the port is typically 3##15. So, from your example, the instance number should be 00. When connecting to the DB with HANA studio, you have used the host and the instance number. Did you use a different one there?

Best regards,

Markus