OpenOffice的数据库引擎为HSQLDB,HSQLDB是一个基于内存和磁盘文件的数据表,以及支持嵌入式和服务器模式的纯JAVA实现的,小型数据库,非常适合开发和集成,利用HSQLDB的jdbcdriver就能顺利读取了.例子如下:
转载自:http://myzhm.blog.163.com/blog/static/7049471820083111619743/
数据库文件为testoo.odb,表名:Table1,采用HSQLDB驱动
public class Testmy { public static void main(String[] args) { jdbcDriver j = new jdbcDriver(); //Instantiate the jdbcDriver from HSQL Connection con = null; //Database objects Statement com = null; ResultSet rec = null; ZipFile sourceFile = null; //For handeling zip files ZipEntry ent = null; Enumeration en = null; //For the entries in the zip file BufferedOutputStream out = null; //For the output from the zip class InputStream in = null; //for reading buffers from the zip file File f = null; //Used to get a temporary file name, not actually used for anything int len; //General length counter for loops List v = new ArrayList(); //Stores list of unzipped file for deletion at end of program try { //Open the zip file that holds the OO.Org Base file sourceFile = new ZipFile("testoo.odb"); //Create a generic temp file. I only need to get the filename from //the tempfile to prefix the extracted files for OO Base f = File.createTempFile("ooTempDatabase", "tmp"); // f.deleteOnExit(); // f.canExecute(); //Get file entries from the zipfile and loop through all of them en = sourceFile.entries(); while (en.hasMoreElements()) { //Get the current element ent = (ZipEntry)en.nextElement(); //If the file is in the database directory, extract it to our //temp folder using the temp filename above as a prefix if (ent.getName().startsWith("database/")) { System.out.println("Extracting File: " + ent.getName()); byte[] buffer = new byte[1024]; //Create an input stream file the file entry in = sourceFile.getInputStream(ent); //Create a output stream to write out the entry to, using the //temp filename created above out = new BufferedOutputStream(new FileOutputStream("/tmp/" + f.getName() + "." + ent.getName().substring(9))); //Add the newly created temp file to the tempfile vector for deleting //later on v.add("/tmp/" + f.getName() + "." + ent.getName().substring(9)); //Read the input file into the buffer, then write out to //the output file while((len = in.read(buffer)) >= 0) out.write(buffer, 0, len); //close both the input stream and the output stream out.close(); in.close(); } } //Close the zip file since the temp files have been created sourceFile.close(); //////////////////////////////////////////////////////////////////// //Create our JDBC connection based on the temp filename used above con = DriverManager.getConnection("jdbc:hsqldb:file:/tmp/" + f.getName(), "SA", ""); //Create a command object and execute, storing the results in the rec object com = con.createStatement(); //com.executeUpdate() com.executeUpdate("create table /"Table2/" (name char(9),age char(3))"); com.executeUpdate("insert into /"Table2/" (name,age) values ('Chris','14')"); rec = com.executeQuery("select * from /"Table1/""); //GO through the resultset, and output the results while (rec.next()) System.out.println("Name: " + rec.getString("Name") + " Sex: " + rec.getString("Sex")); //Close all the database objects ResultSet rec2 = com.executeQuery("select * from /"Table2/""); //GO through the resultset, and output the results while (rec2.next()) System.out.println("Name: " + rec2.getString("Name") + " Age: " + rec2.getString("Age")); //Close all the database objects rec.close(); rec2.close(); com.close(); con.close(); //Delete the temporary files, which file names are stored in the v vector //for (len = 0; len <> v.size(); len++) //(new File((String)v.get(len))).delete(); } catch (Exception e) { e.printStackTrace(); } } }
执行结果
Extracting File: database/script
Extracting File: database/backup
Extracting File: database/properties
Extracting File: database/data
Name: mm Sex: mm
Name: gg Sex: gg
Name: Carter Sex: mm
Name: Chris Age: 14