1 23 24 package org.dbforms.util; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileNotFoundException ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.ObjectInputStream ; 35 36 import java.sql.Blob ; 37 import java.sql.Connection ; 38 import java.sql.ResultSet ; 39 import java.sql.SQLException ; 40 41 49 public class SqlUtil { 50 private static Log logCat = LogFactory.getLog(SqlUtil.class.getName()); 52 53 59 public static final void closeConnection(Connection con) { 60 if (con != null) { 61 try { 62 SqlUtil.logCat.debug("About to close connection - " + con); 63 con.close(); 64 SqlUtil.logCat.debug("Connection closed"); 65 } catch (SQLException e) { 66 SqlUtil 67 .logSqlException(e, 68 "::closeConnection - cannot close the input connection"); 69 } 70 } 71 } 72 73 82 public static final void logSqlException(SQLException e, String desc) { 83 int i = 0; 84 String excDesc = "::logSqlExceptionSQL - main SQL exception"; 85 86 if (!Util.isNull(desc)) { 88 excDesc += (" [" + desc + "]"); 89 } 90 91 SqlUtil.logCat.error(excDesc, e); 92 93 while ((e = e.getNextException()) != null) 94 SqlUtil.logCat.error("::logSqlException - nested SQLException (" 95 + (i++) + ")", e); 96 } 97 98 105 public static final void logSqlException(SQLException e) { 106 SqlUtil.logSqlException(e, null); 107 } 108 109 122 public static FileHolder readFileHolderBlob(ResultSet rs) 123 throws IOException , SQLException { 124 logCat.info("READING FILEHOLDER"); 125 Object o = rs.getObject(1); 126 if (o == null) { 127 logCat.warn("::readDbFieldBlob - blob null, no response sent"); 128 return null; 129 } 130 logCat.info("o instanceof ..." + o.getClass().getName()); 131 try { 135 if (o instanceof java.sql.Blob ) { 136 Blob blob = rs.getBlob(1); 137 ObjectInputStream ois = new ObjectInputStream (blob 138 .getBinaryStream()); 139 FileHolder fh = (FileHolder) ois.readObject(); 140 return fh; 141 } 142 else { 144 InputStream blobIS = rs.getBinaryStream(1); 146 ObjectInputStream ois = new ObjectInputStream (blobIS); 147 FileHolder fh = (FileHolder) ois.readObject(); 148 return fh; 149 } 150 } catch (ClassNotFoundException cnfe) { 151 logCat.error("::readDbFieldBlob - class not found", cnfe); 152 throw new IOException ("error:" + cnfe.toString()); 153 } 154 155 } 156 157 170 public static InputStream readDbFieldBlob(ResultSet rs) throws IOException , 171 SQLException { 172 logCat.info("READING BLOB"); 173 Object o = rs.getObject(1); 174 if (o == null) { 175 logCat.warn("::readDbFieldBlob - blob null, no response sent"); 176 return null; 177 } 178 logCat.info("o instanceof ..." + o.getClass().getName()); 179 if (o instanceof java.sql.Blob ) { 183 return ((Blob ) o).getBinaryStream(); 184 } 185 else { 187 return rs.getBinaryStream(1); 189 } 190 } 191 192 207 public static FileInputStream readDiskBlob(String fileName, 208 String directory, String defVal) throws FileNotFoundException , 209 IOException { 210 logCat.info(new StringBuffer ("READING DISKBLOB\n directory = [") 211 .append(directory).append("]\n").append(" fileName = [") 212 .append(fileName).append("]\n").append(" defaultValue = [") 213 .append(defVal).append("]\n").toString()); 214 215 if (Util.isNull(fileName)) { 216 if ((fileName = defVal) != null) { 217 logCat 218 .info("::readDiskBlob - database data is null; use the default value [" 219 + fileName + "]"); 220 } 221 } 222 223 if (fileName != null) { 226 fileName = fileName.trim(); 227 File file = new File (directory, fileName); 228 if (file.exists()) { 229 logCat.info("::readDiskBlob - file found [" 230 + file.getAbsoluteFile() + "]"); 231 return new FileInputStream (file); 232 } else { 233 logCat.error("::readDiskBlob - file [" 234 + (directory + "/" + fileName) + "] not found"); 235 236 return null; 237 } 238 } else { 239 logCat 240 .warn("::readDiskBlob - file name or directory value is null"); 241 242 return null; 243 } 244 } 245 } 246 | Popular Tags |