写点什么

使用 ABAP(ADBC) 和 Java(JDBC) 连接 SAP HANA 数据库

用户头像
Jerry Wang
关注
发布于: 2021 年 06 月 05 日
使用ABAP(ADBC)和Java(JDBC)连接SAP HANA数据库

在表 DBCON 里维护一条记录,指向 HANA 数据库。con_ENV 里填入 HANA 数据库的主机名和端口号。如 vmXXXX:30015



DATA: ls_new TYPE DBCON.
ls_new-con_name = 'HAN'.ls_new-dbms = 'HDB'.ls_new-user_name = 'system'.ls_new-password = 'Sap12345'.ls_new-con_env = 'vml2018:30015'.

CALL FUNCTION 'DBCON_INSERT' EXPORTING DBCON_WORKAREA = ls_new EXCEPTIONS DBCON_KEY_EXISTS = 1 SECURE_STORE_KEY_EXISTS = 2 SECURE_STORE_OTHER = 3 OTHERS = 4 .IF SY-SUBRC = 0. WRITE:/ 'Insert Successfully'.ENDIF.然后使用ABAP代码创建一个新的table,插入三条entry进行,再读出来,最后删除table.TYPES: BEGIN OF result_t, key TYPE i, value TYPE string, END OF result_t.

DATA: connection TYPE dbcon-con_name VALUE 'HAN', stmt_ref TYPE REF TO cl_sql_statement, cx_sql_exception TYPE REF TO cx_sql_exception, lv_text TYPE string, res_ref TYPE REF TO cl_sql_result_set, d_ref TYPE REF TO DATA, result_tab TYPE TABLE OF result_t, result_line TYPE result_t, row_cnt TYPE i, con_ref TYPE REF TO cl_sql_connection.
con_ref = cl_sql_connection=>get_connection( connection ).stmt_ref = con_ref->create_statement( ).
TRY.
stmt_ref->execute_ddl( 'CREATE TABLE I042416_TESTPROC_TAB( key INT PRIMARY KEY, value NVARCHAR(255) )' ). stmt_ref->execute_update( 'INSERT INTO I042416_TESTPROC_TAB VALUES(1, ''First value'' )' ). stmt_ref->execute_update( 'INSERT INTO I042416_TESTPROC_TAB VALUES(2, ''Second value'' )' ). stmt_ref->execute_update( 'INSERT INTO I042416_TESTPROC_TAB VALUES(3, ''Third value'' )' ). res_ref = stmt_ref->execute_query( 'SELECT * FROM I042416_TESTPROC_TAB' ). GET REFERENCE OF result_tab INTO d_ref. res_ref->set_param_table( d_ref ). row_cnt = res_ref->next_package( ). stmt_ref->execute_ddl( 'DROP TABLE I042416_TESTPROC_TAB' ).CATCH cx_sql_exception INTO cx_sql_exception. lv_text = cx_sql_exception->get_text( ). WRITE:/ 'Error:' , lv_text.ENDTRY.
LOOP AT result_tab INTO result_line. WRITE:/ 'Key:' , result_line-key, ' Value:', result_line-value.ENDLOOP.
复制代码


Java 实现类似:



public static void main(String[] args) { try { Class.forName("com.sap.db.jdbc.Driver"); System.out.println("Create a connection..."); con = (com.sap.db.jdbc.trace.Connection) DriverManager .getConnection("jdbc:sap://<HANA数据库主机名>:30015",// , "system", "此处填入登录密码"); cs = (com.sap.db.jdbc.trace.CallableStatement) con.prepareCall("call I042416_two_par(NULL, NULL)"); cs.execute(); int index = 0; do { int rowCount = 0; com.sap.db.jdbc.trace.ResultSet rs = (com.sap.db.jdbc.trace.ResultSet)cs.getResultSet(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); while (rs != null && rs.next()) { for (int i = 1; i <= columnCount; i++) { System.out.print(rs.getString(i) + " "); if (i == columnCount) { System.out.println(); } } System.out.println("Current Result Set Index:" + index + " Row Count: " + rowCount++); } System.out.println("Result Set Index: " + index++); } while(cs.getMoreResults()); }
复制代码


console 打印结果:数据能够成功通过 JDBC 从 HANA 数据库读取出来。



要获取更多 Jerry 的原创技术文章,请关注公众号"汪子熙":



发布于: 2021 年 06 月 05 日阅读数: 8
用户头像

Jerry Wang

关注

个人微信公众号:汪子熙 2017.12.03 加入

SAP成都研究院开发专家,SAP社区导师,SAP中国技术大使。

评论

发布
暂无评论
使用ABAP(ADBC)和Java(JDBC)连接SAP HANA数据库