2025.4.2
学习内容:
- PreparedStatement
预编译SQL语句,防止SQL注入
使用占位符?:
String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
2.事务处理
开启事务:conn.setAutoCommit(false);
提交事务:conn.commit();
回滚事务:conn.rollback();
示例:
try {
conn.setAutoCommit(false);
// 执行多个SQL操作
conn.commit();
} catch (SQLException e) {
conn.rollback();
e.printStackTrace();
}
我的收获
掌握PreparedStatement的使用方法
学会处理数据库事务,保证数据一致性
理解SQL注入原理及防范措施

浙公网安备 33010602011771号