数据库连接类:
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();
}
}
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();
}
}