近期由于项目需要,实现了从.csv或者.txt文件中读取每行信息后插入到Oracle数据库中的功能,特共享出来。用到第三方开源工具包:opencsv-1.8.jar和commons-dbutils-1.1.jar,需单独下载。
首先需要配置XML文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<DefaultImport>Employee</DefaultImport>
<TargetTable tableName="EMPLOYEE_TEST">
<!--表中对应文件的列名和类型信息-->
<ColumnFileInfo>
<Column name="ID" type="String"/>
<Column name="DEP" type="String"/>
<Column name="NAME" type="String"/>
<Column name="AREA" type="String"/>
<Column name="AGE" type="Number"/>
<Column name="SEX" type="String"/>
<Column name="XUELI" type="String"/>
<Column name="SALARY" type="Number"/>
<Column name="PRIX" type="Date"/>
</ColumnFileInfo>
</TargetTable>
<TargetTable tableName="YFJBXX_TEST">
<ColumnFileInfo>
<Column name="GLGXZJ" type="String"/>
<Column name="YXTZJ" type="String"/>
<Column name="YFXM" type="String"/>
<Column name="JZDZ" type="String"/>
<Column name="JZDXZQH_DM" type="String"/>
<Column name="HJDZ" type="String"/>
<Column name="HJDXZQH_DM" type="String"/>
<Column name="SFZH" type="String"/>
<Column name="CSRQ" type="String"/>
<Column name="MZ_DM" type="String"/>
<Column name="WHCD_DM" type="String"/>
<Column name="HKXZ_DM" type="String"/>
<Column name="HYZK_DM" type="String"/>
<Column name="CHRQ" type="String"/>
<Column name="JRXTRQ" type="String"/>
<Column name="TCXTRQ" type="String"/>
<Column name="TCYY_DM" type="String"/>
<Column name="ZF_XM" type="String"/>
<Column name="ZF_JJDZ" type="String"/>
<Column name="ZF_JJDXZQHDM_DM" type="String"/>
<Column name="ZF_HJDZ" type="String"/>
<Column name="ZF_HJDXZQH_DM" type="String"/>
<Column name="ZF_SFZH" type="String"/>
<Column name="ZF_CSRQ" type="String"/>
<Column name="ZF_MZ_DM" type="String"/>
<Column name="ZF_WHCD_DM" type="String"/>
<Column name="ZF_HKXZ_DM" type="String"/>
<Column name="LGRZRQ" type="String"/>
</ColumnFileInfo>
<ColumnCodeInfo>
<Column name="LRSJ" type="Date"/>
<Column name="SBPCH" type="String"/>
<Column name="SBDWXZQH_DM" type="String"/>
</ColumnCodeInfo>
</TargetTable>
<ImportDealClassList>
<Bean name="ImportDirectImpl" class="fileimport.ImportDirectImpl"/>
<Bean name="ImportWISImpl" class="fileimport.ImportWISImpl"/>
</ImportDealClassList>
<MaxCommitNumber>5000</MaxCommitNumber>
</root>
文件中描述了2个导入工作:(1)表EMPLOYEE_TEST(2)表YFJBXX_TEST,文件中每张表的列名和类型需要和导入的目标表一致,且顺序和文件中要导入的内容相匹配,例如下面的csv文件:
"ID","DEP","NAME","AREA","AGE","SEX","XUELI","SALARY","PRIX"
"001","研发","张三","北京","34","女","本科","4546","1"
"002","销售","李四","天津","45","男","专科","4456","2"
可以看到导入的顺序和上述XML文件中的列名一致。
如果从文件导入到数据库中完全匹配,只需配置文件中的<Bean name="ImportDirectImpl" class="fileimport.ImportDirectImpl"/>即可,代码中调用示例:
//设置XML配置文件所在位置
FileImportInitFactory.setConfigFileName("E:/EclipseProjects/WISImport/bin/fileimport/FileImportConfig.xml");
&nb
sp; FileImportInitFactory.init();
HashMap h = new HashMap(1,1);
//如果有日期型的列,需要设置DateFormat并放入HashMap中
h.put("DateFormat","yyyy-MM-dd HH:mm:ss");
//执行导入工作
new ImportFileEntry().importFile("导入的文件路径及名称.csv","YFJBXX_TEST",false,"ImportDirectImpl",h);
ImportFileEntry()的importFile方法说明:
/**
* 从文件导入到指定表中
* @param fileName String 要导入文件名
* @param tableName String 导入目标表名
* @param firstLineRead boolean 是否读取第一行
* @param dealClass 处理类名称(例如配置文件中Bean name="ImportDirectImpl")
* @param aHashMap 扩展用,需特殊处理时可置入变量
* @throws Exception
*/
public void importFile(String fileName, String tableName, boolean firstLineRead,String dealClass,HashMap aHashMap) throws Exception ;
至此,一个简单的不需做任何处理直接从文件导入数据库对应表的功能就实现了。
但是有些时候我们需要进行特殊的处理,例如表中的当前操作日期列在导入文件中没有,需要在代码中加入,这时就需要实现FileImportInterface接口并加入到配置文件中例如:<Bean name="ImportWISImpl" class="fileimport.ImportWISImpl"/>,ImportWISImpl的实现代码见后续代码清单。
t;);
columnListInfoCode[i][0] = columnName;
columnListInfoCode[i][1] = columnType;
System.out.println("Code columnName:" + columnName + " columnType:" + columnType);
}
}
//生成实例
ImportTableInfoBO importAction = new ImportTableInfoBO();
importAction.setTableName(tableName);
importAction.setColumnNamesFile(columnListInfoFile);
if (columnListInfoCode != null) {
importAction.setColumnNamesCode(columnListInfoCode);
}
//放入静态容器中
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();
//---写入数据库待加入----
//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;
}
}
}
八、调用示例
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";