文件中描述了2個(gè)導(dǎo)入工作:(1)表EMPLOYEE_TEST(2)表YFJBXX_TEST,文件中每張表的列名和類型需要和導(dǎo)入的目標(biāo)表一致,且順序和文件中要導(dǎo)入的內(nèi)容相匹配,例如下面的csv文件:
"ID","DEP","NAME","AREA","AGE","SEX","XUELI","SALARY","PRIX"
"001","研發(fā)","張三","北京","34","女","本科","4546","1"
"002","銷售","李四","天津","45","男","專科","4456","2"
可以看到導(dǎo)入的順序和上述XML文件中的列名一致。
如果從文件導(dǎo)入到數(shù)據(jù)庫中完全匹配,只需配置文件中的<Bean name="ImportDirectImpl" class="fileimport.ImportDirectImpl"/>即可,代碼中調(diào)用示例:
//設(shè)置XML配置文件所在位置
FileImportInitFactory.setConfigFileName("E:/EclipseProjects/WISImport/bin/fileimport/FileImportConfig.xml");
&nb
sp; FileImportInitFactory.init();
HashMap h = new HashMap(1,1);
//如果有日期型的列,需要設(shè)置DateFormat并放入HashMap中
h.put("DateFormat","yyyy-MM-dd HH:mm:ss");
//執(zhí)行導(dǎo)入工作
new ImportFileEntry().importFile("導(dǎo)入的文件路徑及名稱.csv","YFJBXX_TEST",false,"ImportDirectImpl",h);
ImportFileEntry()的importFile方法說明:
/**
* 從文件導(dǎo)入到指定表中
* @param fileName String 要導(dǎo)入文件名
* @param tableName String 導(dǎo)入目標(biāo)表名
* @param firstLineRead boolean 是否讀取第一行
* @param dealClass 處理類名稱(例如配置文件中Bean name="ImportDirectImpl")
* @param aHashMap 擴(kuò)展用,需特殊處理時(shí)可置入變量
* @throws Exception
*/
public void importFile(String fileName, String tableName, boolean firstLineRead,String dealClass,HashMap aHashMap) throws Exception ;
至此,一個(gè)簡(jiǎn)單的不需做任何處理直接從文件導(dǎo)入數(shù)據(jù)庫對(duì)應(yīng)表的功能就實(shí)現(xiàn)了。
但是有些時(shí)候我們需要進(jìn)行特殊的處理,例如表中的當(dāng)前操作日期列在導(dǎo)入文件中沒有,需要在代碼中加入,這時(shí)就需要實(shí)現(xiàn)FileImportInterface接口并加入到配置文件中例如:<Bean name="ImportWISImpl" class="fileimport.ImportWISImpl"/>,ImportWISImpl的實(shí)現(xiàn)代碼見后續(xù)代碼清單。
t;);
columnListInfoCode[i][0] = columnName;
columnListInfoCode[i][1] = columnType;
System.out.println("Code columnName:" + columnName + " columnType:" + columnType);
}
}
//生成實(shí)例
ImportTableInfoBO importAction = new ImportTableInfoBO();
importAction.setTableName(tableName);
importAction.setColumnNamesFile(columnListInfoFile);
if (columnListInfoCode != null) {
importAction.setColumnNamesCode(columnListInfoCode);
}
//放入靜態(tài)容器中
importJobList.put(tableName, importAction);
}
//2.其他配置信息
Element importDealClassList = (Element) eroot.getElementsByTagName("ImportDealClassList").item(0);
String className;
String classFullName;
NodeList beanList = importDealClassList.getElementsByTagName("Bean");
for (int j=0; j < beanList.getLength(); j++){
className = ( (Element) beanList.item(j)).getAttribute("name");
classFullName = ( (Element) beanList.item(j)).getAttribute("class");
dealClassList.put(className,classFullName);
}
System.out.println("importJobList.size()" + importJobList.size());
System.out.println("dealClassList.size()" + dealClassList.size());
; insertNum = 1;
} else {
insertNum++;
}
}
} catch (Exception e) {
e.printStackTrace();
//---寫入數(shù)據(jù)庫待加入----
//log.error(e.getMessage());
}
}
public void close() {
if (conn != null) {
try {
if (conn != null) {
DbUtils.commitAndClose(conn);
System.out.println("close end");
}
} catch (SQLException e) {
e.printStackTrace();
//log.error(e.getCause());
}
}
}
/**
*
* @param dateString String
* @param format String
* @return Date
*/
public Date formatDate(String dateString,String format){
try{
SimpleDateFormat f = new SimpleDateFormat(format);
return f.parse(dateString);
}catch(Exception e){
return null;
}
}
}
八、調(diào)用示例
package fileimport;
import java.util.HashMap;
public class ImportFileExample {
public ImportFileExample() {
}
public static void main(String[] args) {
try {
String ls = "c:/temp/employee_test.csv";