1 package sellwin.db; 2 3 import java.sql.*; 4 import java.util.*; 5 6 import sellwin.domain.*; 7 import sellwin.utils.*; 8 9 13 18 public class StateTaxDB extends DBType implements DBInterface { 19 20 private Connection con; 21 22 private final static String selectQuery = 23 "SELECT " + 24 "STATE_CODE, STATE_FULL_NAME, TAX_PCT, " + 25 "modified_by, modified_date " + 26 "FROM state_tax WHERE STATE_CODE="; 27 28 private final static String selectAllQuery = 29 "SELECT " + 30 "STATE_CODE, STATE_FULL_NAME, TAX_PCT, " + 31 "modified_by, modified_date " + 32 "FROM state_tax "; 33 34 private final static String updateQuery = 35 "UPDATE state_tax " + 36 "SET "; 37 38 private final static String insertQuery = 39 "INSERT INTO state_tax VALUES ( "; 40 41 private final static String deleteAllQuery = 42 "DELETE FROM state_tax"; 43 44 45 49 public StateTaxDB() { 50 } 51 52 56 public StateTaxDB(int dbType) { 57 DB_TYPE = dbType; 58 } 59 60 66 public StateTaxDB(Connection con) { 67 this.con = con; 68 } 69 70 75 public Connection getConnection() { 76 return this.con; 77 } 78 79 84 public void setConnection(Connection con) 85 throws SQLException { 86 87 this.con = con; 88 } 89 90 98 public final Object selectRow(Object pk) 99 throws SQLException { 100 101 StateTax tax = new StateTax(); 102 tax.setCode((String )pk); 103 Statement stmt = null; 104 ResultSet rs = null; 105 String query = selectQuery + "'" + tax.getCode() + "'"; 106 107 try { 108 stmt = con.createStatement(); 109 if (Prefs.DEBUG) LogWrite.write(query); 110 rs = stmt.executeQuery(query); 111 112 int i; 113 while (rs.next()) { 114 i=1; 115 tax.setFullName(rs.getString(i)); i++; 116 tax.setRate(rs.getDouble(i)); i++; 117 tax.setModifiedBy(rs.getString(i)); i++; 118 tax.setModifiedDate(rs.getDate(i)); 119 } 120 } catch (SQLException e) { 121 throw e; 122 } finally { 123 try { 124 if (rs != null) rs.close(); 125 } catch (SQLException x) { throw x; } 126 try { 127 if (stmt != null) stmt.close(); 128 } catch (SQLException x) { throw x; } 129 } 130 131 return tax; 132 } 133 134 135 144 public void updateRow(Object obj) 145 throws SQLException { 146 147 StateTax tax = (StateTax)obj; 148 149 Statement stmt = null; 150 StringBuffer query = new StringBuffer (updateQuery); 151 152 try { 153 stmt = con.createStatement(); 154 155 query.append("tax_pct="); 156 query.append(tax.getRate()); 157 query.append(",modified_by='"); 158 query.append(tax.getModifiedBy()).append("'"); 159 query.append(",modified_date="); 160 if (DB_TYPE == Prefs.MYSQL) 161 query.append("CURRENT_DATE "); 162 else 163 query.append("SYSDATE "); 164 query.append(" WHERE state_code='"); 165 query.append(tax.getCode()).append("'"); 166 167 if (Prefs.DEBUG) LogWrite.write(query.toString()); 168 int updatedRows = stmt.executeUpdate(query.toString()); 169 } catch (SQLException e) { 170 throw e; 171 } finally { 172 try { if (stmt != null) stmt.close(); 173 } catch (SQLException x) { } 174 } 175 } 176 177 186 public long insertRow(Object obj, boolean load) 187 throws SQLException { 188 189 StateTax st = (StateTax)obj; 190 191 Statement stmt = null; 192 StringBuffer query = new StringBuffer (insertQuery); 193 194 try { 195 stmt = con.createStatement(); 196 query.append(JDBC.quoteMore(st.getCode())); 197 query.append(st.getRate()).append(","); 198 query.append(JDBC.quoteMore(st.getFullName())); 199 query.append(JDBC.quoteMore(st.getModifiedBy())); 200 query.append(JDBC.quote(DateUtils.format(DB_TYPE, st.getModifiedDate()))); 201 query.append(")"); 202 if (Prefs.DEBUG) LogWrite.write(query.toString()); 203 int rc = stmt.executeUpdate(query.toString()); 204 } catch (SQLException e) { 205 throw e; 206 } finally { 207 try { if (stmt != null) stmt.close(); 208 } catch (SQLException x) {} 209 } 210 211 return 0L; 212 } 213 214 220 public final void deleteRow(Object obj) 221 throws SQLException { 222 223 } 224 225 229 public final void deleteAllRows() 230 throws SQLException { 231 232 Statement stmt = null; 233 try { 234 stmt = con.createStatement(); 235 if (Prefs.DEBUG) LogWrite.write(deleteAllQuery); 236 stmt.executeUpdate(deleteAllQuery); 237 } catch (SQLException e) { 238 throw e; 239 } finally { 240 try { if (stmt != null) stmt.close(); 241 } catch (SQLException x) { } 242 } 243 } 244 245 253 public final ArrayList selectAllRows(java.util.Date lastSyncDate) 254 throws SQLException { 255 256 ArrayList list = new ArrayList(); 257 StateTax tax; 258 Statement stmt = null; 259 ResultSet rs = null; 260 StringBuffer query = new StringBuffer (); 261 query.append(selectAllQuery); 262 if (lastSyncDate != null) { 263 query.append(" WHERE modified_date > "); 264 query.append(DateUtils.formatDateTime(DB_TYPE, lastSyncDate)); 265 } 266 query.append(" ORDER BY STATE_CODE"); 267 268 try { 269 stmt = con.createStatement(); 270 if (Prefs.DEBUG) LogWrite.write(query.toString()); 271 rs = stmt.executeQuery(query.toString()); 272 273 int i; 274 while (rs.next()) { 275 i=1; 276 tax = new StateTax(); 277 tax.setCode(rs.getString(i)); i++; 278 tax.setFullName(rs.getString(i)); i++; 279 tax.setRate(rs.getDouble(i)); i++; 280 tax.setModifiedBy(rs.getString(i)); i++; 281 tax.setModifiedDate(rs.getDate(i)); 282 list.add(tax); 283 } 284 } catch (SQLException e) { 285 throw e; 286 } finally { 287 try { 288 if (rs != null) rs.close(); 289 } catch (SQLException x) { throw x; } 290 try { 291 if (stmt != null) stmt.close(); 292 } catch (SQLException x) { throw x; } 293 } 294 295 return list; 296 } 297 302 public final void truncate() 303 throws SQLException { 304 305 String query = "truncate table state_tax"; 306 307 Statement stmt = null; 308 try { 309 stmt = con.createStatement(); 310 if (Prefs.DEBUG) LogWrite.write(query); 311 stmt.executeUpdate(query); 312 } catch (SQLException e) { 313 throw e; 314 } finally { 315 try { if (stmt != null) stmt.close(); 316 } catch (SQLException x) { } 317 } 318 } 319 320 } 321 | Popular Tags |