SAP ABAP ADBC和Java JDBC的使用比较-阿里云开发者社区

开发者社区> 开发者小助手-bz4> 正文

SAP ABAP ADBC和Java JDBC的使用比较

简介: 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:image.pngAnd I manually inserted two test records into it:image.pngBoth 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:image.pngAnd 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.


image.pngFurther 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


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
10017 0
使用NAT网关轻松为单台云服务器设置多个公网IP
在应用中,有时会遇到用户询问如何使单台云服务器具备多个公网IP的问题。 具体如何操作呢,有了NAT网关这个也不是难题。
26776 0
阿里云服务器ECS远程登录用户名密码查询方法
阿里云服务器ECS远程连接登录输入用户名和密码,阿里云没有默认密码,如果购买时没设置需要先重置实例密码,Windows用户名是administrator,Linux账号是root,阿小云来详细说下阿里云服务器远程登录连接用户名和密码查询方法
11587 0
windows server 2008阿里云ECS服务器安全设置
最近我们Sinesafe安全公司在为客户使用阿里云ecs服务器做安全的过程中,发现服务器基础安全性都没有做。为了为站长们提供更加有效的安全基础解决方案,我们Sinesafe将对阿里云服务器win2008 系统进行基础安全部署实战过程! 比较重要的几部分 1.
9154 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
13818 0
阿里云ECS云服务器初始化设置教程方法
阿里云ECS云服务器初始化是指将云服务器系统恢复到最初状态的过程,阿里云的服务器初始化是通过更换系统盘来实现的,是免费的,阿里云百科网分享服务器初始化教程: 服务器初始化教程方法 本文的服务器初始化是指将ECS云服务器系统恢复到最初状态,服务器中的数据也会被清空,所以初始化之前一定要先备份好。
7344 0
阿里云服务器ECS登录用户名是什么?系统不同默认账号也不同
阿里云服务器Windows系统默认用户名administrator,Linux镜像服务器用户名root
4488 0
2315
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载