001: /*
002: This file is part of the PolePosition database benchmark
003: http://www.polepos.org
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public
016: License along with this program; if not, write to the Free
017: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018: MA 02111-1307, USA. */
019:
020: package org.polepos.teams.jdbc;
021:
022: import java.sql.*;
023: import java.util.*;
024:
025: import org.polepos.framework.*;
026:
027: /**
028: * @author Herkules
029: */
030: public class JdbcCar extends Car {
031:
032: private static Connection mConnection;
033: private static Statement mStatement;
034:
035: private final String mDBType;
036: private String mName;
037:
038: private final static Map<Class, String> mColTypesMap = new HashMap<Class, String>();
039:
040: static {
041: mColTypesMap.put(String.class, "VARCHAR(100)");
042: mColTypesMap.put(Integer.TYPE, "INTEGER");
043: }
044:
045: public JdbcCar(String dbtype) throws CarMotorFailureException {
046:
047: mDBType = dbtype;
048: mWebsite = Jdbc.settings().getWebsite(mDBType);
049: mDescription = Jdbc.settings().getDescription(mDBType);
050: mName = Jdbc.settings().getName(mDBType);
051:
052: try {
053: Class.forName(Jdbc.settings().getDriverClass(mDBType))
054: .newInstance();
055: } catch (Exception e) {
056: e.printStackTrace();
057: throw new CarMotorFailureException();
058: }
059: }
060:
061: public String name() {
062: if (mName != null) {
063: return mName;
064: }
065: return mDBType;
066: }
067:
068: public void openConnection() throws CarMotorFailureException {
069:
070: try {
071: assert null == mConnection : "database has to be closed before opening";
072: mConnection = DriverManager.getConnection(Jdbc.settings()
073: .getConnectUrl(mDBType), Jdbc.settings()
074: .getUsername(mDBType), Jdbc.settings().getPassword(
075: mDBType));
076: mConnection.setAutoCommit(false);
077: mStatement = mConnection.createStatement();
078: } catch (SQLException e) {
079: e.printStackTrace();
080: throw new CarMotorFailureException();
081: }
082: }
083:
084: /**
085: *
086: */
087: public void closeConnection() {
088:
089: if (mStatement != null) {
090: try {
091: mStatement.close();
092: } catch (SQLException e) {
093: e.printStackTrace();
094: }
095: }
096:
097: try {
098: mConnection.commit();
099: mConnection.close();
100: } catch (SQLException sqlex) {
101: sqlex.printStackTrace();
102: }
103: mConnection = null;
104: }
105:
106: /**
107: * Commit changes.
108: */
109: public void commit() {
110: try {
111: mConnection.commit();
112: } catch (SQLException ex) {
113: ex.printStackTrace();
114: }
115: }
116:
117: /**
118: * Declarative statement executor
119: */
120: public void executeSQL(String sql) {
121: // Log.logger.fine( sql );
122:
123: Statement stmt = null;
124: try {
125: stmt = mConnection.createStatement();
126: stmt.execute(sql);
127: } catch (SQLException ex) {
128: ex.printStackTrace();
129: } finally {
130: if (stmt != null) {
131: try {
132: stmt.close();
133: } catch (SQLException sqlEx) {
134: stmt = null;
135: }
136: }
137: }
138: }
139:
140: /**
141: * Declarative statement executor
142: */
143: public ResultSet executeQuery(String sql) {
144: Log.logger.fine(sql);
145:
146: Statement stmt = null;
147: ResultSet rs = null;
148: try {
149: stmt = mConnection.createStatement();
150: rs = stmt.executeQuery(sql);
151: } catch (SQLException ex) {
152: ex.printStackTrace();
153: }
154: return rs;
155: }
156:
157: /**
158: * Declarative statement executor
159: */
160: public ResultSet executeQueryForUpdate(String sql) {
161: Log.logger.fine(sql);
162:
163: Statement stmt = null;
164: ResultSet rs = null;
165: try {
166: stmt = mConnection.createStatement(
167: ResultSet.TYPE_FORWARD_ONLY,
168: ResultSet.CONCUR_UPDATABLE);
169: rs = stmt.executeQuery(sql);
170: } catch (SQLException ex) {
171: ex.printStackTrace();
172: }
173: return rs;
174: }
175:
176: public void executeUpdate(String sql) {
177:
178: Log.logger.fine(sql);
179:
180: try {
181: mStatement.executeUpdate(sql);
182: } catch (SQLException e) {
183: e.printStackTrace();
184: }
185: }
186:
187: /**
188: * Drop a certain table.
189: */
190: public void dropTable(String tablename) {
191: Statement stmt = null;
192: try {
193: stmt = mConnection.createStatement();
194: stmt.execute("drop table " + tablename);
195: } catch (SQLException ex) {
196: // intentionally empty
197: // don't bother about 'table does not exist'
198: } finally {
199: if (stmt != null) {
200: try {
201: stmt.close();
202: } catch (SQLException sqlEx) {
203: stmt = null;
204: }
205: }
206: }
207: }
208:
209: /**
210: * Create a new table, use the first column name as the primary key
211: */
212: public void createTable(String tablename, String[] colnames,
213: Class[] coltypes) {
214: String sql = "create table " + tablename + " (" + colnames[0]
215: + " INTEGER NOT NULL";
216:
217: for (int i = 1; i < colnames.length; i++) {
218: sql += ", " + colnames[i] + " "
219: + mColTypesMap.get(coltypes[i]);
220: }
221: sql += ", PRIMARY KEY(" + colnames[0] + "))";
222:
223: executeSQL(sql);
224: }
225:
226: public void createIndex(String tablename, String colname) {
227: // The maximum length for index names is 18 for Derby.
228: String sql = "CREATE INDEX X" + tablename + "_" + colname
229: + " ON " + tablename + " (" + colname + ")";
230: executeSQL(sql);
231: }
232:
233: /**
234: * Retrieve a prepared statement.
235: */
236: public PreparedStatement prepareStatement(String sql) {
237: PreparedStatement stmt = null;
238: try {
239: stmt = mConnection.prepareStatement(sql);
240: } catch (SQLException ex) {
241: ex.printStackTrace();
242: }
243: return stmt;
244: }
245:
246: }
|