使用ODBC从Java读取Visual Foxpro数据

我试图从我的Java应用程序查询dbf表。 我参考了这个post

我使用ODBC数据源管理器创建了一个系统数据源,我将数据源名称设置为VFPDS,并将数据库类型设置为.DBC,最后设置.dbc文件的路径。

以下是我的java代码:

import javax.swing.* ; import java.awt.* ; import java.awt.event.* ; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.Statement; // Import custom library containing myRadioListener import java.sql.DriverManager; public class testodbc { public static void main( String args[] ) { try { // Load the database driver Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ; // Get a connection to the database Connection conn = DriverManager.getConnection( "jdbc:odbc:VFPDS" ) ; // Print all warnings for( SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning() ) { System.out.println( "SQL Warning:" ) ; System.out.println( "State : " + warn.getSQLState() ) ; System.out.println( "Message: " + warn.getMessage() ) ; System.out.println( "Error : " + warn.getErrorCode() ) ; } // Get a statement from the connection Statement stmt = conn.createStatement() ; // Execute the query ResultSet rs = stmt.executeQuery( "SELECT * FROM pmsquoteh" ) ;//code crashes here // Loop through the result set while( rs.next() ) System.out.println( rs.getString(1) ) ; // Close the result set, statement and the connection rs.close() ; stmt.close() ; conn.close() ; } catch( SQLException se ) { se.printStackTrace(); System.out.println( "SQL Exception:" ) ; // Loop through the SQL Exceptions while( se != null ) { se.printStackTrace(); System.out.println( "State : " + se.getSQLState() ) ; System.out.println( "Message: " + se.getMessage() ) ; System.out.println( "Error : " + se.getErrorCode() ) ; se = se.getNextException() ; } } catch( Exception e ) { System.out.println( e ) ; } } } 

我有这个例外:

 java.sql.SQLException: [Microsoft][ODBC Visual FoxPro Driver]Not a table. at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source) at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source) at UsingButtons.main(testodbc.java:38) SQL Exception: State : S0002 Message: [Microsoft][ODBC Visual FoxPro Driver]Not a table. Error : 123 

我确信表pmsquoteh退出数据库,我的机器是64位机器。 我究竟做错了什么 ? 我将很感激具体的答案。

我能够在Windows 7上使用jdbc-odbc桥访问FoxPro表,但它花了很多步。 我已经安装了VFP驱动程序,我不记得我从哪里下载它,所以我没有链接。

我从这里复制了jbdc:odbc示例中的代码: http : //www.java2s.com/Code/Java/Database-SQL-JDBC/SimpleexampleofJDBCODBCfunctionality.htm 。

DriverManager.getConnection方法采用数据库URL。 要创建URL,您需要使用ODBC管理器。 不幸的是,我可以通过控制面板访问的ODBC管理器仅适用于64位数据源。 我不知道64位foxpro驱动程序。

要生成32位DSN,您需要运行32位ODBC管理器:odbcad32.exe。 我的机器有几个副本。 我从C:\ Windows \ SysWOW64运行了一个。 转到系统DSN选项卡,然后选择Microsoft Visual Foxpro驱动程序。 单击完成后,您将看到一个对话框,询问FoxPro数据库或表的数据源名称,描述和路径。 您还必须指定是否要连接到.dbc或空闲​​表目录。 您的错误消息让我想知道您的DSN上是否选择了错误的选项。

我传入getConnection方法的数据库URL是“jdbc:odbc:mydsnname”。

运行后,我收到一个错误:

[Microsoft] [ODBC驱动程序管理器]指定的DSN包含驱动程序和应用程序之间的体系结构不匹配

这是因为我使用的是64位JVM和32位DSN。 我下载了一个32位的JVM,并能够用它来运行我的示例类。

我不知道是否有一个可以在64位JVM上设置的开关,告诉它以32位运行。

我从这里使用了JDBC驱动程序:

http://www.csv-jdbc.com/relational-junction/jdbc-database-drivers-products/new-relational-junction-dbf-jdbc-driver/

对我来说很好。 到目前为止的测试(大约一个小时),它读取MEMO文件,似乎没有任何DBC包含表或免费DBF的问题。

不确定这个驱动程序的成本是多少。 由于VFP已经过时,客户最多只需支付100美元。