📄 preparedstatementtest.java
字号:
package net.betterjava.performance.database;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import net.betterjava.performance.StopWatch;
public class PreparedStatementTest extends Object {
private static final int REC_NUMBER = 1000;
public PreparedStatementTest() {
}
public static void main(String[] args) {
PreparedStatementTest preparedStatementTest = new PreparedStatementTest();
preparedStatementTest.test();
}
void test() {
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt = null;
StopWatch stopWatch = new StopWatch();
int i = 0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn =
DriverManager.getConnection(
"jdbc:oracle:thin:@xiefi:1521:xiefi",
"perf",
"perf");
stmt = conn.createStatement();
stopWatch.start();
for (i = 0; i < REC_NUMBER; i++) {
stmt.execute(
"insert into test_table(id,name,description) values("
+ i
+ ",'test','a small test')");
}
stmt.close();
stopWatch.stop();
System.out.println("Time elapsed of Statement:" + stopWatch.getTimeElapsed());
pstmt =
conn.prepareStatement(
"insert into test_table(id,name,description) values(?,?,?)");
stopWatch.start();
for (i = 0; i < REC_NUMBER; i++) {
pstmt.setInt(1, REC_NUMBER + i);
pstmt.setString(2, "test");
pstmt.setString(3, "a small test");
pstmt.execute();
}
pstmt.close();
stopWatch.stop();
System.out.println(
"Time elapsed of PreparedStatement:" + stopWatch.getTimeElapsed());
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
stmt.close();
pstmt.close();
conn.close();
} catch (Exception e) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -