PreparedStatement

    PreparedStatement 表示预编译的SQL语句对象。SQL语句被预编译并且存储在PreparedStatement中,然后可以使用此对象多次高效执行该SQL。

    实例:

// 1.获取 OADBTransaction对象
OAApplicationModule am = (OAApplicationModule)pageContext.getApplicationModule(webBean); // 获取AM
OADBTransaction txn = am.getOADBTransaction(); // 获取OADBTransaction 对象 ,其继承 DBTransaction
PreparedStatement ps = null;
ResultSet rs = null;

// 2. 创建 PreparedStatement 对象
ps = txn.createPreparedStatement(String str, int noRowsPrefetch);
// str表示预编译的SQL语句,noRowsPrefetch 表示执行该SQL时默认获取的行数,可以是DBTransaction.DEFAULT
eg: ps = txn.createPreparedStatement(sqlStr, 1);

// 3.设置SQL中使用的参数的值
ps.setLong(index,param); // ps.setShort(index,param); 以及 setInt,setString的方法为SQL设置参数的值,index为参数的顺序,param为参数的值
eg: ps.setLong(1, Long.parseLong(this.headerId.toString())); // 设置第一个参数的值

// 4.执行查询并返回结果
rs = ps.executeQuery(); // 执行查询并将结果返回到ResultSet结果集中
while(rs.next()){
String tmp = rs.getString(1); // rs.getInt等方法
}
原文地址:https://www.cnblogs.com/chenyongjun/p/3533609.html