java解析dbf文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java解析dbf文件相关的知识,希望对你有一定的参考价值。
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.ObjectUtils;
import cn.com.wfc.db.ConnectionPool;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
/**
* java解析dbf文件,将数据导入数据库表,需要javadbf.4.1.jar包
* @author LiChuntao
*/
public class JavaDBFReaderTest {
public static void main(String args[]) {
Connection conn = null;
PreparedStatement pstmt = null;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
InputStream inputStream = new FileInputStream(new File(
"F:/院校代码.dbf")); // take dbf file as program argument
DBFReader reader = new DBFReader(inputStream);
for (int i = 0; i < reader.getRecordCount(); i++) { // 所有记录行数
Object obj[] = reader.nextRecord();
Map<String, Object> map = new HashMap<String, Object>();
for (int j = 0; j < obj.length; j++) { // 一行中所有字段的数据
DBFField field = reader.getField(j);
map.put(field.getName(), ObjectUtils.defaultIfNull(obj[j], "").toString().trim()); // column和 value
}
list.add(map);
}
inputStream.close();
//将获取的数据入库
String sql = "insert into abcdefg(YXDM,YXMC,DM1,DM2,YXDH,YXSZDQDM,YXDZ,YXLSDM,BLS,YXLXR," +
"YXYZBM,FRHM,EMAIL,HTTP,YXFLDM,DZB)" +
" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
conn = ConnectionPool.getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
for(int m=0;m<list.size();m++){
Map<String,Object> tempMap = list.get(m);
pstmt.setObject(1, tempMap.get("YXDM"));
pstmt.setObject(2, tempMap.get("YXMC"));
pstmt.setObject(3, tempMap.get("DM1"));
pstmt.setObject(4, tempMap.get("DM2"));
pstmt.setObject(5, tempMap.get("YXDH"));
pstmt.setObject(6, tempMap.get("YXSZDQDM"));
pstmt.setObject(7, tempMap.get("YXDZ"));
pstmt.setObject(8, tempMap.get("YXLSDM"));
pstmt.setObject(9, tempMap.get("BLS"));
pstmt.setObject(10, tempMap.get("YXLXR"));
pstmt.setObject(11, tempMap.get("YXYZBM"));
pstmt.setObject(12, tempMap.get("FRHM"));
pstmt.setObject(13, tempMap.get("EMAIL"));
pstmt.setObject(14, tempMap.get("HTTP"));
pstmt.setObject(15, tempMap.get("YXFLDM"));
pstmt.setObject(16, tempMap.get("DZB"));
pstmt.executeUpdate();
}
conn.commit();
System.out.println("success");
} catch (Exception e) {
ConnectionPool.rollback(conn);
System.out.println(e.getMessage());
} finally{
ConnectionPool.close(conn);
ConnectionPool.closePstmt(pstmt);
}
}
/**
* 导入dbf数据
* @param user
* @param xxid
* @param fileStream
* @param info
*/
public boolean importXxxxByDbf(Login user, String xxid, InputStream fileStream,
StringBuffer info) {
boolean importFlag = true;
Connection conn = null;
PreparedStatement pstmt = null;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
List<String> cols = new ArrayList<String>();
try {
DBFReader reader = new DBFReader(fileStream);
//读取dbf中所有数据,并存入到list中
for (int i = 0; i < reader.getRecordCount(); i++) { // 所有记录行数
Object obj[] = reader.nextRecord();
Map<String, Object> map = new HashMap<String, Object>();
for (int j = 0; j < obj.length; j++) { // 一行中所有字段的数据
DBFField field = reader.getField(j);
map.put(field.getName(), ObjectUtils.defaultIfNull(obj[j], "").toString().trim()); // column和 value
}
list.add(map);
}
for(int c=0;c<reader.getFieldCount();c++){ //获取dbf中所有字段,用于生成sql
DBFField field = reader.getField(c);
cols.add(field.getName());
}
fileStream.close();
//向jy_bysxx_src表中添加记录
String inSql = this.createInsertSql(cols, "jy_bysxx_src");
conn = ConnectionPool.getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(inSql);
for(int m=0;m<list.size();m++){
Map<String, Object> map = list.get(m);
for(int n=0;n<cols.size();n++){
pstmt.setObject(n+1,map.get(cols));
}
pstmt.addBatch();
if(m%500==0){ //500条批量一次
pstmt.executeBatch();
}
}
if(list.size()%500!=0) pstmt.executeBatch(); //存在余数,则批量执行余数.
conn.commit();
} catch (Exception e) {
ConnectionPool.rollback(conn);
importFlag = false;
info.append(e.getMessage());
} finally{
ConnectionPool.close(conn);
ConnectionPool.closePstmt(pstmt);
}
return importFlag;
}
以上是关于java解析dbf文件的主要内容,如果未能解决你的问题,请参考以下文章