1 package org.apache.ojb.broker.ant; 2 3 17 18 import java.sql.*; 19 import java.util.Hashtable ; 20 21 30 public class DBUtility 31 { 32 private Connection m_connection = null; 33 34 private String m_url = null; 35 private String m_user = null; 36 private String m_pwd = null; 37 private static String m_ORA_EXCEPTION_1000 = "ORA-01000"; 38 private static String m_ORA_EXCEPTION_604 = "ORA-00604"; 39 40 50 public DBUtility(String url, String user, String pwd) 51 throws SQLException 52 { 53 m_url = url; 54 m_user = user; 55 m_pwd = pwd; 56 m_connection = connect(url, user, pwd); 57 } 58 59 public void release() throws SQLException 60 { 61 if(m_connection != null) 62 { 63 m_connection.close(); 64 } 65 } 66 67 private void resetConnection() 68 { 69 try 70 { 71 release(); 72 connect(m_url, m_user, m_pwd); 73 } 74 catch(Exception e) 75 { 76 System.out.println("Could not reconnect to database!!!! " + e.getMessage()); 77 } 78 } 79 80 private Connection connect(String url, String user, String pwd) throws SQLException 81 { 82 m_connection = DriverManager.getConnection(url, user, pwd); 83 return m_connection; 84 } 85 86 87 96 public boolean exists(String tableName) 97 { 98 boolean bReturn = false; 99 100 if (tableName == null) return bReturn; 101 PreparedStatement checkTable = null; 102 try 103 { 104 checkTable = m_connection.prepareStatement("Select * from " + tableName + " where 1=0"); 107 checkTable.executeQuery(); 108 bReturn = true; 109 } 110 catch(Exception e) 111 { 112 if (e.getMessage().startsWith(m_ORA_EXCEPTION_1000) || e.getMessage().startsWith(m_ORA_EXCEPTION_604)) 113 { 114 System.out.println("Exceeded available Oracle cursors. Resetting connection and trying the SQL statement again..."); 115 resetConnection(); 116 return exists(tableName); 117 } 118 else 119 { 120 bReturn = false; 122 } 123 } 124 125 return bReturn; 126 } 127 128 private Hashtable m_columnCache = new Hashtable (79); 129 130 private ResultSet getColumns(String tableName) 131 { 132 return (ResultSet)m_columnCache.get(tableName); 133 } 134 private void putColumns(String tableName, ResultSet columns) 135 { 136 m_columnCache.put(tableName, columns); 137 } 138 139 159 public void exists(String tableName, String columnName, String jdbcType, boolean ignoreCase) throws SQLException 160 { 161 if (tableName == null) throw new SQLException("TableName was null. You must specify a valid table name."); 162 if (columnName == null) throw new SQLException("Column name was null. You must specify a valid column name."); 163 164 ResultSet columns = getColumns(tableName); 165 166 if(columns == null) 167 { 168 PreparedStatement checkTable = null; 170 try 171 { 172 checkTable = m_connection.prepareStatement("Select * from " + tableName + " where 1=0"); 175 columns = checkTable.executeQuery(); 176 putColumns(tableName, columns); 177 } 178 catch(SQLException sqle) 179 { 180 if (sqle.getMessage().startsWith(m_ORA_EXCEPTION_1000) || sqle.getMessage().startsWith(m_ORA_EXCEPTION_604)) 181 { 182 System.out.println("Exceeded available Oracle cursors. Resetting connection and trying the SQL statement again..."); 183 resetConnection(); 184 exists(tableName, columnName, jdbcType, ignoreCase); 185 } 186 else 187 { 188 throw sqle; 190 } 191 } 192 } 193 194 ResultSetMetaData rsMeta = columns.getMetaData(); 195 int iColumns = rsMeta.getColumnCount(); 196 int jdbcTypeConst = this.getJdbcType(jdbcType); 197 for(int i = 1; i <= iColumns; i++) 198 { 199 if(ignoreCase) 200 { 201 if(columnName.equalsIgnoreCase(rsMeta.getColumnName(i))) 203 { 204 if(jdbcTypeConst != rsMeta.getColumnType(i)) 206 { 207 throw new SQLException("The column '" + tableName + "." + columnName + "' is of type '" + rsMeta.getColumnTypeName(i) + "' and cannot be mapped to the jdbc type '" + jdbcType + "'."); 208 } 209 else 210 { 211 return; 212 } 213 } 214 } 215 else 216 { 217 if(columnName.equals(rsMeta.getColumnName(i))) 219 { 220 if(jdbcTypeConst != rsMeta.getColumnType(i)) 222 { 223 throw new SQLException("The column '" + tableName + "." + columnName + "' is of type '" + rsMeta.getColumnTypeName(i) + "' and cannot be mapped to the jdbc type '" + jdbcType + "'."); 224 } 225 else 226 { 227 return; 228 } 229 } 230 231 } 232 233 } 235 236 throw new SQLException("The column '" + columnName + "' was not found in table '" + tableName + "'."); 237 238 } 239 240 261 public void existsUseWarnings(String tableName, String columnName, String jdbcType, boolean ignoreCase) throws SQLException, SQLWarning 262 { 263 if (tableName == null) throw new SQLException("TableName was null. You must specify a valid table name."); 264 if (columnName == null) throw new SQLException("Column name was null. You must specify a valid column name."); 265 266 ResultSet columns = getColumns(tableName); 267 268 if(columns == null) 269 { 270 try 272 { 273 PreparedStatement checkTable = m_connection.prepareStatement("Select * from " + tableName + " where 1=0"); 276 columns = checkTable.executeQuery(); 277 putColumns(tableName, columns); 278 } 279 catch(SQLException sqle) 280 { 281 if (sqle.getMessage().startsWith(m_ORA_EXCEPTION_1000) || sqle.getMessage().startsWith(m_ORA_EXCEPTION_604)) 282 { 283 System.out.println("Exceeded available Oracle cursors. Resetting connection and trying the SQL statement again..."); 284 resetConnection(); 285 existsUseWarnings(tableName, columnName, jdbcType, ignoreCase); 286 } 287 else 288 { 289 throw sqle; 291 } 292 } 293 } 294 295 ResultSetMetaData rsMeta = columns.getMetaData(); 296 int iColumns = rsMeta.getColumnCount(); 297 int jdbcTypeConst = this.getJdbcType(jdbcType); 298 for(int i = 1; i <= iColumns; i++) 299 { 300 if(ignoreCase) 301 { 302 if(columnName.equalsIgnoreCase(rsMeta.getColumnName(i))) 304 { 305 if(jdbcTypeConst != rsMeta.getColumnType(i)) 307 { 308 throw new SQLWarning("The column '" + tableName + "." + columnName + "' is of type '" + rsMeta.getColumnTypeName(i) + "' and cannot be mapped to the jdbc type '" + jdbcType + "'."); 309 } 310 else 311 { 312 return; 313 } 314 } 315 } 316 else 317 { 318 if(columnName.equals(rsMeta.getColumnName(i))) 320 { 321 if(jdbcTypeConst != rsMeta.getColumnType(i)) 323 { 324 throw new SQLWarning("The column '" + tableName + "." + columnName + "' is of type '" + rsMeta.getColumnTypeName(i) + "' and cannot be mapped to the jdbc type '" + jdbcType + "'."); 325 } 326 else 327 { 328 return; 329 } 330 } 331 332 } 333 334 } 336 337 throw new SQLException("The column '" + columnName + "' was not found in table '" + tableName + "'."); 338 339 } 340 341 342 356 357 public void exists(String tableName, String columnName, boolean ignoreCase) throws SQLException 358 { 359 if (tableName == null) throw new SQLException("TableName was null. You must specify a valid table name."); 360 if (columnName == null) throw new SQLException("Column name was null. You must specify a valid column name."); 361 362 ResultSet columns = getColumns(tableName); 363 364 if(columns == null) 365 { 366 try 368 { 369 PreparedStatement checkTable = m_connection.prepareStatement("Select * from " + tableName + " where 1=0"); 372 columns = checkTable.executeQuery(); 373 putColumns(tableName, columns); 374 } 375 catch(SQLException sqle) 376 { 377 if (sqle.getMessage().startsWith(m_ORA_EXCEPTION_1000) || sqle.getMessage().startsWith(m_ORA_EXCEPTION_604)) 378 { 379 System.out.println("Exceeded available Oracle cursors. Resetting connection and trying the SQL statement again..."); 380 resetConnection(); 381 exists(tableName, columnName, ignoreCase); 382 } 383 else 384 { 385 System.out.println(sqle.getMessage()); 386 throw sqle; 387 } 388 } 389 } 390 391 ResultSetMetaData rsMeta = columns.getMetaData(); 392 int iColumns = rsMeta.getColumnCount(); 393 for(int i = 1; i <= iColumns; i++) 394 { 395 if(ignoreCase) 396 { 397 if(columnName.equalsIgnoreCase(rsMeta.getColumnName(i))) 399 { 400 return; 401 } 402 } 403 else 404 { 405 if(columnName.equals(rsMeta.getColumnName(i))) 407 { 408 return; 409 } 410 411 } 412 413 } 415 416 throw new SQLException("The column '" + columnName + "' was not found in table '" + tableName + "'."); 417 418 } 419 420 421 432 public int getJdbcType(String ojbType) throws SQLException 433 { 434 int result; 435 if(ojbType == null) ojbType = ""; 436 ojbType = ojbType.toLowerCase(); 437 if (ojbType.equals("bit")) 438 result = Types.BIT; 439 else if (ojbType.equals("tinyint")) 440 result = Types.TINYINT; 441 else if (ojbType.equals("smallint")) 442 result = Types.SMALLINT; 443 else if (ojbType.equals("integer")) 444 result = Types.INTEGER; 445 else if (ojbType.equals("bigint")) 446 result = Types.BIGINT; 447 448 else if (ojbType.equals("float")) 449 result = Types.FLOAT; 450 else if (ojbType.equals("real")) 451 result = Types.REAL; 452 else if (ojbType.equals("double")) 453 result = Types.DOUBLE; 454 455 else if (ojbType.equals("numeric")) 456 result = Types.NUMERIC; 457 else if (ojbType.equals("decimal")) 458 result = Types.DECIMAL; 459 460 else if (ojbType.equals("char")) 461 result = Types.CHAR; 462 else if (ojbType.equals("varchar")) 463 result = Types.VARCHAR; 464 else if (ojbType.equals("longvarchar")) 465 result = Types.LONGVARCHAR; 466 467 else if (ojbType.equals("date")) 468 result = Types.DATE; 469 else if (ojbType.equals("time")) 470 result = Types.TIME; 471 else if (ojbType.equals("timestamp")) 472 result = Types.TIMESTAMP; 473 474 else if (ojbType.equals("binary")) 475 result = Types.BINARY; 476 else if (ojbType.equals("varbinary")) 477 result = Types.VARBINARY; 478 else if (ojbType.equals("longvarbinary")) 479 result = Types.LONGVARBINARY; 480 481 else if (ojbType.equals("clob")) 482 result = Types.CLOB; 483 else if (ojbType.equals("blob")) 484 result = Types.BLOB; 485 else 486 throw new SQLException( 487 "The type '"+ ojbType + "' is not a valid jdbc type."); 488 return result; 489 } 490 491 protected void finalize() 492 { 493 try 494 { 495 release(); 496 } 497 catch(Exception e) 498 { 499 e.printStackTrace(); 500 } 501 } 502 } 503 | Popular Tags |