java连接DBF

来源:互联网 发布:oracle批量update数据 编辑:程序博客网 时间:2024/06/02 22:56

数据库连接类:

package dbfconnection;

import java.sql.*;

public class connection {

    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://Program Files//Microsoft Visual Studio//Vfp98//"; 

/*

       "C://Program Files//Microsoft Visual Studio//Vfp98//"   为dbf文件的路径

*/
            con = DriverManager.getConnection(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String sql = "select * from info";
        st = con.createStatement();
        rs = st.executeQuery(sql);
        while (rs.next()) {
            String str = "Name:" + rs.getString("Name") + "/tAddress:" + rs.getString("Address") + "/tAge:" + rs.getInt("Age");
            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();
        }
    }
}

测试代码:

package dbfconnection;

import java.io.IOException;
import java.sql.*;

public class Main {

    public static void main(String[] args) throws SQLException, IOException {
        System.out.println("输出:");
        connection cont = new connection();
        cont.Close();
    }
}

原创粉丝点击