1 23 24 package org.dbforms.config; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.util.IEscaper; 30 import org.dbforms.util.FileHolder; 31 32 import java.io.ByteArrayInputStream ; 33 import java.io.ByteArrayOutputStream ; 34 import java.io.IOException ; 35 import java.io.ObjectOutputStream ; 36 37 import java.sql.Clob ; 38 import java.sql.PreparedStatement ; 39 import java.sql.ResultSet ; 40 import java.sql.SQLException ; 41 import java.sql.Types ; 42 43 51 public class JDBCDataHelper { 52 private static Log logCat = LogFactory.getLog(JDBCDataHelper.class 54 .getName()); 55 56 71 public static Object getData(ResultSet rs, IEscaper escaper, int col) 72 throws SQLException { 73 Object res = null; 74 75 switch (rs.getMetaData().getColumnType(col)) { 76 case Types.CLOB: { 77 String s; 78 Clob tmpObj = (Clob ) rs.getObject(col); 79 80 if (tmpObj != null) { 81 s = tmpObj.getSubString(1, (int) tmpObj.length()); 82 } else { 83 s = null; 84 } 85 86 res = (escaper == null) ? s : escaper.unescapeJDBC(s); 87 88 break; 89 } 90 91 case Types.LONGVARCHAR: 92 case Types.CHAR: 93 case Types.VARCHAR: 94 95 String s = rs.getString(col); 96 res = (escaper == null) ? s : escaper.unescapeJDBC(s); 97 98 break; 99 100 default: { 101 Object tmpObj = rs.getObject(col); 102 res = tmpObj; 103 104 break; 105 } 106 } 107 108 return res; 109 } 110 111 133 public static void fillWithData(PreparedStatement ps, IEscaper escaper, 134 int col, Object value, int fieldType, int blobStrategy) 135 throws SQLException { 136 logCat.debug("fillPreparedStatement( ps, " + col + ", " + value + ", " 137 + fieldType + ")..."); 138 139 switch (fieldType) { 140 case 0: 141 throw new SQLException ("illegal type!"); 142 143 case FieldTypes.BLOB: 144 145 if (value == null) { 146 ps.setNull(col, java.sql.Types.BLOB); 147 } else if (blobStrategy == Table.BLOB_CLASSIC) { 148 FileHolder fileHolder = (FileHolder) value; 150 151 try { 152 ByteArrayOutputStream byteOut = new ByteArrayOutputStream (); 153 ObjectOutputStream out = new ObjectOutputStream (byteOut); 154 out.writeObject(fileHolder); 155 out.flush(); 156 157 byte[] buf = byteOut.toByteArray(); 158 byteOut.close(); 159 out.close(); 160 161 ByteArrayInputStream bytein = new ByteArrayInputStream (buf); 162 int byteLength = buf.length; 163 ps.setBinaryStream(col, bytein, byteLength); 164 165 } catch (IOException ioe) { 168 ioe.printStackTrace(); 169 logCat.info(ioe.toString()); 170 throw new SQLException ( 171 "error storing BLOB in database (BLOB_CLASSIC MODE) - " 172 + ioe.toString(), null, 2); 173 } 174 } else if (value instanceof FileHolder) { 179 try { 181 FileHolder fileHolder = (FileHolder) value; 182 ps.setBinaryStream(col, fileHolder 183 .getInputStreamFromBuffer(), fileHolder 184 .getFileLength()); 185 } catch (IOException ioe) { 186 ioe.printStackTrace(); 187 logCat.info(ioe.toString()); 188 throw new SQLException ( 189 "error storing BLOB in database (BLOB_CLASSIC MODE) - " 190 + ioe.toString(), null, 2); 191 } 192 } else { 194 byte[] data = ((String ) value).getBytes(); 196 ps.setBinaryStream(col, new ByteArrayInputStream (data), 197 data.length); 198 } 199 200 break; 201 202 case FieldTypes.DISKBLOB: 203 ps.setObject(col, (escaper == null) ? value : escaper 204 .escapeJDBC((String ) value), FieldTypes.CHAR); 205 206 break; 207 208 case FieldTypes.CHAR: 209 ps.setObject(col, (escaper == null) ? value : escaper 210 .escapeJDBC((String ) value), FieldTypes.CHAR); 211 212 break; 213 214 default: 215 ps.setObject(col, value, fieldType); 216 217 break; 218 } 219 } 220 } 221 | Popular Tags |