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:
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:
jdbc:sap://myServer:30015/?autocommit=false
Specify one or more failover servers by adding additional hosts, as in the following example:
jdbc:sap://myServer:30015;failover1:30015;failover2:30015/?autocommit=false
To connect to a specific database, for example tdb1, use the databaseName parameter, as illustrated in the following code:
jdbc:sap://localhost:30013/?databaseName=tdb1&user=SYSTEM&password=manager
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:");
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 'hello world' from dummy");
resultSet.next();
String hello = resultSet.getString(1);
System.out.println(hello);
} catch (SQLException e) {
System.err.println("Query failed!");
}
}
}
}
◈ Procedure
1. Install the JDBC driver.
- C:\Program Files\sap\hdbclient\ on Microsoft Windows platforms
- /usr/sap/hdbclient/ on Linux and UNIX platforms
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:
jdbc:sap://myServer:30015/?autocommit=false
Specify one or more failover servers by adding additional hosts, as in the following example:
jdbc:sap://myServer:30015;failover1:30015;failover2:30015/?autocommit=false
To connect to a specific database, for example tdb1, use the databaseName parameter, as illustrated in the following code:
jdbc:sap://localhost:30013/?databaseName=tdb1&user=SYSTEM&password=manager
◈ Example
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:");
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 'hello world' from dummy");
resultSet.next();
String hello = resultSet.getString(1);
System.out.println(hello);
} catch (SQLException e) {
System.err.println("Query failed!");
}
}
}
}
Grate post....
ReplyDeleteTeradata training
Windows admin training
Wordpress training
Qliksense training
Good post ....
ReplyDeleteSCCM training
SCOM training
SSAS training
SSIS training
SSRS training
IntelliMindz is the best IT Training in Coimbatore with placement, offering 200 and more software courses with 100% Placement Assistance.
ReplyDeletesap hana trainingIn Coimbatore
selenium Course In Coimbatore
seo training In Coimbatore
angular training In Coimbatore
aws training In Coimbatore