1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| import java.sql.*;
import java.util.zip.*;
import java.io.*;
import org.hsqldb.jdbcDriver;
import java.util.*;
public class Test {
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 file = 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
//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/digiassn/OODatabase/employeeDatabase.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
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();
rec = com.executeQuery("select * from \"employees\"");
//GO through the resultset, and output the results
while (rec.next())
System.out.println("Last Name: " + rec.getString("nm_emp_last") + " First Name: " + rec.getString("nm_emp_first"));
//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();
}
}
} |
Partager