java實現從一個資料庫查詢資料經過處理匯入另外一個資料庫中

來源:互聯網
上載者:User

標籤:java   資料庫   jdbc   oracle   導資料   

            當資料庫表中有clob欄位或要對錶中資料做較複雜處理時就不太好用指令碼從一個資料庫匯入資料到另外一個資料庫中了,這時就要通過代碼實現了,下面以orale資料庫為例代碼如下:

import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.UUID;import oracle.jdbc.driver.OracleConnection;import oracle.sql.CLOB;/** * import data from one database to another *  * @author  * @version  */public class ImportDataByJdbc {public static void main(String[] args) throws Exception {Connection con = null;// 建立一個資料庫連接PreparedStatement pre = null;// 建立先行編譯語句對象,一般都是用這個而不用StatementResultSet result = null;// 建立一個結果集對象try {Class.forName("oracle.jdbc.driver.OracleDriver");// 載入Oracle驅動程式System.out.println("開始嘗試串連cms資料庫!");String url = "jdbc:oracle:" + "thin:@192.168.0.xx:1521:ORCL";String user = "policy";// 使用者名稱,系統預設的賬戶名String password = "xxxx";// 你安裝時選設定的密碼con = DriverManager.getConnection(url, user, password);// 擷取串連System.out.println("cms串連成功!");String sql = "select t.title,t.channel_code,t.content from cms_news t";// 先行編譯語句,“?”代表參數pre = con.prepareStatement(sql);// 執行個體化先行編譯語句result = pre.executeQuery();// 執行查詢,注意括弧中不需要再加參數importData(result);// while (result.next())// 當結果集不為空白時// System.out.println("使用者名稱:" + result.getString("title"));} catch (Exception e) {e.printStackTrace();} finally {try {// 逐一將上面的幾個對象關閉,因為不關閉的話會影響效能、並且佔用資源// 注意關閉的順序,最後使用的最先關閉if (result != null)result.close();if (pre != null)pre.close();if (con != null)con.close();System.out.println("cms資料庫連接已關閉!");} catch (Exception e) {e.printStackTrace();}}}public static String importData(ResultSet result) {Connection con = null;// 建立一個資料庫連接PreparedStatement pre = null;// 建立先行編譯語句對象,一般都是用這個而不用StatementResultSet result2 = null;// 建立一個結果集對象Timestamp currentTime = new Timestamp(System.currentTimeMillis());SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String dateString = formatter.format(currentTime);try {Class.forName("oracle.jdbc.driver.OracleDriver");// 載入Oracle驅動程式System.out.println("開始嘗試串連omp資料庫!");String url = "jdbc:oracle:" + "thin:@192.168.0.xx:1521:orclgbk";String user = "xjsp";// 使用者名稱,系統預設的賬戶名String password = "xxx";// 你安裝時選設定的密碼con = DriverManager.getConnection(url, user, password);// 擷取串連System.out.println("omp串連成功!");int num = 0;while (result.next()) {String id = getUUID();String entryType = getEntryType(result.getString("CHANNEL_CODE"));String sql = "insert into kms_entry(ID,ENTRY_NAME,ENTRY_TYPE,ENTRY_CONTENT,EDIT_FLAG,AUDIT_TIME,IF_DEL,IF_AUDIT,LAST_EDIT_TIME,CREATE_USER,CREATE_TIME,LAST_EDIT_USER) "+ " values('"+ id+ "','"+ result.getString("title")+ "','"+ entryType+ "',?,'1',to_date('"+ dateString+ "','yyyy-mm-dd hh24:mi:ss'),'1','3',to_date('"+ dateString+ "','yyyy-mm-dd hh24:mi:ss'),'00000000000000000000000000000000',to_date('"+ dateString+ "','yyyy-mm-dd hh24:mi:ss'),'00000000000000000000000000000000')";// 先行編譯語句,“?”代表參數pre = con.prepareStatement(sql);// 執行個體化先行編譯語句CLOB clob = new CLOB((OracleConnection) con); // 建立一個執行個體化對象clob = oracle.sql.CLOB.createTemporary((OracleConnection) con,true, 1);clob.putString(1, result.getString("CONTENT")); // 為對象賦值pre.setClob(1, clob);pre.execute();pre.close();num = num + 1;System.out.println("插入成功第" + (num) + "條資料");}} catch (Exception e) {e.printStackTrace();} finally {try {// 逐一將上面的幾個對象關閉,因為不關閉的話會影響效能、並且佔用資源// 注意關閉的順序,最後使用的最先關閉if (result != null)result.close();if (pre != null)pre.close();if (con != null)con.close();System.out.println("omp資料庫連接已關閉!");} catch (Exception e) {e.printStackTrace();}}return null;}/** * 擷取uuid *  * @return */public static String getUUID() {UUID uuid = UUID.randomUUID();String s = uuid.toString();String uuids = s.substring(0, 8) + s.substring(9, 13)+ s.substring(14, 18) + s.substring(19, 23) + s.substring(24);return uuids;}public static String getEntryType(String chanelcode) {String entryTypeString = "";if (chanelcode != "" && chanelcode != null) {處理資料.....}return entryTypeString;}}
           其中clob欄位要用放進clob對象然後才進行插入,getUUID是當資料庫中表的主鍵不是自增長產生時需要自己擷取進行儲存,如果資料庫表設定的是自增長主鍵就不需要這個了




java實現從一個資料庫查詢資料經過處理匯入另外一個資料庫中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.