1 32 33 34 42 43 package stockonline.ejb.sql; 44 45 import java.sql.*; 46 import javax.sql.DataSource ; 47 48 public class Account 49 { 50 52 final static boolean verbose = false; 53 final static String className = "Account"; 54 55 public Account() {} 56 57 public static int createAccount (Connection conn, String name, String address, int credit) throws Exception 58 { 59 if(verbose) System.out.println(className + ".createAccount() called"); 60 61 int accountID = SeqGenerator.getNextAccountID(conn); 62 createAccountRecord(conn, accountID, name, address, credit); 63 if(verbose) System.out.println(className + ".createAccount(): accountId = " + accountID + " created"); 64 65 return accountID; 66 } 67 68 public static void updateCredit( Connection conn, int accountID, int credit) throws Exception 69 { 70 if(verbose) System.out.println(className + ".updateCredit() called"); 71 72 PreparedStatement pstmt = null; 73 74 try 75 { 76 String sql = "UPDATE subaccount SET sub_credit = ? WHERE sub_accno = ?"; 77 if(verbose) System.out.println(sql); 78 79 pstmt = conn.prepareStatement(sql); 80 pstmt.setInt(1, credit); 81 pstmt.setInt(2, accountID); 82 pstmt.executeUpdate(); 83 84 pstmt.close(); 85 } 86 catch (SQLException ex) 87 { 88 System.err.println(className + ".updateCredit(): " + ex.toString()); 89 throw new Exception (ex.toString()); 90 } 91 } 92 93 public static float getCredit (Connection conn, int accountID) throws Exception 94 { 95 if(verbose) System.out.println("SubAccount.getCredit(conn, " + accountID + ") called"); 96 97 PreparedStatement pstmt = null; 98 ResultSet rs = null; 99 100 try 101 { 102 String sql = "SELECT sub_credit FROM Subaccount WHERE sub_accno = ?"; 103 if(verbose) System.out.println(sql); 104 105 pstmt = conn.prepareStatement(sql); 106 pstmt.setInt(1, accountID); 107 rs = pstmt.executeQuery(); 108 109 if(rs==null) throw new SQLException("SubAccount.getCredit(): rs = null"); 110 if(!rs.next()) throw new SQLException("Fail to get current credit for accountID = " + accountID); 111 112 float currentCredit= rs.getFloat(1); 113 if(verbose) System.out.println("currentCredit = " + currentCredit); 114 115 return currentCredit; 116 } 117 catch(SQLException ex) 118 { 119 System.err.println("Exception in SubAccount.getCredit(): " + ex.getMessage()); 120 throw new Exception (ex.getMessage()); 121 } 122 finally 123 { 124 if(rs!=null) rs.close(); 125 if(pstmt!=null) pstmt.close(); 126 } 127 } 128 129 private static void createAccountRecord( Connection conn, 132 int sub_accno, 133 String sub_name, 134 String sub_address, 135 int sub_credit) 136 throws Exception 137 { 138 if(verbose) System.out.println(className + ".createAccountRecord() called"); 139 140 PreparedStatement pstmt = null; 141 142 try 143 { 144 String sql = "INSERT INTO subaccount VALUES (?,?,?,?)"; 145 if(verbose) System.out.println(sql); 146 147 pstmt = conn.prepareStatement(sql); 148 pstmt.setInt(1, sub_accno); 149 pstmt.setString(2, sub_name); 150 pstmt.setString(3, sub_address); 151 pstmt.setFloat(4, sub_credit); 152 pstmt.executeUpdate(); 153 154 pstmt.close(); 155 } 156 catch (SQLException ex) 157 { 158 System.err.println(className + ".createAccountRecord(): " + ex.toString()); 159 throw new Exception (ex.toString()); 160 } 161 } 162 } | Popular Tags |