java操作dbf文件

来源:互联网 发布:sql一对多查询join 编辑:程序博客网 时间:2024/05/07 20:09



今天才知道原来java可以直接读取dbf文件,不用附加到数据库了

遇到不少问题 mark一下
1),我是win7系统,提示 [Microsoft][ODBC 驱动程序管理器] 驱动程序不支持此功能” 需要把vfpodbc.dll降级版本 , 大小为955K,版本6.1.8630.1 放到C:\Windows\System32
下载地址百度一下都有
2)、url的db路径为dbf 文件存放的路径,不要指向dbf文件  select * from xx为该文件的名
3)、遇到个问题不知道为啥 System.out.println(rs.getString(column));报异常,no data found 但是 String str=rs.getString(column); 然后打印str 就不报错了 不知道为啥?


贴出代码:


import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * 如果提示 [Microsoft][ODBC 驱动程序管理器] 驱动程序不支持此功能” * 更新vfpodbc.dll, 大小为955K,版本6.1.8630.1 放到C:\Windows\System32 * url的db路径为dbf文件存放的路径 select * from xx为该文件的名 */public class connection { public static void main(String[] args) throws SQLException, IOException {        System.out.println("输出:");        connection cont = new connection();        cont.Close();    }    Connection con = null;    Statement st = null;    ResultSet rs = null;    public connection() throws SQLException {        getConnection();    }    public Connection getConnection() throws SQLException {        try {            String url = "jdbc:odbc:driver={Microsoft Visual FoxPro Driver};" +                    "SourceType=DBF;SourceDB=" +                    "C://";             con = DriverManager.getConnection(url);        } catch (Exception e) {            e.printStackTrace();        }               String sql = "select * from get where A0='基金资产净值:'";        st = con.createStatement();        rs = st.executeQuery(sql);        while (rs.next()) {            String str = rs.getString("A4");            System.out.println(str);        }        return con;    }    public void Close() {        try {            if (rs != null) {                rs.close();            }            if (st != null) {                st. close();            }            if (con != null) {                con.close();            }        } catch (SQLException e) {            e.printStackTrace();        }    }}