[英]Reading Visual Foxpro Data From Java using ODBC
我正在嘗試從我的 Java 應用程序中查詢 dbf 表。 我引用了這個線程
我使用 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 驅動程序:
對我來說很好用。 在到目前為止的測試中(大約一個小時),它讀取 MEMO 文件並且似乎對包含 DBC 的表或免費 DBF 沒有問題。
不知道這個驅動程序要多少錢。 由於 VFP 已經過時,客戶可能最多只支付 100 美元。
我在過去六個月中使用 javadbf-0.4.0.jar 沒有問題。我將發布我的 Java 類和主程序。
package foxtablereader.src;
import java.sql.Connection;
import java.sql.SQLException;
import sun.jdbc.rowset.CachedRowSet;
/**
*
* @author kalimk
*/
public class DbfMain {
static CachedRowSet crs;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException {
DbfConnection foxconn = new DbfConnection();
Connection connection = null;
String inputTablePath = "c:\\vfp_table\\";
connection = foxconn.connection(inputTablePath);
String query = "SELECT * FROM TrigExport2.dbf ";
crs = foxconn.select(query, connection);
//crs = ft.query(query, inputTablePath);
System.out.println(" Size " + crs.size());
while (crs.next()) {
System.out.println("DESC " + crs.getString("Serialnum") + " " + crs.getString("cmailcode"));
}
inputTablePath = "C:\\sourcecode\\FoxTableReader\\";
connection = foxconn.connection(inputTablePath);
query = "SELECT * FROM TrigExports.dbf ";
crs = foxconn.select(query, connection);
System.out.println(" Size " + crs.size());
int z = 1;
while (crs.next()) {
System.out.println(z++ + " " + crs.getString("Serialnum") + " " + crs.getString("cmailcode"));
}
inputTablePath = "C:\\sourcecode\\FoxTableReader\\";
connection = foxconn.connection(inputTablePath);
String queryinsert = "insert into Mcdesc (Desc,Mailcode,Del_type,Column1) values ('Kalim','KHAN', 'ERUM','KHAN')";
foxconn.insertUpdate(queryinsert, connection);
inputTablePath = "C:\\sourcecode\\FoxTableReader\\";
connection = foxconn.connection(inputTablePath);
String queryupdate = "update Mcdesc set Column1= 'Sadiqquie' where Desc = 'Kalim'";
foxconn.insertUpdate(queryupdate, connection);
}
}
package foxtablereader.src;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.jdbc.rowset.CachedRowSet;
/**
*
* @author kalimk
*/
public class DbfConnection {
public Connection connection(String inputTablePath) throws SQLException {
Connection con = null;
//String jdbURL = "jdbc:DBF:/C:\\vfp_table\\";
String jdbURL = "jdbc:DBF:/" + inputTablePath;
Properties props = new Properties();
props.setProperty("delayedClose", "0");
try {
Class.forName("com.caigen.sql.dbf.DBFDriver");
try {
con = DriverManager.getConnection(jdbURL, props);
} catch (SQLException ex) {
Logger.getLogger(DbfConnection.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(DbfConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
public CachedRowSet select(String cQuery, Connection con) {
CachedRowSet crs = null;
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(cQuery);
crs = new CachedRowSet();
crs.populate(rs);
stmt.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return crs;
}
public boolean insertUpdate(String cQuery, Connection con) {
boolean crs = false;
try {
PreparedStatement pStmnt = con.prepareStatement(cQuery);
crs = pStmnt.execute();
pStmnt.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return crs;
}
public int insertUpdateCount(String cQuery, Connection con) {
int count = 0;
try {
Statement stmt = con.createStatement();
count = stmt.executeUpdate(cQuery);
System.out.println("Number of rows updated in database = " + count);
stmt.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return count;
}
public int insertUpdateCount2(String cQuery, Connection con) {
int count = 0;
boolean crs = false;
try {
PreparedStatement pStmnt = con.prepareStatement(cQuery);
count = pStmnt.executeUpdate(cQuery);
pStmnt.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return count;
}
public static void DBDisconnect(Connection DBCon) {
try {
if (DBCon != null) {
DBCon.close();
}
} catch (SQLException e) {
System.out.println(e.toString());
} finally {
try {
DBCon.close();
//System.err.println("Fimally is always executed");
} catch (SQLException ex) {
Logger.getLogger(DbfConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.