import java.sql.*
import java.io.IOException
//数据库连接类:
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=DBFSourceDB=" +
"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()
}
}
//测试代码:
public static void main(String[] args) throws SQLException, IOException {
System.out.println("输出:")
connection cont = new connection()
cont.Close()
}
}
java直接访问DBF需要第三方的JDBC驱动支持,DBF的第三方JDBC驱动要收费的,在windows系统下也可以在ODBC里面建DSN,然后用JDBC-ODBC桥连接ODBC的DSN,这样就可以正常访问和使用DBF数据库了,包括增删查改 *** 作,也可以用新建数据表的方式实现导出数据欢迎分享,转载请注明来源:内存溢出
评论列表(0条)