1 19 20 package org.polepos.teams.jdbc; 21 22 import java.sql.*; 23 24 import org.polepos.circuits.barcelona.*; 25 import org.polepos.framework.*; 26 27 28 29 public class BarcelonaJdbc extends JdbcDriver implements BarcelonaDriver { 30 31 private static final String [] TABLES = new String []{ 32 "barcelona0", 33 "barcelona1", 34 "barcelona2", 35 "barcelona3", 36 "barcelona4", 37 }; 38 39 public void takeSeatIn(Car car, TurnSetup setup) throws CarMotorFailureException{ 40 41 super.takeSeatIn(car, setup); 42 43 jdbcCar().openConnection(); 44 45 int i = 0; 46 for(String table : TABLES){ 47 jdbcCar().dropTable( table); 48 jdbcCar().createTable( table, new String []{ "id", "parent", "b" + i}, 49 new Class []{Integer.TYPE, Integer.TYPE, Integer.TYPE} ); 50 jdbcCar().createIndex( table, "parent" ); 51 if(i == 2){ 52 jdbcCar().createIndex( table, "b2" ); 53 } 54 i++; 55 } 56 jdbcCar().closeConnection(); 57 58 } 59 60 61 public void write() { 62 63 try{ 64 PreparedStatement[] statements = new PreparedStatement[5]; 65 for (int i = 0; i < 5; i++) { 66 statements[i] = jdbcCar().prepareStatement("insert into " + TABLES[i] + " (id, parent, b" + i + ") values (?,?,?)"); 67 } 68 69 int count = setup().getObjectCount(); 70 if(count > 0){ 71 for (int i = 1; i<= count; i++) { 72 B4 b4 = new B4(); 73 b4.setAll(i); 74 for (int j = 0; j < 5; j++) { 75 statements[j].setInt(1, i); 76 statements[j].setInt(2, i); 77 statements[j].setInt(3, b4.getBx(j)); 78 statements[j].addBatch(); 79 } 80 } 81 82 for (int j = 0; j < 5; j++) { 83 statements[j].executeBatch(); 84 statements[j].close(); 85 } 86 } 87 88 }catch ( SQLException sqlex ){ 89 sqlex.printStackTrace(); 90 } 91 92 jdbcCar().commit(); 93 94 } 95 96 97 public void read() { 98 StringBuffer sql = select(); 99 sql.append(TABLES[0]); 100 sql.append(".id=?"); 101 query(sql.toString(), setup().getObjectCount()); 102 } 103 104 105 public void query() { 106 StringBuffer sql = select(); 107 sql.append(TABLES[2]); 108 sql.append(".b2=?"); 109 query(sql.toString(), setup().getSelectCount()); 110 } 111 112 115 public void delete(){ 116 117 int count = setup().getObjectCount(); 118 119 PreparedStatement[] statements = new PreparedStatement[5]; 120 for (int i = 0; i < 5; i++) { 121 statements[i] = jdbcCar().prepareStatement("delete from " + TABLES[i] + " where id=?"); 122 } 123 124 try { 125 if(count > 0){ 126 for (int i = 1; i<= count; i++) { 127 for (int j = 0; j < 5; j++) { 128 statements[j].setInt(1,i); 129 statements[j].execute(); 130 addToCheckSum(1); 131 } 132 } 133 134 for (int j = 0; j < 5; j++) { 135 statements[j].executeBatch(); 136 statements[j].close(); 137 } 138 } 139 } catch (SQLException e) { 140 e.printStackTrace(); 141 } 142 143 jdbcCar().commit(); 144 } 145 146 private StringBuffer select(){ 147 StringBuffer sql = new StringBuffer ("select * from "); 148 sql.append(TABLES[0]); 149 for (int i = 1; i < TABLES.length; i++) { 150 sql.append(", "); 151 sql.append(TABLES[i]); 152 } 153 sql.append(" where "); 154 for (int i = 1; i < TABLES.length; i++) { 155 sql.append(TABLES[i]); 156 sql.append(".parent = "); 157 sql.append(TABLES[i - 1]); 158 sql.append(".id"); 159 sql.append(" and "); 160 } 161 return sql; 162 } 163 164 private void query(String sql, int count){ 165 PreparedStatement statement = jdbcCar().prepareStatement(sql.toString()); 166 167 try { 168 for(int i = 1 ; i <= count; i++) { 169 statement.setInt(1,i); 170 ResultSet rs=statement.executeQuery(); 171 if(!rs.next()) { 172 System.err.println("Expected one result, received none: "+ i); 173 } 174 B4 b4 = new B4(rs.getInt(3), rs.getInt(6), rs.getInt(9), rs.getInt(12), rs.getInt(15)); 175 addToCheckSum(b4.checkSum()); 176 if(rs.next()) { 177 System.err.println("Expected one result, received multiple: "+i); 178 } 179 } 180 } catch (SQLException e) { 181 e.printStackTrace(); 182 } 183 184 } 185 186 187 188 189 190 191 192 } 193 | Popular Tags |