创建statment主在循环内外对比




class DemoParse {
public static void main(String args[]) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
PreparedStatement pstmt1 = null;
ResultSet rset = null;
ResultSet rset1 = null;
// first parameter: database name
try {
// get connection - auto commit is off
conn = (Connection) JDBCUtil
.getConnection("fuyou", "fuyou", "orcl");
JDBCUtil.startTrace(conn);
JDBCUtil.getTraceName(conn);
String stmtString = "select /*+ prepareStatement() within loop */ empno, job from emp where ename = ?";
for (int i = 0; i < 5; i++) {
pstmt = conn.prepareStatement(stmtString);
pstmt.setString(1, "SCOTT");
rset = pstmt.executeQuery();
while (rset.next()) {
}
}
stmtString = "select /*+ prepareStatement() outside loop */ empno, job from emp where ename = ?";
pstmt1 = conn.prepareStatement(stmtString);
for (int i = 0; i < 5; i++) {
pstmt1.setString(1, "SCOTT");
rset1 = pstmt1.executeQuery();
while (rset1.next()) {
}
}
} finally {
// release resources associated with JDBC in the finally clause.
JDBCUtil.close(rset);
JDBCUtil.close(pstmt);
JDBCUtil.close(rset1);
JDBCUtil.close(pstmt1);
JDBCUtil.close(conn);
}
}
}




利于tkprof 查看结果 :


select /*+ prepareStatement() within loop */ empno, job
from
emp where ename = :1


call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 5 0.01 0.00 0 0 0 0
Execute 5 0.00 0.00 0 0 0 0
Fetch 5 0.00 0.00 0 35 0 5
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 15 0.01 0.00 0 35 0 5

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 62

Rows Row Source Operation
------- ---------------------------------------------------
1 TABLE ACCESS FULL EMP (cr=7 pr=0 pw=0 time=62 us)


Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 10 0.00 0.00
SQL*Net message from client 10 0.01 0.01
********************************************************************************

select /*+ prepareStatement() outside loop */ empno, job
from
emp where ename = :1


call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 5 0.00 0.00 0 0 0 0
Fetch 5 0.00 0.00 0 35 0 5
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 11 0.00 0.00 0 35 0 5

Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 62

Rows Row Source Operation
------- ---------------------------------------------------
5 TABLE ACCESS FULL EMP (cr=35 pr=0 pw=0 time=189 us)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值