How to to use Database.executePreparedStmtByParamList()

How to to use Database.executePreparedStmtByParamList()

book

Article ID: KB0071131

calendar_today

Updated On:

Products

TIBCO BusinessEvents Enterprise Edition

Description

How to use Database.executePreparedStmtByParamList() to execute a PreparedStatement?

Issue/Introduction

This article explains and attach an example showing how to implement Database.executePreparedStmtByParamList() for DML statements in BE.

Environment

All

Resolution

The Database.executePreparedStmtByParamList() method executes a PreparedStatement. A prepared statement is a SQL statement where values are determined at runtime. In this case, the values to be used are passed as a List of parameters.

Signature:

int executePreparedStmtByParamList(String preparedStmt, List parameterList)

To build the List of parameters creat a List that has been populated using preparedstatement utility methods only, these are:
  1. Database.Util.addIntParam. For integers.
  2. Database.Util.addStringParam. For strings.
  3. Database.Util.addDoubleParam. For doubles.
  4. Database.Util.addParam. For any other type of parameter, it is going to be set as Object.
Below you'll find the steps and example to use "executePreparedStmtByParamList()".

1.- Create the statement replacing the values with "?".
String stm="insert into D_Person(name, last_name, age, idCard, salary, ID$) VALUES(?, ?, ?, ?, ?, ?)";
2.- Create the List of parameters.
Object paramList = Query.Util.newList();
3.- If there is some data type different from the int, string, or double you should add the value to an Object.
//This is special object for Database.Util.addParam in DB is boolean
Object hasIdC = true;
4.- Add the param values in order using the corresponding type.
Database.Util.addStringParam(paramList, "Test");
Database.Util.addStringParam(paramList, "red");
Database.Util.addIntParam(paramList, 25);			
Database.Util.addParam(paramList, hasIdC);
Database.Util.addDoubleParam(paramList, 2.25);
Database.Util.addIntParam(paramList, 2);
5.- Execute the query operation (in this case an insert).
int cnt_prepare_insert=Database.executePreparedStmtByParamList(stm,paramList);

The example is attached to the article and is built on BE 6.2.1. You'll find all the necessary archives to set the DB in the ZIP file. Before running the example rebuild the EAR.
 

Additional Information

Database.executePreparedStmtByParamList():

https://docs.tibco.com/pub/businessevents-enterprise/6.2.2/doc/html/functions/RDBMS/Database/executePreparedStmtByParamList.html

Database.Util.addParam():

https://docs.tibco.com/pub/businessevents-enterprise/6.2.2/doc/html/functions/RDBMS/Database/Util/addParam.html

Attachments

How to to use Database.executePreparedStmtByParamList() get_app