读取本地硬盘文件,快速扫描插入 数据库
package demo;
import java.io.File;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import demo.pojo.SignlModel;
public class Jiaogu {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
getConn();
Jiaogu f = new Jiaogu();
// List<File> ll = f.getFiles(new File("d:\\testfiledir\\T天气预警2016年"),"doc");
List<File> ll = f.getFiles(new File("d:\\testfiledir\\T天气预警2016年"),"doc");
Calendar cal = Calendar.getInstance();
String warnType = null;//预警类型
String warnRange = null; //预警级别
String type = null;//自发或转发
// String type = "转发";//自发或转发
for (File ff : ll) {
long time = ff.lastModified();
cal.setTimeInMillis(time);
String name = ff.getName();
String filePath = ff.getPath();
filePath = filePath.replace("\\", "\\\\");
String moditimme = cal.getTime().toLocaleString();
System.out.println(filePath);
System.out.println(name);
if(name.contains("冰雹")){
warnType = "冰雹";
}else if(name.contains("台风")){
warnType = "台风";
}else if(name.contains("暴雨")){
warnType = "暴雨";
}else if(name.contains("高温")){
warnType = "高温";
}else if(name.contains("寒潮")){
warnType = "寒潮";
}else if(name.contains("大雾")){
warnType = "大雾";
}else if(name.contains("雷电")){
warnType = "雷电";
}else if(name.contains("大风")){
warnType = "大风";
}else if(name.contains("沙尘暴")){
warnType = "沙尘暴";
}else if(name.contains("冰雹")){
warnType = "冰雹";
}else if(name.contains("道路结冰")){
warnType = "道路结冰";
}else if(name.contains("干旱")){
warnType = "干旱";
}else if(name.contains("霜冻")){
warnType = "霜冻";
}else if(name.contains("霾")){
warnType = "霾";
}else if(name.contains("臭氧")){
warnType = "臭氧";
}
if(name.contains("蓝色预警")){
warnRange = "蓝色预警";
}else if(name.contains("黄色预警")){
warnRange = "黄色预警";
}else if(name.contains("红色预警")){
warnRange = "红色预警";
}else if(name.contains("橙色预警")){
warnRange = "橙色预警";
}
if(name.contains("转发")){
type = "转发";
}else{
type = "自发";
}
System.out.println(moditimme);
String id = UUID.randomUUID().toString();
SignlModel signlModel = new SignlModel();
signlModel.setId(id);
signlModel.setFileName(name);
signlModel.setFilePath(filePath);
signlModel.setType(type);
signlModel.setUpdateTime(moditimme);
signlModel.setWarnRank(warnRange);
signlModel.setWarnType(warnType);
insert(signlModel);
}
}
public List<File> getFiles(File fileDir, String fileType) {
List<File> lfile = new ArrayList<File>();
File[] fs = fileDir.listFiles();
for (File f : fs) {
if (f.isFile()) {
if (fileType
.equals(f.getName().substring(
f.getName().lastIndexOf(".") + 1,
f.getName().length())))
lfile.add(f);
} else {
List<File> ftemps = getFiles(f,fileType);
lfile.addAll(ftemps);
}
}
return lfile;
}
private static Connection getConn() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/pj_sz?useUnicode=true&characterEncoding=GBK";
String username = "root";
String password = "root";
Connection conn = null;
try {
Class.forName(driver); //classLoader,加载对应驱动
conn = (Connection) DriverManager.getConnection(url, username, password);
System.out.println("数据库连接成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库连接失败");
}
return conn;
}
private static int insert(SignlModel signlModel) {
Connection conn = getConn();
int i = 0;
// String sql = "insert into signlmodel (id,filename,filepath,warntype,warnrank,updatetime,type) values(?,?,?,?,?,?,?)";
String sql = "insert into historymodel (id,filename,filepath,warntype,warnrank,updatetime,type) values(?,?,?,?,?,?,?)";
System.out.println(sql);
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, signlModel.getId());
pstmt.setString(2, signlModel.getFileName());
pstmt.setString(3, signlModel.getFilePath());
pstmt.setString(4, signlModel.getWarnType());
pstmt.setString(5, signlModel.getWarnRank());
pstmt.setString(6, signlModel.getUpdateTime());
pstmt.setString(7, signlModel.getType());
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
}