Java程式碼之JDBC實現資料庫之間定時的表格傳輸(由一個庫讀取到另一個庫)例項,親測有效
阿新 • • 發佈:2019-01-01
package com.openup.system.service.imp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.openup.system.service.DataReportService;
/**
* 定時任務 Service interface.
* @Table
*/
@Service
@Component
public class DataReportServiceImpl implements DataReportService{
/**
* @方法描述:定時資料匯入到統計表
*/
@Override
//@Scheduled(cron = "0 0 3 * * ?") //每天3點鐘執行方法
public void dataTransmission(){
//宣告Connection物件
Connection con;
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://*******; DatabaseName=****";
String user = "***";
String password = "***";
Connection con2;
String driver2 = "com.mysql.jdbc.Driver";
String url2 = "jdbc:mysql://***/****";
String user2 = "***";
String password2 = "***";
try {
//載入驅動程式
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
Class.forName(driver2);
con2 = DriverManager.getConnection(url2,user2,password2);
if(!con.isClosed()){
System.out.println("成功連線到資料庫!");
}
//2.建立statement類物件,用來執行SQL語句!!
Statement statement = con.createStatement();
//要執行的SQL語句
String sql = "select ID,R_Index from Rpt_SpeedReport";
//要執行的SQL語句
String sql2 = "insert into DIC_INDEX(DI_ID,DI_NAME,DI_IUSER,DI_ITIME) values(?,?,?,?)";
//3.ResultSet類,用來存放獲取的結果集!!
ResultSet rs = statement.executeQuery(sql);
PreparedStatement pst = con2.prepareStatement(sql2);
while(rs.next()){
System.out.print(rs.getString(1)+" ");
System.out.print(rs.getString(2)+"\n");
pst.setString(1,rs.getString(1));
pst.setString(2,rs.getString(2));
pst.setString(3,"1");
pst.setString(4,"201805111650");
pst.executeUpdate();
}
System.out.println("完成");
rs.close();
con.close();
con2.close();
}catch(ClassNotFoundException e) {
//資料庫驅動類異常處理
System.out.println("對不起,找不到驅動程式!");
e.printStackTrace();
} catch(SQLException e) {
//資料庫連線失敗異常處理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
DataReportServiceImpl a = new DataReportServiceImpl();
a.dataTransmission();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.openup.system.service.DataReportService;
/**
* 定時任務 Service interface.
* @Table
*/
@Service
@Component
public class DataReportServiceImpl implements DataReportService{
/**
* @方法描述:定時資料匯入到統計表
*/
@Override
//@Scheduled(cron = "0 0 3 * * ?") //每天3點鐘執行方法
public void dataTransmission(){
//宣告Connection物件
Connection con;
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://*******; DatabaseName=****";
String user = "***";
String password = "***";
Connection con2;
String driver2 = "com.mysql.jdbc.Driver";
String url2 = "jdbc:mysql://***/****";
String user2 = "***";
String password2 = "***";
try {
//載入驅動程式
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
Class.forName(driver2);
con2 = DriverManager.getConnection(url2,user2,password2);
if(!con.isClosed()){
System.out.println("成功連線到資料庫!");
}
//2.建立statement類物件,用來執行SQL語句!!
Statement statement = con.createStatement();
//要執行的SQL語句
String sql = "select ID,R_Index from Rpt_SpeedReport";
//要執行的SQL語句
String sql2 = "insert into DIC_INDEX(DI_ID,DI_NAME,DI_IUSER,DI_ITIME) values(?,?,?,?)";
//3.ResultSet類,用來存放獲取的結果集!!
ResultSet rs = statement.executeQuery(sql);
PreparedStatement pst = con2.prepareStatement(sql2);
while(rs.next()){
System.out.print(rs.getString(1)+" ");
System.out.print(rs.getString(2)+"\n");
pst.setString(1,rs.getString(1));
pst.setString(2,rs.getString(2));
pst.setString(3,"1");
pst.setString(4,"201805111650");
pst.executeUpdate();
}
System.out.println("完成");
rs.close();
con.close();
con2.close();
}catch(ClassNotFoundException e) {
//資料庫驅動類異常處理
System.out.println("對不起,找不到驅動程式!");
e.printStackTrace();
} catch(SQLException e) {
//資料庫連線失敗異常處理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
DataReportServiceImpl a = new DataReportServiceImpl();
a.dataTransmission();
}
}