1 32 33 34 package stockonline.ejb.sql; 42 43 44 import java.sql.*; 45 import javax.sql.DataSource ; 46 47 import stockonline.util.QueryResult; 48 49 public class StockItem implements java.io.Serializable 50 { 51 final static boolean verbose = false; 52 private static QueryResult prices = new QueryResult(); 53 54 public StockItem (){} 55 56 60 public static QueryResult queryByID (Connection conn, int stockID) 61 throws Exception 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 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 (ex.toString()); 91 } 92 finally 93 { 94 if(rs!=null) rs.close(); 95 if(pstmt!=null) pstmt.close(); 96 } 97 } 98 99 103 public static QueryResult queryByCode (Connection conn, String stockCode) 104 throws Exception 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 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 (ex.toString()); 134 } 135 finally 136 { 137 if(rs!=null) rs.close(); 138 if(pstmt!=null) pstmt.close(); 139 } 140 } 141 142 146 public static float getCurrentPrice (Connection conn, int stockID) 147 throws Exception 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 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 (ex.toString()); 176 } 177 finally 178 { 179 if(rs!=null) rs.close(); 180 if(pstmt!=null) pstmt.close(); 181 } 182 } 183 184 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 |