KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > teams > jdbc > JdbcCar


1 /*
2 This file is part of the PolePosition database benchmark
3 http://www.polepos.org
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
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.framework.*;
26
27
28 /**
29  * @author Herkules
30  */

31 public class JdbcCar extends Car{
32     
33     private static Connection mConnection;
34     private static Statement mStatement;
35     
36     private final String JavaDoc mDBType;
37     private String JavaDoc mName;
38     
39     private final static Map<Class JavaDoc,String JavaDoc> mColTypesMap = new HashMap<Class JavaDoc,String JavaDoc>();
40     
41     static{
42         mColTypesMap.put( String JavaDoc.class, "VARCHAR(100)" );
43         mColTypesMap.put( Integer.TYPE, "INTEGER" );
44     }
45     
46     public JdbcCar( String JavaDoc dbtype ) throws CarMotorFailureException {
47         
48         mDBType = dbtype;
49         mWebsite = Jdbc.settings().getWebsite(mDBType);
50         mDescription = Jdbc.settings().getDescription(mDBType);
51         mName = Jdbc.settings().getName(mDBType);
52         
53         try{
54             Class.forName( Jdbc.settings().getDriverClass( mDBType )).newInstance();
55         }catch(Exception JavaDoc e){
56             e.printStackTrace();
57             throw new CarMotorFailureException();
58         }
59     }
60
61     public String JavaDoc name()
62     {
63         if(mName != null){
64             return mName;
65         }
66         return mDBType;
67     }
68
69     
70     public void openConnection() throws CarMotorFailureException
71     {
72         
73         try {
74             assert null == mConnection : "database has to be closed before opening";
75             mConnection = DriverManager.getConnection( Jdbc.settings().getConnectUrl( mDBType ),
76                         Jdbc.settings().getUsername( mDBType ), Jdbc.settings().getPassword( mDBType ) );
77             mConnection.setAutoCommit( false );
78             mStatement = mConnection.createStatement();
79         } catch (SQLException e) {
80             e.printStackTrace();
81             throw new CarMotorFailureException();
82         }
83     }
84
85     
86     /**
87      *
88      */

89     public void closeConnection()
90     {
91         
92         if(mStatement != null){
93             try {
94                 mStatement.close();
95             } catch (SQLException e) {
96                 e.printStackTrace();
97             }
98         }
99         
100         try
101         {
102             mConnection.commit();
103             mConnection.close();
104         }
105         catch ( SQLException sqlex )
106         {
107             sqlex.printStackTrace();
108         }
109         mConnection = null;
110     }
111     
112     
113     /**
114      * Commit changes.
115      */

116     public void commit()
117     {
118         try
119         {
120             mConnection.commit();
121         }
122         catch ( SQLException ex )
123         {
124             ex.printStackTrace();
125         }
126     }
127
128
129     /**
130      * Declarative statement executor
131      */

132     public void executeSQL( String JavaDoc sql )
133     {
134 // Log.logger.fine( sql );
135

136         Statement stmt = null;
137         try
138         {
139             stmt = mConnection.createStatement();
140             stmt.execute( sql );
141         }
142         catch ( SQLException ex )
143         {
144             ex.printStackTrace();
145         }
146         finally
147         {
148             if (stmt != null)
149             {
150                 try { stmt.close(); } catch (SQLException sqlEx) { stmt = null; }
151             }
152         }
153     }
154     
155     
156     /**
157      * Declarative statement executor
158      */

159     public ResultSet executeQuery( String JavaDoc sql )
160     {
161         Log.logger.fine( sql );
162
163         Statement stmt = null;
164         ResultSet rs = null;
165         try
166         {
167             stmt = mConnection.createStatement();
168             rs = stmt.executeQuery( sql );
169         }
170         catch ( SQLException ex )
171         {
172             ex.printStackTrace();
173         }
174         return rs;
175     }
176
177
178     /**
179      * Declarative statement executor
180      */

181     public ResultSet executeQueryForUpdate( String JavaDoc sql )
182     {
183         Log.logger.fine( sql );
184
185         Statement stmt = null;
186         ResultSet rs = null;
187         try
188         {
189             stmt = mConnection.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE );
190             rs = stmt.executeQuery( sql );
191         }
192         catch ( SQLException ex )
193         {
194             ex.printStackTrace();
195         }
196         return rs;
197     }
198     
199     public void executeUpdate(String JavaDoc sql){
200         
201         Log.logger.fine( sql );
202         
203         try {
204             mStatement.executeUpdate(sql);
205         } catch (SQLException e) {
206             e.printStackTrace();
207         }
208     }
209
210
211     /**
212      * Drop a certain table.
213      */

214     public void dropTable( String JavaDoc tablename )
215     {
216         Statement stmt = null;
217         try
218         {
219             stmt = mConnection.createStatement();
220             stmt.execute( "drop table " + tablename );
221         }
222         catch ( SQLException ex )
223         {
224             // intentionally empty
225
// don't bother about 'table does not exist'
226
}
227         finally
228         {
229             if (stmt != null)
230             {
231                 try { stmt.close(); } catch (SQLException sqlEx) { stmt = null; }
232             }
233         }
234     }
235
236
237     /**
238      * Create a new table, use the first column name as the primary key
239      */

240     public void createTable( String JavaDoc tablename, String JavaDoc[] colnames, Class JavaDoc[] coltypes )
241     {
242         String JavaDoc sql = "create table " + tablename
243                 + " (" + colnames[0] + " INTEGER NOT NULL";
244
245
246         for ( int i = 1; i < colnames.length; i++ )
247         {
248             sql += ", " + colnames[i] + " " + mColTypesMap.get( coltypes[i] );
249         }
250         sql += ", PRIMARY KEY(" + colnames[0] + "))";
251
252         executeSQL( sql );
253     }
254
255     public void createIndex( String JavaDoc tablename, String JavaDoc colname )
256     {
257         // The maximum length for index names is 18 for Derby.
258
String JavaDoc sql = "CREATE INDEX X" + tablename + "_" + colname + " ON " + tablename + " (" + colname + ")";
259         executeSQL( sql );
260     }
261
262
263     /**
264      * Retrieve a prepared statement.
265      */

266     public PreparedStatement prepareStatement( String JavaDoc sql )
267     {
268         PreparedStatement stmt = null;
269         try
270         {
271             stmt = mConnection.prepareStatement( sql );
272         }
273         catch ( SQLException ex )
274         {
275             ex.printStackTrace();
276         }
277         return stmt;
278     }
279
280
281 }
282
Popular Tags