定时器任务
package com.satresoure;
import com.satresoure.util.MyTimerTask;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@EnableScheduling
public class SatResoureApplication {
public static void main(String[] args) {
SpringApplication.run(SatResoureApplication.class, args);
}
@Scheduled(cron = "0/2 * * * * ?")
public void springTask() {
MyTimerTask myTimerTask = new MyTimerTask("工作计划");
myTimerTask.run();
}
}
调用定时器的方法
package com.satresoure.util;
public class MyTimerTask extends java.util.TimerTask{
private String wxmc;
public MyTimerTask(String wxmc) {
this.wxmc = wxmc;
}
public String getWxmc() {
return wxmc;
}
public void setWxmc(String wxmc) {
this.wxmc = wxmc;
}
@Override
public void run() {
JavaToSQLTest javaToSQLTest = new JavaToSQLTest();
try {
javaToSQLTest.getData();
javaToSQLTest.setDate();
System.out.println("数据下载---");
} catch (Exception e) {
e.printStackTrace();
}
}
}
java读取数据库内容并存放到文件中进行加密
package com.satresoure.util;
import org.springframework.scheduling.annotation.Scheduled;
import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
/**
* java读取数据库内容并存放到文件中
*
* @author young
*/
public class JavaToSQLTest {
private static int key = 0x10;
/**
* 从远程数据库获取数据写入本地文件
*
* @throws FileNotFoundException
*/
public void getData() throws FileNotFoundException {
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
//新建txt名称为当前时间
File file = new File("D:\\"+formatter.format(date)+".txt");
PrintWriter pw = new PrintWriter(file);
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/cs?useUnicode=true&characterEncoding=UTF-8";
Connection conn = null;
String wxmc;
String lx;
String jssj;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, "root", "123456");
PreparedStatement ps = conn.prepareStatement("select * from ty_wxzy");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
// 对每个字符进行加密:
wxmc = rs.getString("wxmc")+",";
lx = rs.getString("lx")+",";
jssj = rs.getString("jssj");
for (int i = 0; i < wxmc.length(); ++i) {
pw.print((char) (wxmc.charAt(i) ^ key));
}
for (int i = 0; i < lx.length(); ++i) {
pw.print((char) (lx.charAt(i) ^ key));
}
for (int i = 0; i < jssj.length(); ++i) {
pw.print((char) (jssj.charAt(i) ^ key));
}
pw.println((char) ('\n' ^ key));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pw != null) {
pw.close();
}
}
}
读取文件夹内容并进行解密
/**
*
* @param filepath
* @return
*/
public static ArrayList<String> getfile(String filepath) {
try {
String temp;
StringBuilder sb = new StringBuilder();
File f = new File(filepath);
//指定读取编码用于读取中文
InputStreamReader read = new InputStreamReader(new FileInputStream(f), "utf-8");
ArrayList<String> readList = new ArrayList<String>();
BufferedReader reader = new BufferedReader(read);
while ((temp = reader.readLine()) != null && !"".equals(temp)) {
sb.setLength(0);
sb.append(temp);
for (int i = 0; i < sb.length(); ++i) {
sb.setCharAt(i, (char) (sb.charAt(i) ^ key));
}
readList.add(sb.toString());
}
read.close();
return readList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
解密后的数据添加至数据库
/**
* 将本地的txt文件添加至数据库
*/
public void setDate() {
try {
String s;// 保存每一行数据
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/cs?useUnicode=true&characterEncoding=UTF-8";
Connection conn = null;
Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url, "root", "123456");
//数据库操作接口
Statement st = null;
//实例化 st
try {
st = (Statement) conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
//txt文件读取
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
ArrayList<String> list = getfile("D:\\"+formatter.format(date)+".txt");
int num = list.size();
for (int i = 0; i < num; i++) {
if (list.get(i) != null) {
String[] p = list.get(i).split(",");
//txt每行可以分割成6个字符串存到是s[],
String sql = "insert into ty_wxzy_copy1(" +
"wxmc,lx,jssj) values(" +
"'" + p[0] + "','" + p[1] + "','" + p[2] + "')";
//执行插入语句
try {
st.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("添加成功");
}
}
版权声明:本文为weixin_47054648原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。