Skip to Content

Connect Using the SAP HANA JDBC Driver

test
0 %
Connect Using the SAP HANA JDBC Driver
Details

Connect Using the SAP HANA JDBC Driver

October 20, 2020
Created by
March 29, 2020
Create and debug a Java application that connects to SAP HANA using the SAP HANA client.

You will learn

  • How to install Java
  • How to create and debug a Java application that queries a SAP HANA database

Prerequisites

  • You have completed the first 3 tutorials in this mission.

Java Database Connectivity (JDBC) provides an API for accessing databases from Java. An application written to the JDBC standard can be ported to other databases. Database vendors provide JDBC drivers for their database products.


Step 1: Install a JDK

Ensure that you installed a Java Development Kit (JDK) and make sure it is accessible from your path. Details on which Java versions are supported can be found in SAP note 2939501 - SAP HANA Client Supported Platforms for 2.5 and later and Oracle Java SE Support Roadmap.

To verify that it is correctly set up, run the following:

java -version
javac -version

If these commands fail, ensure that the folder they are located in, is included in your path.

Log on to answer question
Step 2: The SAP HANA JDBC driver

The SAP HANA driver for JDBC is a Multi-Release JAR file and as such supports multiple versions of Java. It is available in the client installation folder at C:\SAP\hdbclient\ngdbc.jar and in the maven repository.

  1. Run the following command for version information. If needed, adjust the path to match the installation location on your machine.

    java -jar C:\SAP\hdbclient\ngdbc.jar -v
    
    java -jar ~/sap/hdbclient/ngdbc.jar -v
    
  2. Run the command again without the -v to open the configuration window, which provides driver information and the ability to set trace information:

    JDBC-Driver-Trace-Config

    The JDBC driver has a different version number than the rest of the SAP HANA interfaces.

    The trace options are further described at JDBC Tracing and Trace Options.

  3. Run the following after replacing your_host and your_port to execute a simple query:

    java -jar C:\SAP\hdbclient\ngdbc.jar -u USER1,Password1 -n your_host:your_port -o encrypt=True -o validatecertificate=false -c "SELECT  * FROM HOTEL.CUSTOMER"
    
    java -jar ~/sap/hdbclient/ngdbc.jar -u USER1,Password1 -n your_host:your_port -o encrypt=True -o validatecertificate=false -c "SELECT  * FROM HOTEL.CUSTOMER"
    
    Link text e.g., Destination screen

See JDBC Command-Line Connection Options for additional details on parameters of ngdbc.jar.

Log on to answer question
Step 3: Create a Java application that queries SAP HANA
  1. Create a folder named java, enter the newly created directory, create a file named JavaQuery.java, and open the file in notepad.

    mkdir %HOMEPATH%\HANAClientsTutorial\java
    cd %HOMEPATH%\HANAClientsTutorial\java
    notepad JavaQuery.java
    

    Substitute pico below for your preferred text editor.

    mkdir -p $HOME/HANAClientsTutorial/java
    cd $HOME/HANAClientsTutorial/java
    pico JavaQuery.java
    
  2. Copy the following code into JavaQuery.java:

    import java.sql.*;
    public class JavaQuery {
        public static void main(String[] argv) {
            Connection connection = null;
            try {  
                connection = DriverManager.getConnection(  
                    //"jdbc:sap://10.11.123.134:39015/?encrypt=true&validateCertificate=false", "User1", "Password1");
                    //As of SAP HANA Client 2.6, connections on port 443 enable encryption by default
                    //validateCertificate should be set to false when connecting
                    //to an SAP HANA, express edition instance that uses a self-signed certificate.
    
                    //The below URL gets the host, port and credentials from the hdbuserstore.
                    "jdbc:sap://dummy_host:0/?KEY=USER1UserKey&encrypt=true&validateCertificate=false");  
            }
            catch (SQLException e) {
                System.err.println("Connection Failed:");
                System.err.println(e);
                return;
            }
            if (connection != null) {
                try {
                    System.out.println("Connection to HANA successful!");
                    Statement stmt = connection.createStatement();
                    ResultSet resultSet = stmt.executeQuery("SELECT TITLE, FIRSTNAME, NAME from HOTEL.CUSTOMER;");
                    while (resultSet.next()) {
                        String title = resultSet.getString(1);
                        String firstName = resultSet.getString(2);
                        String lastName = resultSet.getString(3);
                        System.out.println(title + " " + firstName + " " + lastName);
                    }
                }
                catch (SQLException e) {
                    System.err.println("Query failed!");
                }
            }
        }
    }
    
  3. Compile the .java file into a .class file using the following command:

    javac JavaQuery.java
    
  4. Run JavaQuery.class and indicate where the SAP HANA JDBC driver is located. Note that the host, port, UID and PWD will be retrieved from the hdbuserstore.

    java -classpath C:\SAP\hdbclient\ngdbc.jar;. JavaQuery
    
    java -classpath ~/sap/hdbclient/ngdbc.jar:. JavaQuery
    
    Java Query

See JDBC Connection Options in Java Code for additional details on the getConnection method of the DriverManager.

Log on to answer question
Step 4: Debug the application

Eclipse is a popular integrated development environment (IDE) for Java application development and provides a debugger. Download the Eclipse IDE for Java or if you wish to additionally browse the SAP HANA catalog using Data Tools, as shown in step 5, download Eclipse IDE for Enterprise Java Developers.

  1. Unzip the downloaded file and start the Eclipse IDE.

    Eclipse shows views appropriate to a certain task. For Java development, it provides a Java perspective. You may wish to change the perspective to the Java perspective via Window | Perspective | Open Perspective | Java

Java Perspective
  1. Create a new Java project named JavaQuery via the File | New | Java Project wizard.

    Add the JDBC driver as an external jar file.

    Create project
  2. Add a new Java class named JavaQuery and replace its contents with the previous code.

    Add a new class
  3. Set a breakpoint and click the debug toolbar to run the application and to step through the code.

    debugging in eclipse
Log on to answer question
Step 5: Browse SAP HANA using Eclipse Data Tools

The Eclipse IDE for Enterprise Java Developers includes a database source explorer that can be configured with a JDBC driver. The version of Eclipse can be confirmed by selecting Help | About Eclipse IDE.

Eclipse for Enterprise Java Developers

The following steps demonstrate how to configure it to enable connections to SAP HANA.

  1. Open the Data Source Explorer by choosing Window | Show View | Other | Data Management | Data Source Explorer

    Data Source Explorer
  2. Create a connection profile for SAP HANA.

    Data Source
  3. Select New Driver Definition.

    edit definition
  4. Specify where to find the SAP HANA JDBC driver.

    Data Source
  5. Specify the driver class name as com.sap.db.jdbc.Driver and the Connection URL.

    jdbc:sap://dummy_host:0/?KEY=USER1UserKey&encrypt=true&validateCertificate=false
    
    Data Source

    Validate the settings by selecting Test Connection before finishing the wizard.

  6. Browse the database catalog and execute SQL queries.

    Results from Data Source Explorer

    To execute SQL queries in eclipse, open SQL Scrapbook. SQL Scrapbook is similar to a SQL command line and allows you to query tables from inside eclipse.

    It can be opened by selecting the SQL Scrapbook icon from the right corner of the Data Source Explorer window.

    Link text e.g., Destination screen

Congratulations! You have now created and debugged a Java application that connects to and queries an SAP HANA database.

Which of the following statements are true?
×

Next Steps

Back to top