KickJava   Java API By Example, From Geeks To Geeks.

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


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
23
24
25
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import org.polepos.circuits.bahrain.*;
31 import org.polepos.data.*;
32 import org.polepos.framework.*;
33 import org.polepos.teams.jdbc.drivers.melbourne.*;
34
35
36 /**
37  * @author Herkules
38  */

39 public class BahrainJdbc extends JdbcDriver implements BahrainDriver
40 {
41     /**
42      * Number of pilot to be written at once.
43      */

44     private final static int BULKSIZE = 1000;
45     
46     private static final String JavaDoc TABLE = "bahrain";
47
48     public void takeSeatIn(Car car, TurnSetup setup) throws CarMotorFailureException
49     {
50         super.takeSeatIn(car, setup);
51
52         jdbcCar().openConnection();
53         //
54
// Create database structure
55
//
56
jdbcCar().dropTable( TABLE);
57         jdbcCar().createTable( TABLE, new String JavaDoc[]{ "id", "Name", "FirstName", "Points", "LicenseID" },
58                     new Class JavaDoc[]{Integer.TYPE, String JavaDoc.class, String JavaDoc.class, Integer.TYPE, Integer.TYPE} );
59         jdbcCar().createIndex( TABLE, "Name" );
60         jdbcCar().createIndex( TABLE, "LicenseID" );
61
62         jdbcCar().closeConnection();
63     }
64     
65     
66     public void write()
67     {
68         Pilot[] pilots = new Pilot[ BULKSIZE ];
69         int idx = 0;
70         
71         // with a little help from Australia
72
BulkWriteStrategy writer = new BulkWritePreparedStatement(jdbcCar(), TABLE);
73     
74         int commitctr = 0;
75         int count = setup().getObjectCount();
76         int commitInterval = setup().getCommitInterval();
77         for ( int i = 1; i <= count; i++ ){
78         
79             pilots[ idx++ ] = new Pilot( "Pilot_" + i, "Jonny_" + i, i , i );
80             
81             if ( idx == BULKSIZE || i == count){
82                 writer.savePilots(TABLE, pilots, idx, i - idx + 1 );
83                 idx = 0;
84             }
85             
86             if ( commitInterval > 0 && ++commitctr >= commitInterval ){
87                 commitctr = 0;
88                 jdbcCar().commit();
89                 Log.logger.fine( "commit while writing at " + i+1 ); //NOI18N
90
}
91             
92             addToCheckSum(i);
93             
94         }
95         
96         jdbcCar().commit();
97     }
98     
99     
100     public void query_indexed_string(){
101         
102         int count = setup().getSelectCount();
103         
104         for (int i = 1; i <= count; i++) {
105             performQuery( "select * from bahrain where Name='Pilot_" + i + "'" );
106         }
107         
108     }
109
110     
111     public void query_string(){
112         
113         int count = setup().getSelectCount();
114         
115         for (int i = 1; i <= count; i++) {
116             performQuery( "select * from bahrain where FirstName='Jonny_" + i + "'" );
117         }
118         
119     }
120     
121
122     public void query_indexed_int(){
123
124         int count = setup().getSelectCount();
125         
126         for (int i = 1; i <= count; i++) {
127             performQuery( "select * from bahrain where LicenseID=" + i );
128         }
129         
130     }
131
132     
133     public void query_int(){
134         
135         int count = setup().getSelectCount();
136         
137         for (int i = 1; i <= count; i++) {
138             performQuery( "select * from bahrain where Points=" + i );
139         }
140         
141     }
142     
143     /**
144      * Some JDBC implementations don't support ResultSet#updateRow(), so an alternative method can be used.
145      */

146     public void update(){
147         
148         int updateCount = setup().getUpdateCount();
149         
150         updateIndexedStringStmt(updateCount);
151     }
152
153     /**
154      * deleting one at a time, simulating deleting individual objects
155      */

156     public void delete(){
157         
158         int count = setup().getObjectCount();
159         
160         PreparedStatement JavaDoc statement = jdbcCar().prepareStatement("delete from bahrain where id=?");
161         
162         try {
163             for (int i = 1; i <= count; i++) {
164                 statement.setObject(1,i);
165                 statement.execute();
166                 addToCheckSum(1);
167             }
168         } catch (SQLException JavaDoc e) {
169             e.printStackTrace();
170         }
171         
172         jdbcCar().commit();
173     }
174     
175     /**
176      * do the update using the ResultSet#updateRow() method
177      */

178     private void updateIndexedStringUpdateRow(int updateCount)
179     {
180         try{
181             ResultSet JavaDoc rs = jdbcCar().executeQueryForUpdate( "select ID, Name from bahrain" );
182
183             for (int i = 1; i <= updateCount ; i++) {
184                 rs.next();
185                 rs.updateString( 2, rs.getString( 2 ).toUpperCase() );
186                 rs.updateRow();
187                 addToCheckSum(1);
188             }
189         }
190         catch ( SQLException JavaDoc sqlex ){
191             sqlex.printStackTrace();
192         }
193         jdbcCar().commit();
194     }
195     
196     
197     /**
198      * do the update using an 'update' SQL statement
199      */

200     private void updateIndexedStringStmt(int updateCount)
201     {
202         try
203         {
204             PreparedStatement JavaDoc stmt = jdbcCar().prepareStatement( "update bahrain set Name=? where ID=?" );
205             ResultSet JavaDoc rs = jdbcCar().executeQuery( "select ID, Name from bahrain" );
206
207             for (int i = 0; i < updateCount ; i++) {
208                 rs.next();
209                 int id = rs.getInt( 1 );
210                 String JavaDoc name = rs.getString( 2 ).toUpperCase();
211                 stmt.setString( 1, name );
212                 stmt.setInt( 2, id );
213                 stmt.addBatch();
214                 addToCheckSum(1);
215             }
216             stmt.executeBatch();
217             stmt.close();
218         }
219         catch ( SQLException JavaDoc sqlex )
220         {
221             sqlex.printStackTrace();
222         }
223         jdbcCar().commit();
224     }
225
226     
227     
228 }
229
Popular Tags