dbf文件sql驱动 工具

dbf文件简介

dbf 文件的接收和使用工具可以参考

dbf jdbc 驱动简介

目前市面上有的dbf jdbc驱动都是商业软件,需要收费,所以自己动手写了一个简单的dbf jdbc驱动,常见的商业收费dbf jdbc驱动主要有:

  • Caigen DBF
    http://www.caigen.com/index.html 是一款商业的dbf文件驱动
  • StelsDBF
    https://dbf-jdbc.smartcode.com/ 也是一款商业的dbf文件驱动

自研dbf jdbc驱动开发的思路

主要的思路是通过解析sql语句,然后遍历dbf文件,对从dbf文件中读取的记录进行sql语句中条件处理,如下图所示:
dbf文件sql驱动 工具_第1张图片
设计思路

sql解析工具

主要使用的是JSqlParser,这个工具解析sql非常容易理解,代码风格一致,主要是使用了设计模式中的访问者模式;

dbf文件读取工具

使用的是[# dans-dbf-lib],对其进行了进一步的封装。

使用简介

自研dbf jdbc驱动查询代码式例

package com.framework.yjk.test;

import com.framework.yjk.DbfJdbcDriver;
import com.framework.yjk.DbfStatement;

import java.io.PrintWriter;
import java.sql.*;

/**
 * @author yujiakui
 * @version 1.0
 * Email: [email protected]
 * date: 2018/9/11 18:32
 * description:
 **/
public class TestSelectMain {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        DriverManager.setLogWriter(new PrintWriter(System.out, true));
        //Class.forName("com.haiyi.framework.dbf.DbfJdbcDriver");
        DbfJdbcDriver.registDriver();
        Connection connection = DriverManager.getConnection("jdbc:yjk:dbf:src/test/resources");
        DbfStatement statement = (DbfStatement) connection.createStatement();
        /*String query = "SELECT rec_num as A  FROM zq_order where rec_num = 2000000 and  STOCK_CODE = '500500' LIMIT 1";*/
        String query = "SELECT *  FROM zq_order where rec_num = 3 ";
        statement.execute(query);
        ResultSet resultSet = statement.getResultSet();
        System.out.println(resultSet);
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int colNum = resultSetMetaData.getColumnCount();

        int recordNum = 0;
        while (resultSet.next()) {
            for (int i = 1; i <= colNum; i++) {
                System.out.println("列名" + resultSetMetaData.getColumnName(i) + " : value " + resultSet.getObject(i));
            }
            System.out.println("-----------");
            recordNum++;
        }

        System.out.println("----记录条数---" + recordNum);
    }
}

自研dbf jdbc驱动插入代码式例

package com.framework.yjk.test;

import com.framework.yjk.DbfJdbcDriver;
import com.framework.yjk.DbfStatement;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author yujiakui
 * @version 1.0
 * Email: [email protected]
 * date: 2018/9/11 18:32
 * description:
 **/
public class TestInsertMain {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        DriverManager.setLogWriter(new PrintWriter(System.out, true));
        //Class.forName("com.haiyi.framework.dbf.DbfJdbcDriver");
        DbfJdbcDriver.registDriver();
        Connection connection = DriverManager.getConnection("jdbc:yjk:dbf:src/test/resources");
        DbfStatement statement = (DbfStatement) connection.createStatement();
        String query = "insert into zq_order(REC_DATE,OB_SETTLE,REC_NUM,STOCK_CODE,WITHDRAW,STATUS,S_QTY,\n" +
                "PRODUCT,B_QTY,REC_TIME,SETTLE,FIRM,ACC,OB_COMPANY,OB_ACC,ORDER_TYPE,STOCK_NAME,B_PRICE,\n" +
                "TRADE_TYPE,DIR,OB_FIRM,OB_TRADER,COMPANY,S_PRICE,ICE_QTY,TRADER,NICKED,REF_ID)\n" +
                "values('20180628', null, 20243000, '500500',null ,'U', null, '01', 1, '180013',null , \n" +
                "'88839', 'D890008091', null,null , '1', null, 100, null, 'B',null , null, '277',null, \n" +
                "0, null, 'N', '1551030189')";
        statement.execute(query);

        System.out.println("----插入---");
    }
}

自研dbf jdbc驱动删除代码式例

package com.framework.yjk.test;

import com.framework.yjk.DbfJdbcDriver;
import com.framework.yjk.DbfStatement;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author yujiakui
 * @version 1.0
 * Email: [email protected]
 * date: 2018/9/12 8:35
 * description:
 **/
public class TestDeleteMain {

    public static void main(String[] args) throws SQLException {
        DriverManager.setLogWriter(new PrintWriter(System.out, true));
        //Class.forName("com.haiyi.framework.dbf.DbfJdbcDriver");
        DbfJdbcDriver.registDriver();
        Connection connection = DriverManager.getConnection("jdbc:yjk:dbf:src/test/resources");
        DbfStatement statement = (DbfStatement) connection.createStatement();
        String query = "delete from zq_order where rec_num = 2";
        statement.execute(query);

    }
}

自研dbf jdbc驱动更新代码式例

package com.framework.yjk.test;

import com.framework.yjk.DbfJdbcDriver;
import com.framework.yjk.DbfStatement;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author yujiakui
 * @version 1.0
 * Email: [email protected]
 * date: 2018/9/12 8:56
 * description:
 **/
public class TestUpdateMain {

    public static void main(String[] args) throws SQLException {
        DriverManager.setLogWriter(new PrintWriter(System.out, true));
        //Class.forName("com.haiyi.framework.dbf.DbfJdbcDriver");
        DbfJdbcDriver.registDriver();
        Connection connection = DriverManager.getConnection("jdbc:yjk:dbf:src/test/resources");
        DbfStatement statement = (DbfStatement) connection.createStatement();
        String query = "update zq_order set order_type = 2 where rec_num = 6";
        statement.execute(query);

    }
}

源码地址

源码地址:https://github.com/lwjaiyjk/DbfJdbcDriver

参考

  1. https://github.com/jprante/jdbc-driver-csv

你可能感兴趣的:(dbf文件sql驱动 工具)