KickJava   Java API By Example, From Geeks To Geeks.

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


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

35 public class MelbourneJdbc extends JdbcDriver implements MelbourneDriver
36 {
37     /**
38      * Number of pilot to be written at once.
39      */

40     private final static int BULKSIZE = 1000;
41
42     
43     public void takeSeatIn(Car car, TurnSetup setup) throws CarMotorFailureException{
44         super.takeSeatIn(car, setup);
45         
46         jdbcCar().openConnection();
47         
48         //
49
// Create database structure
50
//
51
jdbcCar().dropTable( "australia" );
52         jdbcCar().createTable( "australia", new String JavaDoc[]{ "ID", "Name", "FirstName", "Points", "LicenseID" },
53                     new Class JavaDoc[]{Integer.TYPE, String JavaDoc.class, String JavaDoc.class, Integer.TYPE, Integer.TYPE} );
54
55         jdbcCar().closeConnection();
56     }
57     
58     public void write(){
59         
60         int numobjects = setup().getObjectCount();
61         int commitintervall = setup().getCommitInterval();
62         int commitctr = 0;
63         
64         Pilot[] pilots = new Pilot[ BULKSIZE ];
65         int idx = 0;
66         
67         //BulkWriteStrategy writer = new BulkWriteSingle();
68
BulkWriteStrategy writer = new BulkWritePreparedStatement( jdbcCar(), "australia" );
69         //BulkWriteStrategy writer = new BulkWriteMultiValue();
70

71         for ( int i = 1; i <= numobjects; i++ )
72         {
73             Pilot p = new Pilot( "Pilot_" + i, "Herkules", i, i );
74             pilots[ idx++ ] = p;
75             if ( idx == BULKSIZE )
76             {
77                 writer.savePilots("australia", pilots, idx, i - idx + 1 );
78                 idx = 0;
79             }
80             
81             if ( commitintervall > 0 && ++commitctr >= commitintervall )
82             {
83                 commitctr = 0;
84                 jdbcCar().commit();
85                 Log.logger.fine ( "commit while writing at " + i+1 ); //NOI18N
86
}
87             
88             addToCheckSum(i);
89         }
90         
91         // Write the rest
92
writer.savePilots("australia", pilots, idx, numobjects - idx );
93
94         jdbcCar().commit();
95     }
96     
97     public void read(){
98         
99         int numobjects = setup().getObjectCount();
100         
101         try{
102             ResultSet JavaDoc rs = jdbcCar().executeQuery("select * from australia");
103
104             for ( int i = 0; i < numobjects; i++ ){
105                 rs.next();
106                 Pilot p = new Pilot( rs.getString( 2 ), rs.getString( 3 ), rs.getInt( 4 ), rs.getInt( 5 ) );
107                 addToCheckSum(p.getPoints());
108             }
109         }
110         catch ( SQLException JavaDoc sqlex ){
111             sqlex.printStackTrace();
112         }
113     }
114     
115     public void read_hot() {
116         read();
117     }
118     
119     public void delete(){
120         jdbcCar().executeSQL( "delete from australia" );
121         jdbcCar().commit();
122     }
123
124 }
125
Popular Tags