伊莉討論區
標題:
查詢每日oracle 訂單 處理訂單存到MariaDB
[打印本頁]
作者:
atbox
時間:
2020-9-1 10:49 PM
標題:
查詢每日oracle 訂單 處理訂單存到MariaDB
本帖最後由 atbox 於 2020-9-1 10:53 PM 編輯
import java.sql.*;
public class oracle_to_mysql{
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String url = "jdbc
racle:thin
localhost:1521:test";
String user = "ctest_db";
String pass = "ctest_db";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException ex) { System.out.println(ex.getMessage()); }
// 1 PO 2客編 3簡稱 4客戶 5市話 6手機 7傳真 8地址 9代號 10日期
String str = "SELECT occ01,occ02,occ03,occ04,occ05,occ06,occ241,occud02,occud03,occdate "
+ " FROM ca1_db.occ_file "
+ " ORDER BY occdate";
try(
Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement(); //獲取用於向資料庫發送 SQL 語法的 Statement
ResultSet rs = stmt.executeQuery(str))
{
//System.out.println("ERP訂單資料");
// 1 PO 2客編 3簡稱 4客戶 5 市話 6手機 7 傳真 8地址 9代號 10日期
while(rs.next()){
System.out.println(rs.getRow()+" "+rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5)+" "+
rs.getString(6)+" "+rs.getString(7)+" "+rs.getString(8)+" "+rs.getString(9)+" "+rs.getString(10).substring(0,10));
String url2 = "jdbc:mysql://192.168.0.1:3306/account?characterEncoding=UTF-8";
String user2 = "root";
String pass2 = "root";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) { System.out.println(ex.getMessage()); }
String strsql;
strsql = "INSERT INTO customer(dealer_id,po_number,customer_number,bri_name,key_name,account_tel,account_phone,account_fax,account_addr,create_date) "
+ "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (Connection con2 = DriverManager.getConnection(url2, user2, pass2);
PreparedStatement ps = con2.prepareStatement(strsql))
{
// System.out.println("連線 Mariadb 資料庫");
int i=1;
// pstmt.setInt(1, 4); account_id AUTO_INCREMENT
ps.setInt (i++, 1); // 1 dearl_id
ps.setString(i++, rs.getString(1));// PO 訂單編號
ps.setString(i++, rs.getString(2));// 客編
ps.setString(i++, rs.getString(3));// 簡稱
ps.setString(i++, rs.getString(4));// 客戶
ps.setString(i++, rs.getString(5));// 市話
ps.setString(i++, rs.getString(6));// 手機
ps.setString(i++, rs.getString(7));// 傳真
ps.setString(i++, rs.getString(8));// 地址
ps.setString(i++, rs.getString(9).substring(0,10));// 開通日
int count = ps.executeUpdate();
System.out.println("新增 customer " + count + " 筆");
con2.close();
}catch (SQLException ex) { System.out.println(ex.getMessage()); }
}
conn.close();
}catch (SQLException ex) { System.out.println(ex.getMessage()); }
}
}
作者:
atbox
時間:
2020-9-1 10:57 PM
本帖最後由 atbox 於 2020-9-1 10:59 PM 編輯
從網路上看ORACLE 和的 mysql 的寫法
再將這兩種資料庫給連起來給有用到的人參考
歡迎光臨 伊莉討論區 (https://attach1201.eyny.com/)
Powered by Discuz!