1 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 31 public class JdbcCar extends Car{ 32 33 private static Connection mConnection; 34 private static Statement mStatement; 35 36 private final String mDBType; 37 private String mName; 38 39 private final static Map<Class ,String > mColTypesMap = new HashMap<Class ,String >(); 40 41 static{ 42 mColTypesMap.put( String .class, "VARCHAR(100)" ); 43 mColTypesMap.put( Integer.TYPE, "INTEGER" ); 44 } 45 46 public JdbcCar( String 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 e){ 56 e.printStackTrace(); 57 throw new CarMotorFailureException(); 58 } 59 } 60 61 public String 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 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 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 132 public void executeSQL( String sql ) 133 { 134 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 159 public ResultSet executeQuery( String 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 181 public ResultSet executeQueryForUpdate( String 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 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 214 public void dropTable( String 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 } 227 finally 228 { 229 if (stmt != null) 230 { 231 try { stmt.close(); } catch (SQLException sqlEx) { stmt = null; } 232 } 233 } 234 } 235 236 237 240 public void createTable( String tablename, String [] colnames, Class [] coltypes ) 241 { 242 String 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 tablename, String colname ) 256 { 257 String sql = "CREATE INDEX X" + tablename + "_" + colname + " ON " + tablename + " (" + colname + ")"; 259 executeSQL( sql ); 260 } 261 262 263 266 public PreparedStatement prepareStatement( String 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 |