1 19 20 package org.polepos.teams.jdbc; 21 22 23 24 25 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 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 39 public class BahrainJdbc extends JdbcDriver implements BahrainDriver 40 { 41 44 private final static int BULKSIZE = 1000; 45 46 private static final String TABLE = "bahrain"; 47 48 public void takeSeatIn(Car car, TurnSetup setup) throws CarMotorFailureException 49 { 50 super.takeSeatIn(car, setup); 51 52 jdbcCar().openConnection(); 53 jdbcCar().dropTable( TABLE); 57 jdbcCar().createTable( TABLE, new String []{ "id", "Name", "FirstName", "Points", "LicenseID" }, 58 new Class []{Integer.TYPE, String .class, String .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 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 ); } 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 146 public void update(){ 147 148 int updateCount = setup().getUpdateCount(); 149 150 updateIndexedStringStmt(updateCount); 151 } 152 153 156 public void delete(){ 157 158 int count = setup().getObjectCount(); 159 160 PreparedStatement 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 e) { 169 e.printStackTrace(); 170 } 171 172 jdbcCar().commit(); 173 } 174 175 178 private void updateIndexedStringUpdateRow(int updateCount) 179 { 180 try{ 181 ResultSet 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 sqlex ){ 191 sqlex.printStackTrace(); 192 } 193 jdbcCar().commit(); 194 } 195 196 197 200 private void updateIndexedStringStmt(int updateCount) 201 { 202 try 203 { 204 PreparedStatement stmt = jdbcCar().prepareStatement( "update bahrain set Name=? where ID=?" ); 205 ResultSet 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 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 sqlex ) 220 { 221 sqlex.printStackTrace(); 222 } 223 jdbcCar().commit(); 224 } 225 226 227 228 } 229 | Popular Tags |