@@ -186,15 +186,11 @@ public void actionPerformed(ActionEvent e) {
186
186
Class .forName ("org.postgresql.Driver" );
187
187
188
188
// Connect to database
189
- System .out .println ("Connecting to Database URL = " +url );
190
189
db =DriverManager .getConnection (url ,user ,password );
191
190
192
191
// Create a statement
193
192
stat =db .createStatement ();
194
193
195
- // Set the connection to use transactions
196
- db .setAutoCommit (false );
197
-
198
194
// Also, get the LargeObjectManager for this connection
199
195
lom = ((org .postgresql .Connection )db ).getLargeObjectAPI ();
200
196
@@ -210,7 +206,7 @@ public void actionPerformed(ActionEvent e) {
210
206
public void init ()
211
207
{
212
208
try {
213
- db .setAutoCommit (true );
209
+ // db.setAutoCommit(true);
214
210
stat .executeUpdate ("create table images (imgname name,imgoid oid)" );
215
211
label .setText ("Initialised database" );
216
212
db .commit ();
@@ -219,11 +215,11 @@ public void init()
219
215
}
220
216
221
217
// This must run outside the previous try{} catch{} segment
222
- try {
223
- db .setAutoCommit (true );
224
- }catch (SQLException ex ) {
225
- label .setText (ex .toString ());
226
- }
218
+ // try {
219
+ // db.setAutoCommit(true);
220
+ // } catch(SQLException ex) {
221
+ // label.setText(ex.toString());
222
+ // }
227
223
}
228
224
229
225
/**
@@ -283,37 +279,29 @@ public void run() {
283
279
// fetch the large object manager
284
280
LargeObjectManager lom = ((org .postgresql .Connection )db ).getLargeObjectAPI ();
285
281
286
- System .out .println ("Importing file" );
282
+ db .setAutoCommit (false );
283
+
287
284
// A temporary buffer - this can be as large as you like
288
285
byte buf [] =new byte [2048 ];
289
286
290
287
// Open the file
291
- System .out .println ("Opening file " +dir +"/" +name );
292
288
FileInputStream fis =new FileInputStream (new File (dir ,name ));
293
289
294
- // Gain access to large objects
295
- System .out .println ("Gaining LOAPI" );
296
-
297
290
// Now create the large object
298
- System .out .println ("creating blob" );
299
291
int oid =lom .create ();
300
-
301
- System .out .println ("Opening " +oid );
302
292
LargeObject blob =lom .open (oid );
303
293
304
294
// Now copy the file into the object.
305
295
//
306
296
// Note: we dont use write(buf), as the last block is rarely the same
307
297
// size as our buffer, so we have to use the amount read.
308
- System .out .println ("Importing file" );
309
298
int s ,t =0 ;
310
299
while ((s =fis .read (buf ,0 ,buf .length ))>0 ) {
311
- System . out . println ( "Block s=" + s + " t=" + t ); t +=s ;
300
+ t +=s ;
312
301
blob .write (buf ,0 ,s );
313
302
}
314
303
315
304
// Close the object
316
- System .out .println ("Closing blob" );
317
305
blob .close ();
318
306
319
307
// Now store the entry into the table
@@ -323,6 +311,7 @@ public void run() {
323
311
stat =db .createStatement ();
324
312
stat .executeUpdate ("insert into images values ('" +name +"'," +oid +")" );
325
313
db .commit ();
314
+ db .setAutoCommit (false );
326
315
327
316
// Finally refresh the names list, and display the current image
328
317
ImageViewer .this .refreshList ();
@@ -370,26 +359,28 @@ public void refreshList()
370
359
public void removeImage ()
371
360
{
372
361
try {
362
+ //
373
363
// Delete any large objects for the current name
364
+ //
365
+ // Note: We don't need to worry about being in a transaction
366
+ // here, because we are not opening any blobs, only deleting
367
+ // them
368
+ //
374
369
ResultSet rs =stat .executeQuery ("select imgoid from images where imgname='" +currentImage +"'" );
375
370
if (rs !=null ) {
376
371
// Even though there should only be one image, we still have to
377
372
// cycle through the ResultSet
378
373
while (rs .next ()) {
379
- System .out .println ("Got oid " +rs .getInt (1 ));
380
374
lom .delete (rs .getInt (1 ));
381
- System .out .println ("Import complete" );
382
375
}
383
376
}
384
377
rs .close ();
385
378
386
379
// Finally delete any entries for that name
387
380
stat .executeUpdate ("delete from images where imgname='" +currentImage +"'" );
388
- db .commit ();
389
381
390
382
label .setText (currentImage +" deleted" );
391
383
currentImage =null ;
392
- db .commit ();
393
384
refreshList ();
394
385
}catch (SQLException ex ) {
395
386
label .setText (ex .toString ());
@@ -404,21 +395,30 @@ public void removeImage()
404
395
public void displayImage (String name )
405
396
{
406
397
try {
407
- System .out .println ("Selecting oid for " +name );
398
+ //
399
+ // Now as we are opening and reading a large object we must
400
+ // turn on Transactions. This includes the ResultSet.getBytes()
401
+ // method when it's used on a field of type oid!
402
+ //
403
+ db .setAutoCommit (false );
404
+
408
405
ResultSet rs =stat .executeQuery ("select imgoid from images where imgname='" +name +"'" );
409
406
if (rs !=null ) {
410
407
// Even though there should only be one image, we still have to
411
408
// cycle through the ResultSet
412
409
while (rs .next ()) {
413
- System .out .println ("Got oid " +rs .getInt (1 ));
414
410
canvas .setImage (canvas .getToolkit ().createImage (rs .getBytes (1 )));
415
- System .out .println ("Import complete" );
416
411
label .setText (currentImage =name );
417
412
}
418
413
}
419
414
rs .close ();
420
415
}catch (SQLException ex ) {
421
416
label .setText (ex .toString ());
417
+ }finally {
418
+ try {
419
+ db .setAutoCommit (true );
420
+ }catch (SQLException ex2 ) {
421
+ }
422
422
}
423
423
}
424
424
@@ -454,6 +454,7 @@ public static void main(String args[])
454
454
frame .setLayout (new BorderLayout ());
455
455
ImageViewer viewer =new ImageViewer (frame ,args [0 ],args [1 ],args [2 ]);
456
456
frame .pack ();
457
+ frame .setLocation (0 ,50 );
457
458
frame .setVisible (true );
458
459
}catch (Exception ex ) {
459
460
System .err .println ("Exception caught.\n " +ex );