Java通过jdbc连接Mysql数据库 二 之批量插入提升性能

var connection: Connection = null
    var pstmt:PreparedStatement = null
    try{
      connection = MySQLUtils.getConnection()

      connection.setAutoCommit(false)//设置手动提交

      pstmt = connection.prepareStatement(
        "insert into day_video_access_topn_stat(day,cms_id,times)" +
          "values(?, ?, ?)")
      for (elem <- list) {
        pstmt.setString(1, elem.day)
        pstmt.setLong(2, elem.cmsId)
        pstmt.setLong(3, elem.times)

        pstmt.addBatch()
      }
      pstmt.executeBatch()//执行批量处理,性能会好很多
      connection.commit()//手工提交

    }catch {
      case exception: Exception => exception.printStackTrace()
    }finally {
      MySQLUtils.release(connection, pstmt)
    }
posted @ 2020-10-29 13:08  火火火火四火  阅读(194)  评论(0编辑  收藏  举报