Connect to SAP HANA via JDBC

SAP HANA provides a driver that enables Java applications to connect to the SAP HANA database with the JDBC application programming interface (API).


  1. Install the JDBC driver.
    The driver (ngdbc.jar) is installed as part of the SAP HANA client installation and by default is located at:
    • C:\Program Files\sap\hdbclient\ on Microsoft Windows platforms
    • /usr/sap/hdbclient/ on Linux and UNIX platforms
  2. Add ngdbc.jar to your classpath.
  3. Write Java code to create a connection to the database and execute SQL commands. Use a connection string in the form of jdbc:sap://<server>:<port>[/?<options>]. For example:
    Specify one or more failover servers by adding additional hosts, as in the following example:

    To connect to a specific database, for example tdb1, use the databaseName parameter, as illustrated in the following code:



The following is an example of connecting to an SAP HANA server called myhdb, which was installed as instance 07, with user name myName and password mySecret. Make sure to change these for your system, and add the JDBC driver (ngdbc.jar) to your classpath.

import java.sql.*;
public class jdemo {
   public static void main(String[] argv) {
      Connection connection = null;
      try {                  
         connection = DriverManager.getConnection(
            "jdbc:sap://myhdb:30715/?autocommit=false", "myName", "mySecret");                  
      } catch (SQLException e) {
         System.err.println("Connection Failed:");
      if (connection != null) {
         try {
            System.out.println("Connection to HANA successful!");
            Statement stmt = connection.createStatement();
            ResultSet resultSet = stmt.executeQuery("Select 'hello world' from dummy");
            String hello = resultSet.getString(1);
       } catch (SQLException e) {
          System.err.println("Query failed!");