使用的Java教程網站 - 的例子
public static void viewTable(Connection con, String dbName)
throws SQLException {
//Modify this code according to 2nd database vendor and credentials-
String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn2 = DriverManager.getConnection(url);
Statement stmt = null;
Statement insertStmt = null;
String query = "select COF_NAME, SUP_ID, PRICE, " +
"SALES, TOTAL " +
"from " + dbName + ".COFFEES";
try {
stmt = con.createStatement();
insertStmt = conn2.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
String insertQuery = "INSERT INTO COFFEES (COF_NAME, SUP_ID, PRICE, sales, total) VALUES (coffeeName,supplierID, proce, sales, total);
try{
insertStmt.execute(insertQuery);
}catch(SQLExeption e){
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) { stmt.close();}
if(insertStmt != null) {insertStmt.close();}
}
}
使用2種不同'Connections',一個指向源數據庫和其他定位到目標數據庫,並使用批量插入來處理工作。無論如何,如果可以的話,最好使用像Pentaho這樣的ETL工具 –
您應該執行類似於[this(using MySQL)]中給出的語句(http://www.tech-recipes.com/rx/) 1487 /複製一個存在的MySQL的表到一個新表/)。因爲你沒有修改任何數據。 –