java读取dbf文件

1.下载DBFReader jar包

2.实例代码

package service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

import util.DBUtil;

import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFReader;

public class DBFUtil{
    private static final String InsertSql = "insert into V_SSMZ (YE,NO,DP,E0,E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13,E14,E15,E16,E17,E18,E19) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    private static final String UpdateSql = "update V_SSMZ set ye=?,no=?,dp=?,e0=?,e1=?,e2=?,e3=?,e4=?,e5=?,e6=?,e7=?,e8=?,e9=?,e10=?,e11=?,e12=?,e13=?,e14=?,e15=?,e16=?,e17=?,e18=?,e19=? where ye = ? and no= ?";
    
    public static void main (String[] args){
        DBFUtil sdf = new DBFUtil ();
        try{
            sdf.DBFReader ("V_SSMZ", "D:\\常用软件\\jar包\\ssbase.DBF");
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
    public void DBFReader(String tableName,String filePath) throws SQLException{
        Set<String> map = new HashSet <String> ();
        DBFReader reader=null;//从dbf中获取内容 
        Connection con = null;
        PreparedStatement Insertstat = null;
        PreparedStatement Updatestat = null;
        InputStream in;
        try{
            in = new FileInputStream(new File(filePath)); 
            reader=new DBFReader(in);//将文件从文件流中读入。  
            
            Object[] rowObjects = null;
            
            con = DBUtil.getPlantDBConnection ();
            Insertstat = con.prepareStatement (InsertSql);
            Updatestat = con.prepareStatement (UpdateSql);
            while ((rowObjects = reader.nextRecord()) != null) {
                String ye = new String(rowObjects[0].toString ().trim ().getBytes ("ISO-8859-1"),"GBK");
                String no = new String(rowObjects[1].toString ().trim ().getBytes ("ISO-8859-1"),"GBK");
                if(map.contains (ye+"_"+no)){
                    for(int i=0;i<rowObjects.length;i++){
                        String value = rowObjects[i]==null?"":new String(rowObjects[i].toString ().trim ().getBytes ("ISO-8859-1"),"GBK");
                        Updatestat.setString (i+1, value);
                    }
                    Updatestat.setString (rowObjects.length+1, new String(rowObjects[0].toString ().trim ().getBytes ("ISO-8859-1"),"GBK"));
                    Updatestat.setString (rowObjects.length+2, new String(rowObjects[1].toString ().trim ().getBytes ("ISO-8859-1"),"GBK"));
                    
                    Updatestat.addBatch ();
                }else{
                    for(int i=0;i<rowObjects.length;i++){
                        String value = rowObjects[i]==null?"":new String(rowObjects[i].toString ().trim ().getBytes ("ISO-8859-1"),"GBK");
                        Insertstat.setString (i+1, value);
                    }
                    Insertstat.addBatch ();
                }
                map.add (ye+"_"+no);
            }
            Insertstat.executeBatch ();
            Updatestat.executeBatch ();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (DBFException e){
            e.printStackTrace();
        }catch (SQLException e){
            e.printStackTrace();
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }finally{
            Updatestat.close ();
            Updatestat.close ();
            con.close ();
        }
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。