Statement和PreparedStatement的使用区别

相同点:

都具有方法:

executeQuery(用于查询)

execute(用于创建表)

executeUpdate(用于插入数据)


使用中的不同点:

Statement state = conn.createStatement();
String sql = "SELECT * FROM student";
ResultSet rs = state.executeQuery(sql);



private static final String INSERT = "INSERT INTO userinfo("

+ "id,name,password,age,sex,email"
+ ")VALUES("
+ "sys_guid(),?,?,?,?,?"

+ ")";

public boolean save(List userInfos) {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);//禁止自动提交
PreparedStatement state =
conn.prepareStatement(INSERT);
for (int i = 0; i < 50; i++) {
state.setString(1, "test" + i);
state.setString(2, "12345" + i);
state.setInt(3, 22);
state.setString(4, "1");
state.setString(5, "test" + i + "@mail.com");
}

} catch (Exception e) {
// TODO: handle exception
} finally {

}
return false;
}

你可能感兴趣的:(Statement和PreparedStatement的使用区别)