-->

阅读Visual FoxPro数据使用ODBC的Java(Reading Visual Foxpro

2019-09-02 11:57发布

我想从我的Java应用程序查询DBF表。 我把这个参考线

我创建使用ODBC数据源管理系统数据源,我设置数据源名称是VFPDS,并将数据库类型finaly .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位的机器。 我究竟做错了什么 ? 我将不胜感激具体答案。

Answer 1:

我能与JDBC-ODBC桥来访问Windows 7的FoxPro表,但它采取了一些步骤。 我已经安装了VFP驱动程序,我不记得在那里我下载它,所以我没有一个链接进行。

这里ODBC示例:我从JBDC复制的代码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”。

我跑在此之后,我收到了一个错误:

[微软] [ODBC驱动程序管理器]指定的DSN包含驱动程序和应用程序之间的失配的架构

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

我不知道如果有,你可以在64位JVM设置一个开关来告诉它作为32位运行。



Answer 2:

我用JDBC驱动程序从这里开始:

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

对我来说工作正常。 在迄今为止的测试(约一小时),它读取MEMO的文件,似乎有过任何DBC-containted表或免费dBFS的任何问题。

不知道有多少这样的驱动程序的成本。 客户端只可能会支付$最大100自VFP超越过时。



文章来源: Reading Visual Foxpro Data From Java using ODBC