KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stockonline > ejb > sql > Account


1 /*
2  * StockOnline: EJB 1.1 Benchmark.
3  *
4  * Copyright Commonwealth Scientific and Industrial Research Organisation (CSIRO - www.csiro.au), Australia 2001, 2002, 2003.
5  *
6  * Contact: Paul.Brebner@csiro.au
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or any
11  * later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * Originally developed for the CSIRO Middleware Technology Evaluation (MTE) Project, by
23  * the Software Architectures and Component Technologies Group, CSIRO Mathematical and Information Sciences
24  * Canberra and Sydney, Australia
25  *
26  * www.cmis.csiro.au/sact/
27  * www.cmis.csiro.au/adsat/mte.htm
28  *
29  * Initial developer(s): Shiping Chen, Paul Brebner, Lei Hu, Shuping Ran, Ian Gorton, Anna Liu.
30  * Contributor(s): ______________________.
31  */

32
33
34 //
35
//
36
// History:
37
// 10/08/2001 Shiping Initial coding based on the existing code
38
//
39
//
40
//
41

42
43 package stockonline.ejb.sql;
44
45 import java.sql.*;
46 import javax.sql.DataSource JavaDoc;
47
48 public class Account
49 {
50     /** A boolean varible to control debug, default is true
51       */

52     final static boolean verbose = false;
53     final static String JavaDoc className = "Account";
54
55     public Account() {}
56
57     public static int createAccount (Connection conn, String JavaDoc name, String JavaDoc address, int credit) throws Exception JavaDoc
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 JavaDoc
69     {
70         if(verbose) System.out.println(className + ".updateCredit() called");
71
72         PreparedStatement pstmt = null;
73
74             try
75         {
76             String JavaDoc 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 JavaDoc(ex.toString());
90             }
91     }
92
93         public static float getCredit (Connection conn, int accountID) throws Exception JavaDoc
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 JavaDoc 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 JavaDoc(ex.getMessage());
121         }
122         finally
123         {
124             if(rs!=null) rs.close();
125             if(pstmt!=null) pstmt.close();
126         }
127     }
128
129     // Interanl methods
130
//
131
private static void createAccountRecord( Connection conn,
132                             int sub_accno,
133                             String JavaDoc sub_name,
134                             String JavaDoc sub_address,
135                             int sub_credit)
136     throws Exception JavaDoc
137     {
138         if(verbose) System.out.println(className + ".createAccountRecord() called");
139
140         PreparedStatement pstmt = null;
141
142             try
143         {
144             String JavaDoc 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 JavaDoc(ex.toString());
160             }
161     }
162 }
Popular Tags