| Method from example.ImageViewer Detail: |
public void close() {
try {
db.close();
} catch(SQLException ex) {
System.err.println(ex.toString());
}
System.exit(0);
}
This closes the connection, and ends the application |
public void displayImage(String name) {
try {
//
// Now as we are opening and reading a large object we must
// turn on Transactions. This includes the ResultSet.getBytes()
// method when it's used on a field of type oid!
//
db.setAutoCommit(false);
ResultSet rs = stat.executeQuery("select imgoid from images where imgname='"+name+"'");
if(rs!=null) {
// Even though there should only be one image, we still have to
// cycle through the ResultSet
while(rs.next()) {
canvas.setImage(canvas.getToolkit().createImage(rs.getBytes(1)));
label.setText(currentImage = name);
}
}
rs.close();
} catch(SQLException ex) {
label.setText(ex.toString());
} finally {
try {
db.setAutoCommit(true);
} catch(SQLException ex2) {
}
}
}
This displays an image from the database.
For images, this is the easiest method. |
public void importImage() {
FileDialog d = new FileDialog(frame,"Import Image",FileDialog.LOAD);
d.setVisible(true);
String name = d.getFile();
String dir = d.getDirectory();
d.dispose();
// now start the true importer
Thread t = new importer(db,name,dir);
//t.setPriority(Thread.MAX_PRIORITY);
t.start();
}
This imports an image into the database, using a Thread to do this in the
background. |
public void init() {
try {
//db.setAutoCommit(true);
stat.executeUpdate("create table images (imgname name,imgoid oid)");
label.setText("Initialised database");
db.commit();
} catch(SQLException ex) {
label.setText(ex.toString());
}
// This must run outside the previous try{} catch{} segment
//try {
//db.setAutoCommit(true);
//} catch(SQLException ex) {
//label.setText(ex.toString());
//}
}
This method initialises the database by creating a table that contains
the image names, and Large Object OID's |
public static void instructions() {
System.err.println("java example.ImageViewer jdbc-url user password");
System.err.println("\nExamples:\n");
System.err.println("java -Djdbc.driver=org.postgresql.Driver example.ImageViewer jdbc:postgresql:test postgres password\n");
System.err.println("This example tests the binary large object api of the driver.\nBasically, it will allow you to store and view images held in the database.");
System.err.println("Note: If you are running this for the first time on a particular database,\nyou have to select \"Initialise\" in the \"PostgreSQL\" menu.\nThis will create a table used to store image names.");
}
This is the command line instructions |
public void itemStateChanged(ItemEvent e) {
displayImage(list.getItem(((Integer)e.getItem()).intValue()));
}
|
public static void main(String[] args) {
if(args.length!=3) {
instructions();
System.exit(1);
}
try {
Frame frame = new Frame("PostgreSQL ImageViewer v7.0 rev 1");
frame.setLayout(new BorderLayout());
ImageViewer viewer = new ImageViewer(frame,args[0],args[1],args[2]);
frame.pack();
frame.setLocation(0,50);
frame.setVisible(true);
} catch(Exception ex) {
System.err.println("Exception caught.\n"+ex);
ex.printStackTrace();
}
}
This is the application entry point |
public void refreshList() {
try {
// First, we'll run a query, retrieving all of the image names
ResultSet rs = stat.executeQuery("select imgname from images order by imgname");
if(rs!=null) {
list.removeAll();
while(rs.next())
list.addItem(rs.getString(1));
rs.close();
}
} catch(SQLException ex) {
label.setText(ex.toString()+" Have you initialised the database?");
}
}
This refreshes the list of available images |
public void removeImage() {
try {
//
// Delete any large objects for the current name
//
// Note: We don't need to worry about being in a transaction
// here, because we are not opening any blobs, only deleting
// them
//
ResultSet rs = stat.executeQuery("select imgoid from images where imgname='"+currentImage+"'");
if(rs!=null) {
// Even though there should only be one image, we still have to
// cycle through the ResultSet
while(rs.next()) {
lom.delete(rs.getInt(1));
}
}
rs.close();
// Finally delete any entries for that name
stat.executeUpdate("delete from images where imgname='"+currentImage+"'");
label.setText(currentImage+" deleted");
currentImage=null;
refreshList();
} catch(SQLException ex) {
label.setText(ex.toString());
}
}
This removes an image from the database
Note: With postgresql, this is the only way of deleting a large object
using Java. |