KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.sql.*;
23 import java.util.*;
24
25 import org.polepos.data.*;
26 import org.polepos.framework.*;
27
28
29 /**
30  * @author Herkules
31  */

32 public abstract class JdbcDriver extends org.polepos.framework.Driver {
33     
34     public void prepare() throws CarMotorFailureException{
35         ((JdbcCar)car()).openConnection();
36     }
37     
38     public void backToPit(){
39         ((JdbcCar)car()).closeConnection();
40     }
41     
42     public JdbcCar jdbcCar(){
43         return (JdbcCar)car();
44     }
45
46     /**
47      * Helper: perform any query
48      */

49     protected void performQuery(String JavaDoc sql) {
50         Log.logger.fine( "starting query" ); //NOI18N
51

52         try{
53             ResultSet rs = jdbcCar().executeQuery( sql );
54     
55             while( rs.next() ){
56                 Pilot p = new Pilot( rs.getString( 2 ), rs.getString( 3 ), rs.getInt( 4 ), rs.getInt( 5 ) );
57                 addToCheckSum(p.checkSum());
58             }
59         }
60         catch ( SQLException sqlex )
61         {
62             sqlex.printStackTrace();
63         }
64     }
65
66     protected <Value> void performSingleResultQuery(String JavaDoc sql,List<Value> values) {
67         Log.logger.fine( "starting query" ); //NOI18N
68
PreparedStatement stat=jdbcCar().prepareStatement(sql);
69         try {
70             for(Value val : values) {
71                 stat.setObject(1,val);
72                 ResultSet rs=stat.executeQuery();
73                 if(!rs.next()) {
74                     System.err.println("Expected one result, received none: "+val);
75                 }
76                 Pilot p = new Pilot( rs.getString( 2 ), rs.getString( 3 ), rs.getInt( 4 ), rs.getInt( 5 ) );
77                 addToCheckSum(p.checkSum());
78                 if(rs.next()) {
79                     System.err.println("Expected one result, received multiple: "+val);
80                 }
81             }
82         } catch (SQLException sqlexc) {
83             sqlexc.printStackTrace();
84         }
85         finally {
86             try {
87                 stat.close();
88             } catch (SQLException e) {
89                 e.printStackTrace();
90             }
91         }
92     }
93     
94     
95
96 }
97
Popular Tags