1 32 33 package com.knowgate.dataobjs; 34 35 import java.sql.Connection ; 36 import java.sql.Statement ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.Timestamp ; 40 41 import java.util.Date ; 42 43 import java.math.BigDecimal ; 44 45 import com.knowgate.debug.DebugFile; 46 47 52 public class DBCommand { 53 54 private DBCommand() { } 55 56 58 68 public static Integer queryInt(Connection oCon, String sSQL) 69 throws SQLException ,NumberFormatException ,ClassCastException { 70 71 Statement oStm = null; 72 ResultSet oRst = null; 73 Object oObj = null; 74 Integer oInt; 75 76 if (DebugFile.trace) { 77 DebugFile.writeln("Begin DBCommand.queryInt("+sSQL+")"); 78 DebugFile.incIdent(); 79 } 80 81 try { 82 oStm = oCon.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 83 oRst = oStm.executeQuery(sSQL); 84 if (oRst.next()) { 85 oObj = oRst.getObject(1); 86 if (oRst.wasNull()) oObj = null; 87 } 88 oRst.close(); 89 oRst=null; 90 oStm.close(); 91 oStm=null; 92 } catch (Exception xcpt) { 93 if (DebugFile.trace) { 94 DebugFile.writeln(xcpt.getClass().getName()+" "+xcpt.getMessage()); 95 DebugFile.decIdent(); 96 } 97 if (oRst!=null) { try {oRst.close(); } catch (Exception ignore) {} } 98 if (oStm!=null) { try {oStm.close(); } catch (Exception ignore) {} } 99 throw new SQLException (xcpt.getMessage()); 100 } 101 102 if (null==oObj) 103 oInt = null; 104 else 105 oInt = new Integer (oObj.toString()); 106 107 if (DebugFile.trace) { 108 DebugFile.decIdent(); 109 if (null==oInt) 110 DebugFile.writeln("End DBCommand.queryInt() : null"); 111 else 112 DebugFile.writeln("End DBCommand.queryInt() : "+oInt.toString()); 113 } 114 115 return oInt; 116 } 118 120 130 131 public static String queryStr(Connection oCon, String sSQL) 132 throws SQLException { 133 134 Statement oStm = null; 135 ResultSet oRst = null; 136 String sStr = null; 137 138 if (DebugFile.trace) { 139 DebugFile.writeln("Begin DBCommand.queryStr("+sSQL+")"); 140 DebugFile.incIdent(); 141 } 142 143 try { 144 oStm = oCon.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 145 oRst = oStm.executeQuery(sSQL); 146 if (oRst.next()) { 147 sStr = oRst.getString(1); 148 if (oRst.wasNull()) sStr = null; 149 } 150 oRst.close(); 151 oRst=null; 152 oStm.close(); 153 oStm=null; 154 } catch (Exception xcpt) { 155 if (DebugFile.trace) { 156 DebugFile.writeln(xcpt.getClass().getName()+" "+xcpt.getMessage()); 157 DebugFile.decIdent(); 158 } 159 if (oRst!=null) { try {oRst.close(); } catch (Exception ignore) {} } 160 if (oStm!=null) { try {oStm.close(); } catch (Exception ignore) {} } 161 throw new SQLException (xcpt.getMessage()); 162 } 163 164 if (DebugFile.trace) { 165 DebugFile.decIdent(); 166 DebugFile.writeln("End DBCommand.queryInt() : "+sStr); 167 } 168 169 return sStr; 170 } 172 174 184 185 public static BigDecimal queryBigDecimal(Connection oCon, String sSQL) 186 throws SQLException { 187 188 Statement oStm = null; 189 ResultSet oRst = null; 190 BigDecimal oDec = null; 191 192 if (DebugFile.trace) { 193 DebugFile.writeln("Begin DBCommand.queryBigDecimal("+sSQL+")"); 194 DebugFile.incIdent(); 195 } 196 197 try { 198 oStm = oCon.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 199 oRst = oStm.executeQuery(sSQL); 200 if (oRst.next()) { 201 oDec = oRst.getBigDecimal(1); 202 if (oRst.wasNull()) oDec = null; 203 } 204 oRst.close(); 205 oRst=null; 206 oStm.close(); 207 oStm=null; 208 } catch (Exception xcpt) { 209 if (DebugFile.trace) { 210 DebugFile.writeln(xcpt.getClass().getName()+" "+xcpt.getMessage()); 211 DebugFile.decIdent(); 212 } 213 if (oRst!=null) { try {oRst.close(); } catch (Exception ignore) {} } 214 if (oStm!=null) { try {oStm.close(); } catch (Exception ignore) {} } 215 throw new SQLException (xcpt.getMessage()); 216 } 217 218 if (DebugFile.trace) { 219 DebugFile.decIdent(); 220 if (null==oDec) 221 DebugFile.writeln("End DBCommand.queryBigDecimal() : null "); 222 else 223 DebugFile.writeln("End DBCommand.queryBigDecimal() : "+oDec.toString()); 224 } 225 return oDec; 226 } 228 230 240 241 public static Date queryDateTime(Connection oCon, String sSQL) 242 throws SQLException { 243 244 Statement oStm = null; 245 ResultSet oRst = null; 246 Timestamp oTs = null;; 247 Date oDt = null; 248 249 if (DebugFile.trace) { 250 DebugFile.writeln("Begin DBCommand.queryDateTime("+sSQL+")"); 251 DebugFile.incIdent(); 252 } 253 254 try { 255 oStm = oCon.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 256 oRst = oStm.executeQuery(sSQL); 257 if (oRst.next()) { 258 oTs = oRst.getTimestamp(1); 259 if (oRst.wasNull()) 260 oDt = null; 261 else 262 oDt = new Date (oTs.getTime()); 263 } 264 oRst.close(); 265 oRst=null; 266 oStm.close(); 267 oStm=null; 268 } catch (Exception xcpt) { 269 if (DebugFile.trace) { 270 DebugFile.writeln(xcpt.getClass().getName()+" "+xcpt.getMessage()); 271 DebugFile.decIdent(); 272 } 273 if (oRst!=null) { try {oRst.close(); } catch (Exception ignore) {} } 274 if (oStm!=null) { try {oStm.close(); } catch (Exception ignore) {} } 275 throw new SQLException (xcpt.getMessage()); 276 } 277 278 if (DebugFile.trace) { 279 DebugFile.decIdent(); 280 if (null==oDt) 281 DebugFile.writeln("End DBCommand.queryDateTime() : null "); 282 else 283 DebugFile.writeln("End DBCommand.queryDateTime() : "+oDt.toString()); 284 } 285 return oDt; 286 } 288 290 297 public static int executeUpdate(Connection oCon, String sSQL) throws SQLException { 298 if (DebugFile.trace) { 299 DebugFile.writeln("Begin DBCommand.executeUpdate("+sSQL+")"); 300 DebugFile.incIdent(); 301 } 302 303 Statement oStm = null; 304 int iAffected = 0; 305 try { 306 oStm = oCon.createStatement(); 307 iAffected = oStm.executeUpdate(sSQL); 308 oStm.close(); 309 oStm=null; 310 } catch (SQLException sqle) { 311 if (DebugFile.trace) { 312 DebugFile.writeln("SQLException "+sqle.getMessage()); 313 DebugFile.decIdent(); 314 } 315 if (null!=oStm) { try { oStm.close(); } catch (Exception ignore) {} } 316 } 317 318 if (DebugFile.trace) { 319 DebugFile.decIdent(); 320 DebugFile.writeln("End DBCommand.executeUpdate() : " + String.valueOf(iAffected)); 321 } 322 323 return iAffected; 324 } } 326
| Popular Tags
|