SAP ABAP ADBC和Java JDBC的使用比較
Horst Keller has already introduced ADBC in his blog ABAP Geek 15 – ADBC long times ago. And recently during my self study on PostgreSQL I made some practice to connect PostgreSQL in Java programming using Java Database Connectivity – JDBC. In fact I found out that there are lots of commonality between these two technologies.
There is a demo program demo_adbc_query mentioned in SAP help to demonstrate the use of ADBC.
I make some changes on it in order to perform a line-by-line comparison with JDBC. The source code of adapted program:
REPORT zjerry_adbc.CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
PRIVATE SECTION.
CLASS-DATA: BEGIN OF result_line,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
fldate TYPE sflight-fldate,
END OF result_line,
result_tab LIKE TABLE OF result_line.ENDCLASS.CLASS demo IMPLEMENTATION.
METHOD main.
DATA: carrid TYPE sflight-carrid VALUE 'AA',
cols TYPE adbc_column_tab,
lv_carrid TYPE string,
con_ref TYPE REF TO cl_sql_connection,
con_name TYPE dbcon-con_name VALUE 'DEFAULT'.
cols = VALUE #( ( CONV adbc_name( 'CARRID' ) )
( CONV adbc_name( 'CONNID' ) )
( CONV adbc_name( 'FLDATE' ) ) ).
lv_carrid = cl_abap_dyn_prg=>quote( to_upper( carrid ) ).
TRY.
con_ref = cl_sql_connection=>get_connection( con_name ).
DATA(statement) = con_ref->create_statement( ).
DATA(lv_query) = `SELECT carrid, connid, fldate ` &&
`FROM sflight ` &&
`WHERE mandt = ` && `'` && sy-mandt && `' AND` &&
` carrid = ` && lv_carrid.
DATA(result) = statement->execute_query( lv_query ).
result->set_param_table( itab_ref = REF #( result_tab )
corresponding_fields = cols ).
IF result->next_package( ) > 0.
SORT result_tab BY carrid connid fldate.
WRITE:/ 'Number of lines found: ', lines( result_tab ).
ENDIF.
con_ref->close( ).
CATCH cx_sql_exception INTO DATA(err).
ENDTRY.
ENDMETHOD.ENDCLASS.START-OF-SELECTION.
demo=>main( ).
And now have a look at how JDBC can achieve the same.
I have replicated an ABAP table COMM_PRODUCT to my local PostgreSQL server:
And I manually inserted two test records into it:
Both is the source code implemented in Java to perform a query against this table and display result:
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Timestamp;public class PostgreSQLJDBC {
private Connection connection = null;
private void select() {
try {
int index = 0;
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:9812/zproduct", "postgres", "XXXXXX");
Statement stmt = connection.createStatement();
String query = "SELECT * FROM public.comm_product;";
ResultSet rs = stmt.executeQuery(query);
while ( rs.next() ) {
System.out.println("Row index: " + index++);
String client = rs.getString("client");
System.out.println("Client: " + client);
String guid = rs.getString("product_guid");
System.out.println("Product guid: " + guid);
Timestamp validFrom = rs.getTimestamp("valid_from");
System.out.println("Valid from: " + validFrom);
}
rs.close();
stmt.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
PostgreSQLJDBC jdbcTest = new PostgreSQLJDBC();
jdbcTest.select();
}}
Output in console:
And I mark the corresponding part in both language which has the same semantic meaning with same color ( although grammar is different ).
Through this comparison we can know that both connectivity technology follow the same idea.
Further reading
I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:
- Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP
- Functional programming – Simulate Curry in ABAP
- Functional Programming – Try Reduce in JavaScript and in ABAP
- Simulate Mockito in ABAP
- A simulation of Java Spring dependency injection annotation @Inject in ABAP
- Singleton bypass – ABAP and Java
- Weak reference in ABAP and Java
- Fibonacci Sequence in ES5, ES6 and ABAP
- Java byte code and ABAP Load
- How to write a correct program rejected by compiler: Exception handling in Java and in ABAP
- An small example to learn Garbage collection in Java and in ABAP
- String Template in ABAP, ES6, Angular and React
- Try to access static private attribute via ABAP RTTI and Java Reflection
- Local class in ABAP, Java and JavaScript
- Integer in ABAP, Java and JavaScript
- Covariance in Java and simulation in ABAP
- Various Proxy Design Pattern implementation variants in Java and ABAP
- Tag(Marker) Interface in ABAP and Java
- Bitwise operation ( OR, AND, XOR ) on ABAP Integer
- ABAP ICF handler and Java Servlet
- ADBC and JDBC
- CL_ABAP_CORRESPONDING, CL_JAVA_CORRESPONDING and CL_JS_CORRESPONDING
- Build an Cross Site Scripting example in Java and ABAP
- Play around with JSONP in nodeJS server and ABAP server
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2723719/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用ABAP(ADBC)和Java(JDBC)連線SAP HANA資料庫JavaJDBC資料庫
- ABAP SICF服務和Java Servlet的比較JavaServlet
- Java的位元組碼和ABAP load的比較Java
- ABAP OPEN SQL裡OPEN CURSOR和SELECT的比較SQL
- Java和JavaSciprt比較Java
- SAP Hybris Commerce的JSP tag和SAP BSP tag的比較JS
- SAP ABAP和Java的動態代理實現Java
- 關於Spring的JDBC連線mysql(與傳統jdbc比較)SpringJDBCMySql
- Dalvik 和 Java 位元組碼的比較Java
- Java™ 教程(比較字串和字串的部分)Java字串
- Java 中 Comparable 和 Comparator 比較Java
- EXCEL,POI,EASYEXCEL的使用和比較Excel
- ABAP的include關鍵字,Java的import, C的include和C4C ABSL 的import比較JavaImport
- SAP UI5 Gateway Export 和 Client Export 的比較UIGatewayExportclient
- Java,Go和Rust之間的比較 - DexterJavaGoRust
- Spring Boot中如何使用JDBC讀取和寫入資料,JDBC和JPA的對比,JdbcTemplate和SimpleJdbcInsert的用法對比Spring BootJDBC
- SAP UI5和React的頁面渲染效能比較UIReact
- SAP CRM,Cloud for Customer和Fiori應用的direct navigation比較CloudNavigation
- Java的Covariance設計原理和SAP ABAP的模擬實現Java
- Java的BigDecimal比較大小JavaDecimal
- js 深比較和淺比較JS
- Java 比較器Java
- JAVA中的Comparable介面和自定義比較器Java
- Java中的字串操作(比較String,StringBuiler和StringBuffer)Java字串UI
- JAVA中字串比較equals()和equalsIgnoreCase()的區別Java字串
- SAP ABAP Application Log 的使用方法APP
- SAP ABAP SQL的execution plan和cacheSQL
- SAP CRM和Cloud for Customer裡的Formatted Text控制元件的比較CloudORM控制元件
- ABAP Netweaver, SAP Cloud Platform和Kubernetes的使用者區分CloudPlatform
- 在SAP ABAP裡使用註解@Inject模擬Java SpringJavaSpring
- Go和Python比較的話,哪個比較好?GoPython
- 淺談Java和SAP ABAP的靜態代理和動態代理,以及ABAP面向切面程式設計的嘗試Java程式設計
- ABAP和Java的destination和JNDIJava
- Java動態代理 jdk和cglib的實現比較JavaJDKCGLib
- Hybris service layer和SAP CRM WebClient UI架構的橫向比較WebclientUI架構
- SAP Hybris的build callback和SAP ABAP的SGEN事務碼UI
- ImageMagic 和 GraphicsMagick 的比較
- ArrayList和LinkedList的比較