JAVA 操作 DBF 文件数据库
1、依赖夹包
javadbf-0[1].4.1.jar jconn3.jar
2、添加属性文件 jdbc.properties
jdbc.driverClassName=com.sybase.jdbc3.jdbc.SybDriver jdbc.url=jdbc:sybase:Tds:ip:port/dbname jdbc.username=sa jdbc.password=******
3、实现代码
package com.XXX.weitedbf.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import com.linuxense.javadbf.DBFField; import com.linuxense.javadbf.DBFWriter; /** * @author zsh * 时间:2015-01-05 * 描述:将数据库数据 备份为dbf文件 */ public class WriteDBF { private static final String LINE = "\r\n"; //换行符 private static final String TAB = "\t"; //制表符 private static String url; private static String driveName; private static String username; private static String password; private static final boolean printSql = true; static{ Properties p = new Properties(); try { InputStream in = WriteDBF.class.getResourceAsStream("/jdbc.properties"); p.load(in); in.close(); if(p.containsKey("jdbc.driverClassName")){ driveName = p.getProperty("jdbc.driverClassName"); } if(p.containsKey("jdbc.url")){ url = p.getProperty("jdbc.url"); } if(p.containsKey("jdbc.username")){ username = p.getProperty("jdbc.username"); } if(p.containsKey("jdbc.password")){ password = p.getProperty("jdbc.password"); } } catch (IOException ex) { Logger.getLogger(WriteDBF.class.getName()).log(Level.SEVERE, null, ex); } } //传入参数 // "table=aa;where=\ and\ YEAR\ ='2014';path=E:\\xxxx.DBF" public static void main(String[] args) { try { if(args.length < 1){ System.out.println("参数传入错误......."); return; } String value = args[0]; String[] values = value.split(";"); String tableName = values[0].substring(values[1].indexOf("=") + 1); String filte = values[1].substring(values[1].indexOf("=") + 1).replace("\\", ""); String path = values[2].substring(values[1].indexOf("=")); System.out.println("开始启动连接......."); Connection conn = getConnection(); System.out.println("数据连接成功......."); //数据库查询数据 //gb //ba //n_wt401_2014_export Map map = getData(conn, tableName, filte); System.out.println("开始导出数据......."); List<Map> fieldList = new ArrayList<Map>(); List<Map> dataList = new ArrayList<Map>(); fieldList = (List<Map>) map.get("fieldList"); dataList = (List<Map>) map.get("dataList"); //写DBF文件 writeDBF(fieldList, dataList, path); System.out.println("导出完成!文件路径:" + path); } catch (Exception e) { System.out.println("数据库连接失败......" + LINE + e.getMessage()); } } // 获取数据库连接 public static Connection getConnection() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException { try { Class.forName(driveName).newInstance(); Properties sysProps = System.getProperties(); sysProps.put("user", username); // 设置数据库访问用户名 sysProps.put("password", password); // 密码 sysProps.put("charset", "cp936"); return DriverManager.getConnection(url, sysProps); } catch (InstantiationException e) { throw e; } catch (IllegalAccessException e) { throw e; } } /** * @描述:dbf 文件操作 * @author * @param fieldList * 表头集合 * @param dataList * 数据集合 * @param path * 存放路径 */ @SuppressWarnings("rawtypes") private static void writeDBF(List<Map> fieldList, List<Map> dataList, String path) { OutputStream fos = null; try { // 定义DBF文件字段 DBFField[] fields = new DBFField[fieldList.size()]; int row = 0; for (Map map : fieldList) { // 表头 String name = map.get("name").toString(); if (name.length() >= 10)//字段名长度不能超过10 name = name.substring(0, 9); int length = Integer.parseInt(map.get("length").toString()); fields[row] = new DBFField(); fields[row].setName(name); fields[row].setDataType(DBFField.FIELD_TYPE_C); fields[row].setFieldLength(length); row++; } // 定义DBFWriter实例用来写DBF文件 DBFWriter writer = new DBFWriter(); // 把字段信息写入DBFWriter实例,即定义表结构 writer.setFields(fields); row = 0; int size = dataList.size(); int nowRow = 0; for(Map map : dataList){ Object[] rowData = new Object[fieldList.size()]; for(Map fMap : fieldList){ Object key = fMap.get("name"); rowData[row] = map.get(key)==null?null: map.get(key).toString(); row++; } row = 0; nowRow++; if(nowRow % 10 == 0) System.out.println("当前进度 : " + nowRow +"/" + size); writer.addRecord(rowData); } File file = new File(path); fos = new FileOutputStream(file); // 写入数据 writer.write(fos); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (Exception e) { } } } /** * @描述: 获取数据数据库数据和表头字段 * @param connection * 数据库链接 * @param tableName * 表名 * @param filte * 查询条件 例如: and a=a and b>1 ... * @return map ["fieldList" 字段名,"dataList" 数据集合] * @throws SQLException */ @SuppressWarnings("rawtypes") private static Map getData(Connection connection, String tableName, String filter) throws SQLException { String sql = "select * from " + tableName + " where 1 = 1"; if (filter != null && !"".equals(filter)) sql += filter; if (printSql) System.out.println("sql : " + sql); PreparedStatement ps = null; ResultSet rs = null; ps = connection.prepareStatement(sql); rs = ps.executeQuery(); ResultSetMetaData md = rs.getMetaData(); int columnCount = md.getColumnCount(); List<Map> fieldList = new ArrayList<Map>();// 表头集合 for (int i = 1; i <= columnCount; i++) { Map map = new HashMap(); map.put("name", md.getColumnName(i)); map.put("length", md.getColumnDisplaySize(i)); map.put("type", (byte) md.getColumnType(i)); fieldList.add(i - 1, map); } List dataList = new ArrayList(); while (rs.next()) {// 数据集合 Map dataMap = new HashMap(); for (Map map : fieldList) { String name = map.get("name").toString(); dataMap.put(name, rs.getObject(name)); } dataList.add(dataMap); } Map retMap = new HashMap(); retMap.put("fieldList", fieldList); retMap.put("dataList", dataList); if (rs != null) rs.close(); if (ps != null) ps.close(); if (connection != null) connection.close(); return retMap; } }
4、打包后执行jar文件批处理(可显示打印控制台信息)
java -jar dbf.jar "aa;where=\ and\ YEAR\ ='2014';path=E:\\aa.DBF" pause;