1 27 28 29 package com.sun.ebank.util; 30 31 import java.sql.*; 32 import javax.sql.*; 33 import com.sun.ebank.util.*; 34 import com.sun.ebank.ejb.exception.*; 35 36 37 41 public final class DBHelper { 42 public static final String getNextAccountId(Connection con) 45 throws SQLException, MissingPrimaryKeyException { 46 Debug.print("DBHelper getNextAccountId"); 47 48 return getNextId(con, "next_account_id"); 49 } 50 52 public static final String getNextCustomerId(Connection con) 53 throws SQLException, MissingPrimaryKeyException { 54 Debug.print("DBHelper getNextCustomerId"); 55 56 return getNextId(con, "next_customer_id"); 57 } 58 60 public static final String getNextTxId(Connection con) 61 throws SQLException, MissingPrimaryKeyException { 62 Debug.print("DBHelper getNextTxId"); 63 64 return getNextId(con, "next_tx_id"); 65 } 66 68 private static final String getNextId(Connection con, String table) 69 throws SQLException, MissingPrimaryKeyException { 70 Debug.print("DBHelper getNextId"); 71 72 String selectStatement = "select max(id) from " + table; 73 String updateStatement = "update " + table + " " + "set id = id + 1 "; 74 75 PreparedStatement prepSelect = con.prepareStatement(selectStatement); 76 PreparedStatement prepUpdate = con.prepareStatement(updateStatement); 77 78 prepUpdate.executeUpdate(); 79 80 ResultSet rs = prepSelect.executeQuery(); 81 rs.next(); 82 83 int i = rs.getInt(1); 84 rs.close(); 85 prepSelect.close(); 86 prepUpdate.close(); 87 88 if (i <= 0) { 89 throw new MissingPrimaryKeyException("Table " + table + 90 " is empty."); 91 } 92 93 return Integer.toString(i); 94 } 95 97 public static final java.sql.Date toSQLDate(java.util.Date inDate) { 98 return new java.sql.Date (inDate.getTime()); 100 } 101 } 102 | Popular Tags |