This article assumes that you have a basic understanding of JDBC (Java Database Connectivity). You can also compare it to Adbc (ABAP Database Connectivity) for details please refer to my blog adbc and JDBC
This article is divided into two parts, the first part of the content:
- Describes how to create a Hana DB instance on the SAP cloud Platform
- Develop a Java application and deploy it to the SAP Cloud Platform. The Java application uses JDBC to operate on the same Hana database on the SAP cloud platform.
The second part of the content:
Develop a Java application that is deployed in the On-premise system located in the corporate network. The Java app accesses the Hana instance on the SAP cloud platform via Cloud connector. My previous public article use the JAVA+SAP cloud platform +sap Cloud Connector to invoke the functions in the ABAP on-premise system to describe how applications deployed on the Internet network can access corporate The services on the On-premise system within the network, and the scenario that this article will introduce is the opposite: Java applications deployed on On-premise access the Hana database on the SAP Cloud Platform on the Internet network.
The full source code for the Java application described in this article is on my github:
Create a Hana DB instance on the SAP cloud Platform
- Log into the SAP Cloud Platform cockpit and create a new Hana DB instance:
Set the database ID and password for system user. Here I set the database ID to HANA01, which will be used later.
Once created, the DB instance state becomes started, and you can add its development tools link to the Favorites folder, which is also used later.
The Hana DB Instance on the SAP Cloud Platform is now available. The next step is to develop a Java program to access it through JDBC.
Accessing the Hana database using JDBC
Import the Java app from my GitHub to eclipse, as shown in:
The application is mainly divided into three files:
Person.java
The person model is defined, containing only three members of Id,firstname and LastName, and the corresponding getter and setter methods, known as Pojo (Plain old Java Object). The name Pojo is used to emphasize that it is an ordinary Java object that does not conform to any particular Java conventions or frameworks (such as EJBS).
Persondao.java
As the name implies: Dao-data Access Object. Connect the Hana DB instance through JDBC, create a database table named T_persons, and the logic to insert data into the table is written in this DAO class.
Persistencewithjdbcservlet.java
A simple UI implemented through a servlet that accepts user input and invokes Persondao to send input to the Hana DB instance.
The acquisition of the JDBC database DataSource instance is done through Jndi, which is passed into the DAO constructor as an input parameter, and all subsequent JDBC operations are performed through the datasource.
This DEFAULTDB configuration is located in Web. XML:
Deploy the app to the SAP Cloud Platform, with the app named Jerryjdbc.
With the configuration of Web. XML only, Java applications have no way of knowing which DB instance to operate on the SAP Cloud platform.
For this we need to bind the JERRYJDBC application with the Hana DB instance hana01 created in the first step. The binding is created by the data Source bingdings page:
Once the bindings are successful, you can insert a person record into the Hana DB instance by using a simple interface provided by the Servlet UI, Jerry Wang.
Access the DB instance on your phone to see Jerry Wang that you just inserted.
On-premise system applications connect to a DB instance on the Internet network
The Java applications and Hana database instances described in the previous article are located on the SAP Cloud platform. My previous public article use the JAVA+SAP cloud platform +sap Cloud Connector to invoke the functions in the ABAP on-premise system to describe how applications deployed on the Internet network can access corporate Service on the network, now we're trying to flip the direction of the visit: Now I'm deploying Java applications in the corporate network, such as a server in the SAP Chengdu Research Lab, to connect the Java application to the Hana DB instance on the SAP cloud platform.
Again, it is necessary to use the cloud Connector again.
Configure Cloud Connector to complete the connection from the On-premise system to the cloud
Create another Hana db instance, named Jerrydemo, on the SAP cloud platform. Then sign in to Cloud Connector, click on the label On-premise to Cloud and create a new service Channels:
Assign the newly created DB instance Jerrydemo to this new channel:
Create success as. Note the generated port number 32215 is used later.
Modify the configuration of the Java server on the on-premise system to point to the DB instance of the SAP cloud Platform
The code for the Java application does not need to be adjusted, only the server configuration needs to be changed. Modify the file connection.properties below the Servers folder to point to the server's database connection to the service Channel just configured on the Cloud connector via the cloud Connector is connected to the Hana database on the SAP Cloud Platform as a bridge.
Javax.persistence.jdbc.url: The localhost:32215 that is pointed to is the service Channel configured on the Cloud connector, The channel I assigned the Hana DB Instance ID is jerrydemo. Currentschema=system, which means that the database tables I created through JDBC are assigned to the SYSTEM schema.
Javax.persistenc.jdbc.user/password: Fill in Jerrydemo the access username and password for this DB instance.
All configurations are complete at this point.
Start the localhost server on the on-premise system and insert two data:
Open the Hana development tool Connection on the SAP Cloud Platform and observe the two records inserted by the Web app just running on the on-premise system from the system schema. Demonstrates the successful attempt to write to the SAP Cloud Platform database from the On-premise system.
To get more original Jerry's technical articles, please follow the public number "Wang Zixi" or scan the QR code below:
Use JDBC to operate the Hana database on the SAP cloud Platform