Java parsing dbf file

Source: Internet
Author: User
Tags getmessage

dbf file, cannot be saved directly as an Excel file, will lose data! You need to store the DBF data in a database and export the data to Excel from the database.

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 parsing dbf file , import data into database table , need javadbf. 4.1.jar Package

* @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:/University code . DBF")); //Take dbf file as program argument

Dbfreader reader = new Dbfreader (inputstream);

for (int i = 0; i < Reader.getrecordcount (); i++) { //all record lines

Object obj[] = Reader.nextrecord ();

map<string, object> map = new hashmap<string, object> ();

for (int j = 0; J < obj.length; j + +) data for all fields in {//a row

Dbffield field = Reader.getfield (j);

Map.put (Field.getname (), objectutils.  Defaultifnull(Obj[j], ""). ToString (). Trim ()); //column and value

}

List.add (map);

}

Inputstream.close ();

//Get data into the library

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 (Tempmap.get ("YXLXR"));

Pstmt.setobject (one, Tempmap.get ("YXYZBM"));

Pstmt.setobject (Tempmap.get ("FRHM"));

Pstmt.setobject (Tempmap.get ("EMAIL"));

Pstmt.setobject (Tempmap.get ("HTTP"));

Pstmt.setobject (Tempmap.get ("YXFLDM"));

Pstmt.setobject (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);

}

}

/**

* Import DBF data

* @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);

Read all data in DBF and deposit to list

for (int i = 0; i < Reader.getrecordcount (); i++) {//all record rows

Object obj[] = Reader.nextrecord ();

map<string, object> map = new hashmap<string, object> ();

for (int j = 0; J < Obj.length; J + +) {//data for all fields in a row

Dbffield field = Reader.getfield (j);

Map.put (Field.getname (), Objectutils.defaultifnull (Obj[j], ""). ToString (). Trim ()); Column and value

}

List.add (map);

}

for (int c=0;c<reader.getfieldcount (); C + +) {//Get all the fields in DBF for generating SQL

Dbffield field = Reader.getfield (c);

Cols.add (Field.getname ());

}

Filestream.close ();

Adding records to the JY_BYSXX_SRC table

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 batch Once

Pstmt.executebatch ();

}

}

if (List.size ()%500!=0) Pstmt.executebatch (); If there is a remainder, the remainder is executed in bulk.

Conn.commit ();

} catch (Exception e) {

Connectionpool.rollback (conn);

Importflag = false;

Info.append (E.getmessage ());

} finally{

Connectionpool.close (conn);

Connectionpool.closepstmt (PSTMT);

}

return importflag;

}

Java parsing dbf file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.