KickJava   Java API By Example, From Geeks To Geeks.

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


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
package stockonline.ejb.sql;
42
43
44 import java.sql.*;
45 import javax.sql.DataSource JavaDoc;
46
47 import stockonline.util.QueryResult;
48
49 public class StockItem implements java.io.Serializable JavaDoc
50 {
51     final static boolean verbose = false;
52     private static QueryResult prices = new QueryResult();
53
54     public StockItem (){}
55
56     /** The SQL implementation to query stock prices by its ID
57     * @param conn The connection to the database
58     * @param stockID The iditifier of the stock to query
59     */

60     public static QueryResult queryByID (Connection conn, int stockID)
61         throws Exception JavaDoc
62     {
63         if(verbose) System.out.println("StockItem.queryByID(conn, " + stockID + ") called");
64
65         PreparedStatement pstmt = null;
66         ResultSet rs = null;
67
68         try
69         {
70             String JavaDoc sql = "SELECT current_val, high_val, low_val FROM StockItem WHERE stock_id = ?";
71             if(verbose) System.out.println(sql);
72
73             pstmt = conn.prepareStatement(sql);
74             pstmt.setInt(1, stockID);
75             rs = pstmt.executeQuery();
76
77             if(rs==null) throw new SQLException("queryByID.rs = null");
78             if(!rs.next()) throw new SQLException("Fail to query the prices for stockID = " + stockID);
79
80             prices.current_val = rs.getFloat(1);
81             prices.high_val = rs.getFloat(2);
82             prices.low_val = rs.getFloat(3);
83
84             if(verbose) print();
85             return prices;
86         }
87         catch(SQLException ex)
88         {
89             System.err.println("Exception in SockItem.queryByID(): " + ex.getMessage());
90             throw new Exception JavaDoc(ex.toString());
91         }
92         finally
93         {
94             if(rs!=null) rs.close();
95             if(pstmt!=null) pstmt.close();
96         }
97     }
98
99     /** The SQL implementation to query stock prices by its Code
100     * @param conn The connection to the database
101     * @param stockCode The code of the stock to query
102     */

103     public static QueryResult queryByCode (Connection conn, String JavaDoc stockCode)
104         throws Exception JavaDoc
105     {
106         if(verbose) System.out.println("StockItem.queryByCode(conn, " + stockCode + ") called");
107
108         PreparedStatement pstmt = null;
109         ResultSet rs = null;
110
111         try
112         {
113             String JavaDoc sql = "SELECT current_val, high_val, low_val FROM StockItem WHERE code = ?";
114             if(verbose) System.out.println(sql);
115
116             pstmt = conn.prepareStatement(sql);
117             pstmt.setString(1, stockCode);
118             rs = pstmt.executeQuery();
119
120             if(rs==null) throw new SQLException("queryByCode.rs = null");
121             if(!rs.next()) throw new SQLException("Fail to query the prices for stockCode = " + stockCode);
122
123             prices.current_val = rs.getFloat(1);
124             prices.high_val = rs.getFloat(2);
125             prices.low_val = rs.getFloat(3);
126
127             if(verbose) print();
128             return prices;
129         }
130         catch(SQLException ex)
131         {
132             System.err.println("Exception in SockItem.queryByCode(): " + ex.getMessage());
133             throw new Exception JavaDoc(ex.toString());
134         }
135         finally
136         {
137             if(rs!=null) rs.close();
138             if(pstmt!=null) pstmt.close();
139         }
140     }
141
142     /** The SQL implementation to query stock current price by its ID.
143     * @param conn The connection to the database
144     * @param stockID The iditifier of the stock to query
145     */

146     public static float getCurrentPrice (Connection conn, int stockID)
147         throws Exception JavaDoc
148     {
149
150         if(verbose) System.out.println("StockItem.getCurrentPrice(conn, " + stockID + ") called");
151
152         PreparedStatement pstmt = null;
153         ResultSet rs = null;
154
155         try
156         {
157             String JavaDoc sql = "SELECT current_val FROM StockItem WHERE stock_id = ?";
158             if(verbose) System.out.println(sql);
159
160             pstmt = conn.prepareStatement(sql);
161             pstmt.setInt(1, stockID);
162             rs = pstmt.executeQuery();
163
164             if(rs==null) throw new SQLException("queryCurrentPrice(): rs = null");
165             if(!rs.next()) throw new SQLException("Fail to query the prices for stockID = " + stockID);
166
167             prices.current_val = rs.getFloat(1);
168             if(verbose) System.out.println("Current price = " + prices.current_val);
169
170             return prices.current_val;
171         }
172         catch(SQLException ex)
173         {
174             System.err.println("Exception in SockItem.getCurrentPrice(): " + ex.getMessage());
175             throw new Exception JavaDoc(ex.toString());
176         }
177         finally
178         {
179             if(rs!=null) rs.close();
180             if(pstmt!=null) pstmt.close();
181         }
182     }
183
184     // Internal method to print all prices
185
//
186
private static void print()
187     {
188         System.out.println("Current price = " + prices.current_val);
189         System.out.println(" high price = " + prices.high_val);
190         System.out.println(" low price = " + prices.low_val);
191     }
192 }
193
194
Popular Tags