KickJava   Java API By Example, From Geeks To Geeks.

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


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
// 19/11/2001 Shiping Change dateFormatter to dynamic varible due to
40
// that java.text.SimpleDateFormat is not thread safe.
41
//
42
//
43

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

53     final static boolean verbose = false;
54         private java.text.SimpleDateFormat JavaDoc dateFormatter = new java.text.SimpleDateFormat JavaDoc ("dd/MM/yy");
55
56         public StockTx() {}
57
58         /**
59     * addTransaction with internal txID
60     */

61     public int create ( Connection conn,
62                     String JavaDoc txType,
63                     int accountID,
64                     int stockID,
65                     int amount,
66                     float price)
67     throws Exception JavaDoc
68     {
69         if(verbose)
70         {
71             System.out.println("Transaction.create(Connection conn) called");
72             System.out.println("txType = " + txType);
73             System.out.println("accountID = " + accountID);
74             System.out.println("stockID = " + stockID);
75             System.out.println("amount = " + amount);
76             System.out.println("price = " + price);
77         }
78
79         int txID = SeqGenerator.getNextTxID(conn);
80         if(verbose) System.out.println("generated txID = " + txID);
81
82         create_Imp(conn, txID, txType, accountID, stockID, amount, price);
83
84         return txID;
85     }
86
87     // Internal methods
88
//
89
private void create_Imp ( Connection conn,
90                         int trans_id,
91                         String JavaDoc trans_type,
92                         int sub_accno,
93                         int stock_id,
94                         int amount,
95                         float price)
96     throws Exception JavaDoc
97     {
98         PreparedStatement pstmt = null;
99
100         try
101         {
102             String JavaDoc sql = "INSERT INTO StockTransaction VALUES (?, ?, ?, ?, ?, ?, ?)";
103             if(verbose) System.out.println(sql);
104
105             String JavaDoc currentDate = dateFormatter.format( new java.util.Date JavaDoc() );
106             if(verbose) System.out.println(currentDate);
107
108                 pstmt = conn.prepareStatement(sql);
109             pstmt.setInt(1, trans_id);
110             pstmt.setString(2,trans_type);
111             pstmt.setInt(3, sub_accno);
112             pstmt.setInt(4, stock_id);
113             pstmt.setInt(5, amount);
114             pstmt.setFloat(6, price);
115             pstmt.setString(7,currentDate);
116
117             pstmt.executeUpdate();
118         }
119         catch(SQLException ex)
120         {
121             System.err.println("Exception in StockTransaction.create_Imp(): " + ex.getMessage());
122             throw new Exception JavaDoc(ex.toString());
123         }
124         finally
125         {
126             if(pstmt!=null) pstmt.close();
127         }
128     }
129 }
Popular Tags