您的位置:首页 > 数据库

Java代码之JDBC实现数据库之间定时的表格传输(由一个库读取到另一个库)实例,亲测有效

2018-05-11 16:55 1021 查看
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();
}
} 阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐