思路:
1.加载配置文件,进行对 数据库的相关初始化及其他相关参数
2.Config <通过load函数加载配置文件中的除Database信息相关的其他配置参数的内容>
1.加载配置文件,进行对 数据库的相关初始化及其他相关参数
2.尝试打开指定的xls文件开始对数据库表写入
相关资源
,比如配置文件,创建数据库表的sql 语句及readme.txt见 本人的上传资源具体地址为:
具体代码展示:
1.Main <主流程控制类>
/*
* Copyright 2007 - 2014 RollStone
*
* All Rights Reserved.
*/
package csi;
import csi.file.FileBase;
import csi.file.FileDelay;
import csi.file.FileFail;
import csi.file.FileFirewall;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* @brief
* @author RollStone
* @created Jul 9, 2014 5:05:19 PM
*/
public class Main {
public static void main(String[] args) {
//确定日期,若是无参数则取值为昨天的yyyymmdd
String dateStr;
if (args.length == 1) {
dateStr = args[0];
} else {
dateStr = getYesterday();
}
Config config = new Config();
config.load();
FileBase[] fbs = new FileBase[]{new FileFirewall(), new FileFail(), new FileDelay()};
try {
Connection conn = new DBConnector().getConnection();
conn.setAutoCommit(false); //以防止分割的时候只进行了部分插入
for (FileBase base : fbs) {
base.setMsgMaxLength(config.getMsgMaxLength());
base.setExcelFilename(config.getProvinceCode(), dateStr);
String excelfilename = base.getExcelFilename();
int existRecordNums = base.checkData(conn, excelfilename);
String efWithPath = config.getExcelPath() + excelfilename;
base.writeExcelContentToTable(conn, efWithPath, existRecordNums + 1);
}
conn.close();
} catch (SQLException e) {
}
}
public static String getYesterday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
return sdf.format(c.getTime());
}
}
2.Config <通过load函数加载配置文件中的除Database信息相关的其他配置参数的内容>
/*
* Copyright 2000 - 2014 RollStone
* All Rights Reservered.
*/
package csi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @brief
*
* @author RollStone
* @created 2014-07-24 03:34:39
*/
public class Config {
private static final String DEFAULT_CONFIG = "pax.properties";
private String configFile;
protected Properties properties = new Properties();
public Config(String cfg) {
configFile = cfg;
initOthers();
}
public Config() {
configFile = DEFAULT_CONFIG;
initOthers();
}
/ Database part /
//This section be extends by class DBConnector
/ Config others /
private String msgMaxLenth;
private String excelPath;
private String provinceCode;
private boolean isLinux;
private boolean isTest;
private void initOthers() {
msgMaxLenth = null;
excelPath = null;
provinceCode = null;
isLinux = true;
isTest = false;
}
public int getMsgMaxLength() {
return Integer.parseInt(msgMaxLenth);
}
public String getProvinceCode() {
return provinceCode;