Applies to:
SAP Process Integration 7.0 and Above Versions. Database handling using JDBC API in SAP PI graphical maps.
Summary:
In many business scenarios we like to insert/ update or delete the database record of a particular database. We can achieve this in various ways, this article demonstrates how to access database records and execute DDL and DML statements from SAP PI Mapping level. This article demonstrate the code and it’s functionality for SQL Select statement but the code can be extended and modified with other SQL statement as well.
Introduction:
JDBC stands for "Java DataBase Connectivity". It is an API (Application Programming Interface) which consists of a set of Java classes, interfaces and exceptions and a specification to which both JDBC driver vendors and JDBC developers adhere when developing applications which are platform and database independent applications in java. JDBC is a very popular data access standard. RDBMS (Relational Database Management Systems) or third-party vendors develop drivers which adhere to the JDBC specification. Other developers use these drivers to develop applications which access those databases.
SUN (Currently Oracle) prepares and maintains the JDBC specification. Since JDBC is just a specification (suggestions for writing and using JDBC drivers), third-party vendors develop JDBC drivers adhering to this specification. JDBC developers then use these drivers to access data sources.
Prerequisite:
Knowledge of SAP Process Integration 7.0 and/or above, JDBC API knowledge, knowledge of Graphical Mapping in SAP PI.
What is JDBC and its scope within Java:
JDBC provides a standard library for accessing relational databases
JDBC API standardizes
• Way to establish connection to database
• Approach to initiating queries
• Method to create stored (parameterized) queries
• The data structure of query result (table)
– Determining the number of columns
– Looking up metadata, etc.
– API does not standardize SQL syntax
• JDBC is not embedded SQL
– JDBC class located in
java.sql package
• Note: JDBC is not officially an acronym; unofficially,
“Java Database Connectivity” is commonly used
Most important members, such as Connection, Statement, and ResultSet, are interfaces instead of being classes.
This is because; the point of JDBC is to hide the specifics of accessing a particular database.
The implementation of the underlying classes is done in the vendor provided driver and associated classes
What are the JDBC drivers and how it is used:
A JDBC driver is a software component enabling a Java application to interact with a database.
JDBC Drivers are set of classes that enables the Java application to communicate with databases. Java.sql that ships with JDK contains various classes for using relational databases. But these classes do not provide any implementation, only the behaviors are defined. The actual implementations are done in third-party drivers. Third party vendors implement the java.sql.Driver interface in their database driver.
There are 4 types of JDBC drivers. Commonest and most efficient of which are type 4 drivers. Here is the description of each of them:
SUN encourages developing and using type 4 drivers in applications.
Overview of JDBC Architecture and API:
The JDBC API is available in the java.sql and javax.sql packages. Following are important JDBC classes, interfaces and exceptions in the java.sql package:
javax.sql is part of J2SE 1.4 and J2EE 1.3. It adds following features to JDBC in addition to the ones provided by java.sql package:
DriverManager
to efficiently obtain data source connections (possibly using hidden connection pooling).Basics of JDBC and creating Statements:
Seven Basic Steps in Using JDBC
Basic Code Snippets are given below:
try {
} catch { ClassNotFoundException cnfe) {
}
String host = "dbhost.yourcompany.com";
String dbName = "someName";
int port = 1234;
String oracleURL = "jdbc:oracle:thin:@" + host +
":" + port + ":" + dbName;
String mysqlURL = "jdbc:mysql://" + host
+
":" + port + "/" + dbName
String username = "jay_debesee";
String password = "secret";
Connection connection =
username,
password);
Statement statement = connection.createStatement();
String query = "SELECT col1, col2, col3 FROM sometable";
ResultSet resultSet = statement.executeQuery(query);
–
To modify the database, use executeUpdate , supplying a string that uses UPDATE , INSERT , or DELETE
–
Use setQueryTimeout to specify a maximum delay to wait for results
while(resultSet.next()) {
}
–
First column has index 1, not 0
– ResultSet provides various getXxx methods that take a column index or name and returns the data
–As opening a connection is expensive, postpone this step if additional database operations are expected
Step by Step approach and Java Code in Detail:
In SAP PI Graphical Mapping we can access the database and manipulate the records of that using standard JDBC API. The following attached Java code is used in this case to retrieve the record based on a select statement and populating the same value in the target field.
UDF Code:
try
{ AbstractTrace trace = container.getTrace();
String connectionURL ="jdbc:oracle:thin:@//msp06db4.pace.mdtx.com:1521/pfwgdw.pace.mdtx.com";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
Class.forName("oracle.jdbc.OracleDriver").newInstance();
connection = DriverManager.getConnection(connectionURL,"userId","password");
trace.addInfo("Connected");
statement = connection.createStatement();
String Query="SELECT A2.COMPONENTSERIALNO AS BATTERY_SERIALNUMBER FROM FW.FWLOT F1, FW.ABCTBL A2 WHERE A2.FACILITY = F1.FACILITY AND A2.TOPLEVELLOTID = F1.LOTID AND SERIALNUMBER='";
Query= Query +SRL_NO[0]+"'AND A2.LOCATION IN ('Battery','PowerSource') AND A2.DELETEDLOT='NO'";
//String Query= "SELECT LOTID FROM FW.FWLOT F1 WHERE PRODUCTNAME='042193004'";
rs = statement.executeQuery(Query);
while (rs.next()) {
trace.addInfo(rs.getString(1));
result.addValue(rs.getString(1));
}
rs.close();
statement.close();
connection.close();
}
catch (SQLException e)
{
result.addValue(e.getMessage());
}
catch(Exception e)
{
result.addValue(e.getMessage());
}
The above code takes one input and fetches the record from the database and populates the value to the target field.
Testing the Scenario:
Following are the two structures for sender and receiver in mapping-
The UDF is used in between the below mentioned field-
The UDF takes one input as Serial number which is the value of SERIAL_NO source field and using this value it is calling the database and populating the target field BATSRL.
Output on execution in test tab-
Output checked from TOAD SQL Navigator-
Restriction:
Though using this approach is very simple and easy but still it is having some restrictions as below-
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
11 | |
7 | |
5 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 |