日期:2014-05-20 浏览次数:21252 次
java对vfp数据库的操作
--------------------------------------------
一:创建新的ODBC数据资源
1.windows2000的控制面板-》管理工具-》数据源(ODBC),出现下面的对话框,在对话框的”用户DSN”:选择“Visual FoxPro Database”的用户数据源,单击“添加”按纽。
2.在出现的对话框里,选择使用“Microsoft Visual Foxpro Driver”驱动程序来安装数据资源,单击“完成”按纽。
3.在弹出来的答话框,我们设置“Data Source Name”为JDBCDemo.java,“DataBase type”项为“Free Table directory”,然后在“path:”项指定JDBCDemo.java所在的目录为ODBC数据资源的目录。再选择”Options“,把Fetch data om background选上。Collating sequence: Machine
2.JDBCDemo.java
/*
* @(#)JDBCDemo.java 2003/9/29
*/
import java.sql.*;
/**
* 演示JDBC操作数据库的各项功能,包括表的创建
* (CREATE)和删除(DROP),记录的插入(INSERT),
* 选择(SELECT)和更改(UPDATE)等操作 。
*/
public class JDBCDemo
{
public static void main(String args[])
{
try {
Statement stmt;
PreparedStatement pstmt;
ResultSet rs;
// 加载 jdbc-odbc 桥驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//定义JDBC URL
String url = "jdbc:odbc:JDBCDemo";
//得到与数据库的连接
Connection con DriverManager.getConnection (url);
//显示URL和连接信息
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
//得到一个Statement对象
stmt = con.createStatement();
//如果表DemoTable已经存在,则删除之,否则,抛掷一个异常
System.out.println("DROP TABLE DemoTable, if it exists.");
try{
stmt.executeUpdate("DROP TABLE DemoTable");
}catch(Exception e){
System.out.print(e);
System.out.println("No existing table to delete");
}
//在数据库中创建一个表DemoTable
stmt.executeUpdate("CREATE TABLE DemoTable ("+ "test_id int,test_val char(15) not null)");
System.out.println("table DemoTable created!");
//在表中插入一些值
stmt.executeUpdate("INSERT INTO DemoTable ("+ "test_id, test_val) VALUES(1,'One')");
stmt.executeUpdate("INSERT INTO DemoTable ("+ "test_id, test_val) VALUES(2,'Two')");
stmt.executeUpdate("INSERT INTO DemoTable ("+ "test_id, test_val) VALUES(3,'Three')");
stmt.executeUpdate("INSERT INTO DemoTable ("+ "test_id, test_val) VALUES(4,'Four')");
stmt.executeUpdate("INSERT INTO DemoTable ("+ "test_id, test_val) VALUES(5,'Five')");
//得到另一个Statement对象
stmt = con.createStatement();
//查询数据库中的表DemoTable,得到以test_id排序后的所有记录,
//并存储在ResultSet对象rs中
rs = stmt.executeQuery("SELECT * from DemoTable ORDER BY test_id");
//显示表DemoTable中的所有记录
System.out.println("Display all results:");
while(rs.next())
{
int theInt= rs.getInt("test_id");
String str = rs.getString("test_val");
System.out.println("\ttest_id= " + theInt + "\tstr = " + str);
}
// 创建已准备好的语句,更新“DemoTable”表中
// 某条记录的test_val字段。
// 已准备好的语句接受两个参数。
pstmt = con.prepareStatement("UPDATE DemoTable SET test_val = ? WHERE test_id = ?");
//更改表DemoTable中的第2条记录的test_val字段的值
// 充填UPDATE语句中的“?”,并执行UPDATE语句
pstmt.setString(1, "Hello!");//就是说第一个参数test_val为Hello!
pstmt.setInt(2, 2);//就是说第二个参数test_id为2
pstmt.executeUpdate();
System.out.println("Update row number 2: OK.");
//显示表DemoTable中更新后的第2条记录
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * from DemoTable ORDER BY test_id");
System.out.println("Display row 2:");
if (rs.next() && rs.next())
{
int theInt= rs.getInt("test_id");
String str = rs.getString("test_val");
System.out.println("\ttest_id= " + theInt + "\tstr = " + str);}
con.close(); //关闭与数据库的连接
}catch( Exception e ) {
e.printStackTrace();
}
}
}
在windows2000+jdk1.3+Visual Foxpro 6.0上通过测试。
以下小丑修正:
以上代码是正确的,我测试了一下.只有一句:
*********************************
//得到与数据库的连接
Connection con DriverManager.getConnection (url);
********************************
掉了个=;我写了个简单的测试与数据库的连接.(ODBC的建立照以上,这里我取名为OdbcJava).这里在JDK1.5.0_07下测试通过.
import java.sql.*;
public class Jdbc {
public static void main(String args[]) {
Connection con;
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:OdbcJava";
try{
Class.forName(driver);
}catch(Exception e){
System.out.println("ODBC数据源连接错误:"+e.toString());
}
try{
con=DriverManager.getConnection(url);
}catch(Exception e){
System.out.println("VFP数据库连接错误:"+e.toString());
}
System.out.println("OK..VFP数据库连接成功!");
}
}