01: /*
02: This file is part of the PolePosition database benchmark
03: http://www.polepos.org
04:
05: This program is free software; you can redistribute it and/or
06: modify it under the terms of the GNU General Public License
07: as published by the Free Software Foundation; either version 2
08: of the License, or (at your option) any later version.
09:
10: This program is distributed in the hope that it will be useful,
11: but WITHOUT ANY WARRANTY; without even the implied warranty of
12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: GNU General Public License for more details.
14:
15: You should have received a copy of the GNU General Public
16: License along with this program; if not, write to the Free
17: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18: MA 02111-1307, USA. */
19:
20: package org.polepos.teams.jdbc;
21:
22: import java.sql.*;
23: import java.util.*;
24:
25: import org.polepos.data.*;
26: import org.polepos.framework.*;
27:
28: /**
29: * @author Herkules
30: */
31: public abstract class JdbcDriver extends org.polepos.framework.Driver {
32:
33: public void prepare() throws CarMotorFailureException {
34: ((JdbcCar) car()).openConnection();
35: }
36:
37: public void backToPit() {
38: ((JdbcCar) car()).closeConnection();
39: }
40:
41: public JdbcCar jdbcCar() {
42: return (JdbcCar) car();
43: }
44:
45: /**
46: * Helper: perform any query
47: */
48: protected void performQuery(String sql) {
49: Log.logger.fine("starting query"); //NOI18N
50:
51: try {
52: ResultSet rs = jdbcCar().executeQuery(sql);
53:
54: while (rs.next()) {
55: Pilot p = new Pilot(rs.getString(2), rs.getString(3),
56: rs.getInt(4), rs.getInt(5));
57: addToCheckSum(p.checkSum());
58: }
59: } catch (SQLException sqlex) {
60: sqlex.printStackTrace();
61: }
62: }
63:
64: protected <Value> void performSingleResultQuery(String sql,
65: List<Value> values) {
66: Log.logger.fine("starting query"); //NOI18N
67: PreparedStatement stat = jdbcCar().prepareStatement(sql);
68: try {
69: for (Value val : values) {
70: stat.setObject(1, val);
71: ResultSet rs = stat.executeQuery();
72: if (!rs.next()) {
73: System.err
74: .println("Expected one result, received none: "
75: + val);
76: }
77: Pilot p = new Pilot(rs.getString(2), rs.getString(3),
78: rs.getInt(4), rs.getInt(5));
79: addToCheckSum(p.checkSum());
80: if (rs.next()) {
81: System.err
82: .println("Expected one result, received multiple: "
83: + val);
84: }
85: }
86: } catch (SQLException sqlexc) {
87: sqlexc.printStackTrace();
88: } finally {
89: try {
90: stat.close();
91: } catch (SQLException e) {
92: e.printStackTrace();
93: }
94: }
95: }
96:
97: }
|