您的位置:首页 > 数据库

JDBC连接OpenOffice.org中的Base数据库

2007-09-21 19:20 337 查看
自从用了Ubuntu就不再想跑到Windows下面去了。可是编程的时候还是要用到数据库,出了mysql首选之外,装了OOo的也可以利用Base这个现成的数据库来管理自己的资料,因为它同样支持JDBC连接。所以开始我们的OOo Base JDBC之旅吧。

一、当然是确保安装了OOo了,如果没有的话,可以自己下载自行安装
详情见:http://forum.ubuntu.org.cn/viewtopic.php?t=76127 或者到官方网站www.openoffice.org
二、下载驱动程序 hsqldb
下载地址:http://sourceforge.net/project/showfiles.php?group_id=23316
三、把下载好的驱动程序解压,找到hsqldb/lib下面的hsqldb.jar拷贝到JAVA_HOME/jre/lib/ext,比如我的就是:/opt/jdk1.6.0_02/jre/lib/ext
四、新建数据库了
打开OOo中的Base数据库,根据向导建立一个数据库,然后建立一个自己的表person.里面有两项内容name、sex.如图:



五,编程连接数据库
(http://digiassn.blogspot.com/2006/07/java-creating-jdbc-connection-to.html)
Thanks!


import org.hsqldb.jdbcDriver;


import java.sql.*;


import java.util.zip.*;


import java.io.*;


import java.util.*;




public class OooData




...{




public static void main(String[] args) ...{






try ...{


Class.forName("org.hsqldb.jdbcDriver");


}




catch(Exception x) ...{


System.out.println("Unable to load the driver class!");


}






Connection con = null; //Database objects




Statement com = null;


ResultSet rec = null;


ZipFile file = null; //For handling 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






//Unzip zip file, via info from




//http://www.devx.com/getHelpOn/10MinuteSolution/20447








try ...{


//Open the zip file that holds the OO.Org Base file




file = new ZipFile("/home/liceven/Data.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();




//Get file entries from the zipfile and loop through all of them




en = file.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 = file.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




file.close();




//Create our JDBC connection based on the temp filename used above








try ...{


con = DriverManager.getConnection("jdbc:hsqldb:file:/tmp/" + f.getName(), "SA", "");


}




catch (SQLException x) ...{


System.out.println("Couldn't get connection!");


}






//Create a command object and execute, storing the results in the rec object




com = con.createStatement();


rec = com.executeQuery("select * from "person"");




//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




rec.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/log
Extracting File: database/properties
Extracting File: database/data
Name: Kate sex: Female
Name: Spence sex: Male
Name: Yaokee sex: Male
七、如果有什么问题,可以留言跟我讨论。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: