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();
}
}