java获取数据库数据保存到本地txt文件中
生活随笔
收集整理的這篇文章主要介紹了
java获取数据库数据保存到本地txt文件中
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先是Services
@Service public class logServiceimpl implements logService {@Autowired@Qualifier("primaryJdbcTemplate")protected JdbcTemplate jdbcTemplate;@Overridepublic List<lockLog> getlog() {String sql="show OPEN TABLES where In_use > 0;";List<Map<String, Object>> list= jdbcTemplate.queryForList(sql);JSONArray jsonArray = new JSONArray();jsonArray.addAll(list);List<lockLog> lockLogList = jsonArray.toJavaList(lockLog.class);return lockLogList;}}然后是Controller
@Component public class runCron {@Resourceprivate logService logservice;@Value("${dir}")private String dir;@Scheduled(cron="0 0/1 * * * ?") //每一分鐘執行一次public void getlog() throws IOException {List<lockLog> mapList = logservice.getlog();File file = new File(dir);if (!file.exists()) {file.createNewFile();}//true 禁止覆蓋之前的數據FileWriter fileWriter=new FileWriter(file,true);PrintWriter fout = new PrintWriter(fileWriter);SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date(System.currentTimeMillis());fout.write(formatter.format(date));String huanhang = System.getProperty("line.separator");fout.write(huanhang);for (int i = 0; i < mapList.size(); i++) {lockLog sql = (lockLog) mapList.get(i);String content = sql.getDatabase() + "\t" + sql.getTable() + "\t" + sql.getIn_use() + "\t" + sql.getName_locked();fout.write(content);huanhang = System.getProperty("line.separator");fout.write(huanhang);}fout.flush();fout.close();}}最后是配置文件application.properties
dir=D:\\aaa.txtspring.datasource.primary.jdbc-url=jdbc:mysql://192.168.xx:xx/xx spring.datasource.primary.username=xx spring.datasource.primary.password=xx spring.datasource.primary.driver-class-name= com.mysql.cj.jdbc.Driver總結
以上是生活随笔為你收集整理的java获取数据库数据保存到本地txt文件中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 路由器的设置方法-路由器的信道如何设置
- 下一篇: link无线路由器怎么设置-用手机如何设