一次性获取多个oracle序列的值,实现关联表多数据的批量insert
前些天發(fā)現(xiàn)了一個巨牛的人工智能學(xué)習(xí)網(wǎng)站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉(zhuǎn)到教程。
業(yè)務(wù) 要求批量導(dǎo)入不小于10W條數(shù)據(jù)到 user 表,但是user表在 insert ?每條數(shù)據(jù)的同時要 ?insert ?一條對應(yīng)數(shù)據(jù)到 customer表,
并且是以 ?customer ?表的主鍵作為 user 表的外鍵。
?
所以想到要一次性獲取多個 序列值,再把對應(yīng)的序列給不同表,并分別作為兩個表的主鍵和外鍵的值。
?
方法很簡單 ?就一句代碼 :
?
String squence ="select USR_CUSTOMER_SEQ.nextval cust_id from (select 1 from all_objects where rownum <= "+usrlist.size()+")";List<String> squenceList = BatchInsert.selectSql(squence);"select USR_CUSTOMER_SEQ.nextval cust_id from (select 1 from all_objects where rownum <= "+usrlist.size()+")";List<String> squenceList = BatchInsert.selectSql(squence);?
usrlist?是解析表格后得到的要導(dǎo)入的 user數(shù)據(jù) 集合,有多少條數(shù)據(jù)就取多少個序列值。
selectSql 方法 只是JDBC連接數(shù)據(jù)庫 執(zhí)行了這句SQL 并返回了查到的 序列值,拿到這個序列集合就可以根據(jù)業(yè)務(wù)作后續(xù)實現(xiàn)了。
?
?
// 單純查詢 public static List<String> selectSql(String sql){Connection conn = null;//定義為空值Statement stmt = null;ResultSet rs = null;conn = getConnection();List<String> list = new ArrayList<String>();try {stmt = conn.createStatement();//創(chuàng)建一個Statement語句對象rs = stmt.executeQuery(sql);//執(zhí)行sql語句while(rs.next()){list.add(rs.getString("cust_id"));}} catch (SQLException e) {e.printStackTrace();}finally{try {conn.close();stmt.cancel();rs.close();}catch (SQLException e) {e.printStackTrace();}}return list;}
getConnection?方法是獲取數(shù)據(jù)庫連接:
?
?
public static Connection getConnection(){ //連接數(shù)據(jù)庫的方法 try { Class.forName("oracle.jdbc.driver.OracleDriver"); //初始化驅(qū)動包 conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } return conn;}?
?
?
?
?
另 ?批量導(dǎo)入實現(xiàn)見博文:關(guān)聯(lián)表多數(shù)據(jù)的批量insert (批量導(dǎo)入,測試10W條數(shù)據(jù)用時46秒)
?
總結(jié)
以上是生活随笔為你收集整理的一次性获取多个oracle序列的值,实现关联表多数据的批量insert的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用ASP创建MDaemon用户
- 下一篇: 如何编写用户操作手册