package samples.test; import com.jacob.com.*; import com.jacob.activeX.*; class Access { public static void main(String[] args) throws Exception { ComThread.InitSTA(); ActiveXComponent ax = new ActiveXComponent("DAO.PrivateDBEngine"); // this only works for access files pre-access-2000 Dispatch db = open(ax, ".\\sample2.mdb"); String sql = "select * from MainTable"; // make a temporary querydef Dispatch qd = Dispatch.call(db, "CreateQueryDef","").toDispatch(); // set the SQL string on it Dispatch.put(qd, "SQL", sql); Variant result = getByQueryDef(qd); // the 2-d safearray is transposed from what you might expect System.out.println(result.toSafeArray()); close(db); ComThread.Release(); } /** * Open a database */ public static Dispatch open(ActiveXComponent ax, String fileName) { Variant f = new Variant(false); // open the file in read-only mode Variant[] args = new Variant[] {new Variant(fileName), f, f}; Dispatch openDB = ax.invoke("OpenDatabase", args).toDispatch(); return openDB; } /** * Close a database */ public static void close(Dispatch openDB) { Dispatch.call(openDB, "Close"); } /** * Extract the values from the recordset */ public static Variant getValues(Dispatch recset) { Dispatch.callSub(recset,"moveFirst"); Variant vi = new Variant(4096); Variant v = Dispatch.call(recset,"GetRows", vi); return v; } public static Variant getByQueryDef(Dispatch qd) { // get a reference to the recordset Dispatch recset = Dispatch.call(qd, "OpenRecordset").toDispatch(); // get the values as a safe array String[] cols = getColumns(recset); for(int i=0;i