码迷,mamicode.com
首页 > 数据库 > 详细

java读取dbf文件

时间:2015-04-23 15:38:16      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:

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 ();
        }
    }
}

 

java读取dbf文件

标签:

原文地址:http://www.cnblogs.com/staryy/p/4450497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!