mysql批量插入

來源:互聯網
上載者:User

標籤:

 說明下在建Statement的時候,後面兩個參數的意義: 
 
第一個參數指定 ResultSet 的類型。其選項有: 
TYPE_FORWARD_ONLY:預設類型。只允許向前訪問一次,並且不會受到其他使用者對該資料庫所作更改的影響。 
TYPE_SCROLL_INSENSITIVE:允許在列表中向前或向後移動,甚至可以進行特定定位,例如移至列表中的第四個記錄或者從當前位置向後移動兩個記錄。不會受到其他使用者對該資料庫所作更改              的影響。 
TYPE_SCROLL_SENSITIVE:象 TYPE_SCROLL_INSENSITIVE 一樣,允許在記錄中定位。這種類型受到其他使用者所作更改的影響。如果使用者在執行完查詢之後刪除一個記錄,那個記錄將從 Re              sultSet 中消失。類似的,對資料值的更改也將反映在 ResultSet 中。 
              第二個參數設定 ResultSet 的並發性,該參數確定是否可以更新 ResultSet。其選項有: 
CONCUR_READ_ONLY:這是預設值,指定不可以更新 
ResultSet CONCUR_UPDATABLE:指定可以更新 ResultSet


1 public void save(List<Item> itemList) { 2 3 try { 4 Connection conn = JdbcUtils.getConnection(); 5 String sql = "INSERT INTO `rbyair_trace`.`trace_info` (`track_id`, `member_id`, `mid`, `session_id`, `user_ip`, `opsystem`, `phone_m                 odel`, `app_name`, `app_version`, `campain_id`, `page_context`, `web_version`, `source_channel`, `start_from`, `banne                 r_index`, `activity_id`, `category_id`, `list_index`, `pin_id`, `target_pinid`, `group_id`, `share_target`, `share_us                 erid`, `joinuser_id`, `order_id`, `goods_id`, `pay_type`, `order_type`, `url`, `phone_time`, `trace_time`) VALUES (?,                 ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 6 conn.setAutoCommit(false); 7 PreparedStatement ps = conn 8 .prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, 9 ResultSet.CONCUR_READ_ONLY);10 int count = 0;11 for (Item item : itemList) {12 if (item.getTrackId()==null) {13 logger.error("該條資料沒有買點ID"+item.toString());14 continue;15 }16 ps.setString(1, item.getTrackId());17 ps.setInt(2, item.getMemberId());18 ps.setString(3, item.getMid());19 ps.setString(4, item.getSessionId());20 ps.setString(5, item.getUserIp());21 ps.setString(6, item.getOpsystem());22 ps.setString(7, item.getPhoneModel());23 ps.setString(8, item.getAppName());24 ps.setString(9, item.getAppVersion());25 ps.setString(10, item.getCampainId());26 ps.setString(11, item.getPageContext());27 ps.setString(12, item.getWebVersion());28 ps.setString(13, item.getSourceChannel());29 ps.setString(14, item.getStartFrom());30 ps.setInt(15, item.getBannerIndex());31 ps.setInt(16, item.getActivityId());32 ps.setInt(17, item.getCategoryId());33 ps.setInt(18, item.getListIndex());34 ps.setInt(19, item.getPinId());35 ps.setInt(20, item.getTargetPinid());36 ps.setInt(21, item.getGroupId());37 ps.setInt(22, item.getShareTarget());38 ps.setInt(23, item.getShareUserid());39 ps.setInt(24, item.getJoinuserId());40 ps.setLong(25, item.getOrderId());41 ps.setLong(26, item.getGoodsId());42 ps.setInt(27, item.getPayType());43 ps.setInt(28, item.getOrderType());44 ps.setString(29, item.getUrl());45 ps.setTimestamp(30, item.getPhoneTime());46 ps.setTimestamp(31, item.getTraceTime());47 48 ps.addBatch();49 count++;50 if (count % 10000 == 0) {51 // 1W條一提交52 ps.executeBatch();53 conn.commit();54 count = 0;55 }56 }57 ps.executeBatch();58 conn.commit();59 ps.clearBatch();60 } catch (SQLException e) {61 logger.error("插入資料錯誤" + e.getMessage());62 }

 

mysql批量插入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.