2013-05-10 35 views
2

我想從我的Java應用程序中查詢dbf表。我把這個參考thread使用ODBC從Java讀取Visual Foxpro數據

我使用ODBC數據源管理員創建了一個系統數據源,我將數據源名稱設置爲VFPDS並將數據庫類型設置爲.DBC finaly我將路徑設置爲.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位機的表。 我在做什麼錯?我將不勝感激具體的答案。

+0

VFP錯誤「不是表」可能意味着很多不同的事情。這可能意味着表格已損壞。這也可能意味着該表已被加密。你有VFP的副本嗎?如果是這樣,你可以在那裏打開桌子嗎?如果是這樣,你知道問題出在你的連接代碼上。如果不是,那就是桌子。 – 2013-05-10 20:48:28

+0

@ TamarE.Granor,我可以打開表並查看它的記錄。所以它不是桌子 – user1912404 2013-05-13 06:27:04

回答

0

我能夠通過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 Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application

這是因爲我用一個64位的JVM具有32位DSN。我下載了一個32位的JVM,並能夠使用它來運行我的示例類。

我不知道是否有一個開關可以在64位JVM上設置,以告訴它作爲32位運行。