2025.4.2

学习内容:

  1. 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注入原理及防范措施
posted @ 2025-04-02 22:52  被迫敲代码  阅读(2)  评论(0)    收藏  举报