zoukankan      html  css  js  c++  java
  • MYSQL 之 JDBC(五): 增删改查(三)PreparedStatement

    是Statement的子接口,可以传入带占位符的sql语句,并且提供了补充占位符变量的方法。

    使用Statement需要进行拼写SQL语句,很辛苦,很容易出错。

    引号的问题处理很复杂,不利于维护。

    可以有效的禁止sql注入。(通过用户输入非法的sql命令)

    代码的可读性和可维护性,最大可能的提高性能(批量插入)

    代码测试

    复制代码

    public void testPreparedStatement() {
        Connection connection = null;
        PreparedStatement ps = null;
    
        try {
            connection = JDBCTools.getConnection();
            String sql = "insert into t_user (id, username, pwd, regTime, lastLoginTime) values(?,?,?,?,?)";
            ps = connection.prepareStatement(sql);
            ps.setInt(1, 2);
            ps.setString(2, "狗贼");
            ps.setString(3, "123456");
            ps.setDate(4, new Date(System.currentTimeMillis()));
            ps.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(ps, connection);
        }
    }
  • 相关阅读:
    [USACO12FEB]牛券Cow Coupons
    合并果子
    序列合并
    中位数
    道路游戏
    教主的花园
    摆花
    hello world之Makefile
    mysql+tomcat+spring 配置心得(从0开始搭环境)
    C#,.Net自动生成大写字母编码
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13308980.html
Copyright © 2011-2022 走看看