天天看点

JAVA dbf的解析

           作为一个什么都不懂的JAVA菜中菜,第一个有挑战性的任务是做dbf的解析。

           dbf文件在做这个项目之前从来都没有接触过,也从来都没有听说过,应该是一个比较老的东西了。无从下手的我,只能先实现了一个普通文件的上传功能,能够把一个dbf文件上传到了服务器上指定的文件夹中。

           代码部分完全摘自其他大神,出处我忘了是哪里了,dbf解析采用了通用的方法,用了dbf的解析的jar包,之后将读取出的数据插入进数据库中,这部分自己写的,写得很菜,现在的水平能够实现功能已经很不错了,完全不会考虑代码的一些可读性还有效率等。这里粘一下主要的代码:

public static void main(String[] arg){

    Connection con;

    String driver = "com.mysql.jdbc.Driver";

    String url = "jdbc:mysql://localhost:3306/store";

    String user ="root";

    String password ="1234";

    try{

        Class.forName(driver);

        con = DriverManager.getConnection(url, user, password);

        if(!con.isClosed()){

            System.out.println("Succeeded connecting to the Database");

            //Statement state = con.createStatement();

            String path ="E:\\yue\\Documents\\河北2012\\河北全\\t_jhk.dbf";

           InputStream fis = new FileInputStream(path);

           DBFReader reader = new DBFReader(fis);

           reader.setCharactersetName("GBK");

           int fieldsCount = reader.getFieldCount();

                    //System.out.println("字段数:"+fieldsCount);

                    DBFField[] df = new DBFField[fieldsCount+2];

                    for( int i=0; i<fieldsCount; i++)   

                    {  

                      df[i] = reader.getField(i);

                      //System.out.println("field"+i+":"+df[i].getName());

                    }

                    Object[] rowValues;

                    String t_record = null;

                    ArrayList record = new ArrayList();

                    int a = 0;

                    //一条条取出path文件中记录  

                    while((rowValues = reader.nextRecord()) != null)

                    {  

                      for( int i=0; i<rowValues.length; i++)

                      {                         

                        t_record=rowValues[i].toString();

                        record.add(i, t_record);

                      //System.out.println(record);

                      }

                      a=a+rowValues.length;

                    }  

//                    for( int i=0; i<record.size(); i++)

//                      {                         

//                        System.out.println(record.get(i));

//                      }  

                  System.out.println("OVER");

        for(int j=0;j<reader.getRecordCount();j++){          

        String sql_field = "insert into jhk values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

            PreparedStatement state = con.prepareStatement(sql_field);        

            int t=1;

            for(int i=j*19;i<(j+1)*19;i++){                    

            state.setString(t,(String) record.get(i));

            t=t+1;

            }

            state.executeUpdate();

        }

            con.close();

        }

    }

    catch (SQLException e){

        e.printStackTrace();

    }catch (Exception e){

        e.printStackTrace();

    } finally{

        System.out.println("数据库数据成功获取");

    }

}

如果有什么需要的资源,可以评论或者私聊。