Trouble connecting to a remote HANA database via JDBC

19,046

Solution 1

I was using the incorrect SAP jar. I was using sapdbc.jar when I needed the HANA client jar (ngdbc.jar). It all connected after that jar and driver switch.

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

    String url = "jdbc:sap://xx.x.x.xxx:30015/?databaseName=DBNAME";
    String user = "user";
    String password = "password";

    Connection cn = java.sql.DriverManager.getConnection(url, user, password);

    ResultSet rs = cn.createStatement().executeQuery("CALL MY_SCHEMA.STORED_PROC");

    // ... do whatever with the results ...

} catch (Exception e) {
    e.printStackTrace();
}

Solution 2

Yes you are right you need to use the ngdbc.jar to make the connection.

The instance number is included in the port no. that you are connecting to.

e.g if your port no. is 30015, then 00 is the instance.

Hope this helps.

Share:
19,046
Davidson
Author by

Davidson

Updated on July 21, 2022

Comments

  • Davidson
    Davidson almost 2 years

    I'm running a small JAVA program from my laptop trying to connect via JDBC to our HANA server for a "Can we?" prototype.

    I understand it's possible to connect via JDBC connection to a remote HANA server. However, I cannot. Here's the methodology I'm using from the JAVA using the sapdbc.jar file. I'm just testing a connection here.

        DataSourceSapDB ds = new DataSourceSapDB();
        ds.setServerName("10.x.x.xxx");
        ds.setPort(30015);
        ds.setDatabaseName("dbNAME");
        ds.setUser("myUser");
        ds.setPassword("myPassword");
        Connection c = ds.getConnection();
    
        if (c == null) return;  
    

    The instance is 00 but don't see where to include it in the connect string, if it is required. I've double-checked all the properties.

    Our HANA server is hosted by another company though access to it is inside our network. Could this be a reason?

    Thanks for any assinstance.

    The connection error I get is:

    com.sap.dbtech.jdbc.exceptions.JDBCDriverException: SAP DBTech JDBC: Cannot connect to jdbc:sapdb://10.x.x.xxxx:30015/dbNAME [Connect reply receive failed [Connection reset].].
        at com.sap.dbtech.jdbc.DriverSapDB.connect(DriverSapDB.java:178)
        at com.sap.dbtech.jdbcext.DataSourceSapDBBase.openPhysicalConnection(DataSourceSapDBBase.java:374)
        at com.sap.dbtech.jdbcext.DataSourceSapDB.getConnection(DataSourceSapDB.java:49)
        at com.glazers.hana.utils.HanaStoredProcedure.execute(HanaStoredProcedure.java:37)
        at com.glazers.hana.utils.HanaStoredProcedure.main(HanaStoredProcedure.java:24)
    
  • Shah Muzaffar
    Shah Muzaffar about 6 years
    where from can we get that ngdbc.jar other. I know we can get it from SAP client installation, but is there any other source to get it
  • Amresh
    Amresh almost 5 years
    @ShahMuzaffar You can convert your project to Maven and install below dependency to pom file. <!-- mvnrepository.com/artifact/com.sap.cloud.db.jdbc/ngdbc --> <dependency> <groupId>com.sap.cloud.db.jdbc</groupId> <artifactId>ngdbc</artifactId> <version>2.4.51</version> <type>jar</type> </dependency> Or you can directly download the jar files from maven.