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 weather warning 2016"), "Doc");
list<file> ll = f.getfiles (new File ("d:\\testfiledir\\t weather warning 2016"), "Doc");
Calendar cal = Calendar.getinstance ();
String warntype = null;//alert type
String warnrange = null; Alert level
String type = null;//spontaneous or forwarded
String type = "forward";//spontaneous or forward
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 ("Hail")) {
Warntype = "Hail";
}else if (name.contains ("Typhoon")) {
Warntype = "Typhoon";
}else if (Name.contains ("rainstorm")) {
Warntype = "Rainstorm";
}else if (name.contains ("High Temperature")) {
Warntype = "High Temperature";
}else if (Name.contains ("cold wave")) {
Warntype = "cold wave";
}else if (Name.contains ("Fog")) {
Warntype = "Fog";
}else if (Name.contains ("Thunder")) {
Warntype = "Thunder";
}else if (Name.contains ("Gale")) {
Warntype = "Gale";
}else if (Name.contains ("Sandstorm")) {
Warntype = "Sandstorm";
}else if (Name.contains ("Hail")) {
Warntype = "Hail";
}else if (name.contains ("Road Icing")) {
Warntype = "road icing";
}else if (name.contains ("dry")) {
Warntype = "drought";
}else if (Name.contains ("Frost")) {
Warntype = "Frost";
}else if (name.contains ("Haze")) {
Warntype = "Haze";
}else if (Name.contains ("ozone")) {
Warntype = "Ozone";
}
if (Name.contains ("Blue Alert")) {
Warnrange = "Blue Warning";
}else if (name.contains ("Yellow Alert")) {
Warnrange = "yellow warning";
}else if (Name.contains ("Red Alert")) {
Warnrange = "red warning";
}else if (name.contains ("Orange Alert")) {
Warnrange = "orange alert";
}
if (Name.contains ("forwarding")) {
Type = "Forward";
}else{
Type = "spontaneous";
}
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, load the corresponding drive
conn = (Connection) drivermanager.getconnection (URL, username, password);
SYSTEM.OUT.PRINTLN ("Database connection succeeded");
} catch (Exception e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Database connection failed");
}
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;
}
}
Read local hard disk files and quickly scan into database