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
- mysql benchmark基准测试
- redis 学习笔记(6)-cluster集群搭建
- redis 学习笔记(6)-cluster集群搭建
- dubbox REST服务使用fastjson替换jackson
- JavaScript中removeEventListener()使用注意事项
- dubbox REST服务使用fastjson替换jackson
- struts2(二)之配置文件详解与结果视图
- CSS魔法堂:你真的懂text-align吗?
- 黑客可以利用传感器数据来破解手机密码
- spring-boot 速成(3) actuator
- 利用sharding-jdbc分库分表
- 利用sharding-jdbc分库分表
- 协议森林17 我和你的悄悄话 (SSL/TLS协议)
- spring-boot 速成(1) helloworld
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- pytorch随机采样操作SubsetRandomSampler()
- Pytorch上下采样函数–interpolate用法
- scrapy框架携带cookie访问淘宝购物车功能的实现代码
- 浅析Python __name__ 是什么
- PHP判断访客是否手机端(移动端浏览器)访问的方法总结【4种方法】
- PHP后期静态绑定实例浅析
- php+mysql开发中的经验与常识小结
- PHP时间戳和日期相互转换操作实例小结
- php json转换相关知识(小结)
- PHP单例模式应用示例【多次连接数据库只实例化一次】
- 踩坑:pytorch中eval模式下结果远差于train模式介绍
- Python读取二进制文件代码方法解析
- Python3 webservice接口测试代码详解
- php往mysql中批量插入数据实例教程
- PHP类与对象后期静态绑定操作实例详解