用PreparedStatement实现对数据表的增删改操作

/**
 * @program: 用PreparedStatement实现对数据表的增删改操作
 * @description:
 * @author: Mr.Fan
 * @create: 2021-05-29 10:27
 **/
public class PreparedStatementUpdateTest {
    public void update(String sql, Object ...args) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //1.获取数据库连接
            conn = JDBCUtils.getConnection();
            //2.预编译sql语句
            ps = conn.prepareStatement(sql);
            //3.填充占位符
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            //4.执行
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, ps);
        }
    }
}

posted @ 2021-07-17 22:56  子丶非鱼Zzz  阅读(72)  评论(0)    收藏  举报