KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > transactions > SaveImageOracle


1 package transactions;
2
3 import dinamica.*;
4 import java.io.*;
5 import java.sql.*;
6
7 /**
8  * Save image or document into Oracle 9i blob column.
9  * Oracle thin driver for 9i does not fully adhere to
10  * the JDBC Blob API, so this is a patch for Oracle only.
11  *
12  * @author Oasahi and Martin Cordova
13  * */

14 public class SaveImageOracle extends GenericTransaction
15 {
16
17     /* (non-Javadoc)
18      * @see dinamica.GenericTransaction#service(dinamica.Recordset)
19      */

20     public int service(Recordset inputParams) throws Throwable JavaDoc
21     {
22         int rc = super.service(inputParams);
23         
24         //set image size field
25
String JavaDoc path = (String JavaDoc)inputParams.getValue("file");
26         File f = new File(path);
27         Integer JavaDoc size = new Integer JavaDoc((int)f.length());
28         inputParams.setValue("image_size", size);
29
30         //fix original filename (remove path)
31
String JavaDoc fileName = (String JavaDoc)inputParams.getValue("file.filename");
32         fileName = fileName.substring(fileName.lastIndexOf(File.separator)+1);
33         inputParams.setValue("file.filename", fileName);
34         
35         //prepare sql template
36
String JavaDoc sql = getResource("query.sql");
37         sql = getSQL(sql, inputParams);
38         
39         /* start patch oracle 9i */
40         
41         //get db object and save record with empty blob
42
Db db = getDb();
43         db.exec(sql);
44
45         // get sequence value
46
Recordset rsId = db.get("select seq_image.currval id from dual");
47         rsId.next();
48         String JavaDoc id = rsId.getString("id");
49
50         // get record using SELECT FOR UPDATE...
51
Class.forName("oracle.sql.BLOB");
52         sql = getResource("query-blob.sql");
53         sql = StringUtil.replace(sql, "${id}", id);
54     
55         Connection conn = this.getConnection();
56         Statement s = null;
57         ResultSet rs = null;
58         BufferedOutputStream out = null;
59         BufferedInputStream inp = null;
60         
61         try {
62             s = conn.createStatement();
63             rs = s.executeQuery(sql);
64             while (rs.next())
65             {
66                 // get BLOB
67
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("image_data");
68                 
69                 // save file contents into BLOB
70
out = new BufferedOutputStream(blob.getBinaryOutputStream());
71                 byte buffer[] = new byte[8192];
72                 int c = 0;
73                 inp = new BufferedInputStream( new FileInputStream(f) );
74                 while (c >= 0)
75                 {
76                     c = inp.read(buffer);
77                     if (c>0)
78                         out.write(buffer, 0, c);
79                 }
80             }
81         }
82         catch (Throwable JavaDoc e)
83         {
84             throw e;
85         }
86         finally
87         {
88             if (inp!=null) inp.close();
89             if (out!=null) out.close();
90             if (rs!=null) rs.close();
91             if (s!=null) s.close();
92         }
93         
94         /* end patch oracle 9i */
95         
96         return rc;
97         
98     }
99
100 }
101
Popular Tags