JDBC PreparedStatement和事务提交

代码的漏洞

通过1 'or ' 1 '=' 1让password判断永久为true从而登录到系统

  •  PreparedStatement的使用

    目录

    代码的漏洞

     PreparedStatement的使用

    批量数据添加

    一.用一个preparedStatement存入5万次 访问了5万次io效率低下

    二.用pstmt的addBatch();批处理 暂存一定的数据统计发送给数据库

     示例代码:


    • public interface PreparedSteatment  extends Stetement
      • 称为预编译的statement对象
      • 和statement区别
        • 对sql语句不使用拼接的方式,而是用问号代替,占位
    • 在获取preparedStatemment对象时就要求执行
    • 在数据库服务器上只要执行一次就会被缓存下来 preparedStatemment的优势
        public static void main(String[] args) throws  Exception{
              Scanner sc = new Scanner(System.in);
              String next1 = sc.next();
              String next2 = sc.next();
              Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/companydb", "root", "root");
              Class.forName("com.mysql.jdbc.Driver");//mysql的
      
              DriverManager.getConnection("jdbc:mysql:///companydb", "root", "root");
              String sql = "SELECT ?,?  FROM User ";
              PreparedStatement pstatement = (PreparedStatement) connection.prepareStatement(sql);
              pstatement.setString(1,next1);
              pstatement.setString(2,next2);
              pstatement.executeQuery();
              System.out.println("完成");
              pstatement.close();
              connection.close();
          }

      批量数据添加

      • 实际开发中,用preparedStatemment
    • 一.用一个preparedStatement存入5万次 访问了5万次io效率低下

    • 二.用pstmt的addBatch();批处理 暂存一定的数据统计发送给数据库

       示例代码:

      public static void main(String[] args) throws  Exception{
              Class.forName("com.mysql.jdbc.Driver");//mysql的
              //注册驱动
              Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
             //获取连接
              DriverManager.getConnection("jdbc:mysql:///test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true", "root", "root");
              //打开批处理
              String sql ="insert  into  account values(null ,?,?)  ";
              //sql语句
              PreparedStatement pstatement = (PreparedStatement) connection.prepareStatement(sql);
              //获取PreparedStatement对象执行sql
              for (int i =1;i<50000;i++)
              {
                  pstatement.setString(1,"account"+i);
                  pstatement.setInt(2,i);
                  pstatement.addBatch();
                  if (i%1000==0)//一次处理1000条数据
                  {
                      pstatement.executeBatch();
                      pstatement.clearBatch();
                  }
              }
      

      手动提交事务

      • 批量删除TRUNCATE:删除磁盘文件,新建一个空表
      • 默认情况下,mysql是不支持批处理,在jdbc批处理参数
      • 打开批处理开关characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
   public static void main(String[] args) throws  Exception{
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = (Connection) DriverManager.getConnection
                ("jdbc:mysql:///test", "root", "root");
        connection.setAutoCommit(false);
        String sql1="update account set accounts=? where card_id=1";
        String sql2="update account set accounts=? where card_id=2";
        PreparedStatement pst = (PreparedStatement) connection.prepareStatement(sql1);
        PreparedStatement pst1 = (PreparedStatement) connection.prepareStatement(sql2);
            pst.setInt(1,1500);
            pst1.setInt(1,500);
                
            pst.executeUpdate();
            pst1.executeUpdate();
            connection.commit();

        try {
            connection.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            connection.close();
        }

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值