KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > demo > checkToursDB


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.demo.checkToursDB
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.demo;
23
24 import org.apache.derby.tools.ij;
25 import toursdb.insertMaps;
26 import java.sql.DriverManager JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32
33 public class checkToursDB {
34
35     public static void main(String JavaDoc args[]) {
36
37         String JavaDoc[] dbfiles = {"ToursDB_schema.sql","loadTables.sql"};
38         try {
39             System.setProperty("ij.database","jdbc:derby:toursDB;create=true");
40             for (int i = 0 ; i < dbfiles.length ; i++)
41             {
42                 String JavaDoc[] ijArgs = {dbfiles[i]};
43                 ij.main(ijArgs);
44             }
45         } catch (Exception JavaDoc e) {
46             e.printStackTrace();
47         }
48
49         PreparedStatement JavaDoc ps = null;
50         ResultSet JavaDoc rs = null;
51         Connection JavaDoc connCS = null;
52
53         // now populate the map table
54
try {
55             insertMaps.main(args);
56         } catch (SQLException JavaDoc se) {
57             se.printStackTrace();
58         } catch (Exception JavaDoc e) {
59             e.printStackTrace();
60         }
61
62         // first get connection...
63
try {
64             ij.getPropertyArg(args);
65             connCS = ij.startJBMS();
66         } catch (SQLException JavaDoc se) {
67             se.printStackTrace();
68         } catch (Exception JavaDoc e) {
69             e.printStackTrace();
70         }
71
72         // now ensure we can select from all the tables
73
doSelect(connCS);
74
75         // we've inserted, selected, now update a row in each table
76
try {
77             ps = connCS.prepareStatement
78                 ("select ECONOMY_SEATS from AIRLINES where AIRLINE = 'AA'");
79             rs = ps.executeQuery();
80             if (rs.next())
81                 System.out.print("ECONOMY_SEATS is first: " + rs.getInt(1));
82             Statement JavaDoc stmt = connCS.createStatement();
83             stmt.execute("update AIRLINES set ECONOMY_SEATS=108 where AIRLINE = 'AA'");
84             rs = ps.executeQuery();
85             if (rs.next())
86                 System.out.println(", ECONOMY_SEATS is then: " + rs.getString(1));
87         
88             ps = connCS.prepareStatement ("select COUNTRY from COUNTRIES where COUNTRY_ISO_CODE = 'US'" );
89             rs = ps.executeQuery();
90             if (rs.next())
91                 System.out.print("COUNTRY is first: " + rs.getString(1));
92             stmt = connCS.createStatement();
93             stmt.execute("update COUNTRIES set COUNTRY='United States of America' where COUNTRY_ISO_CODE = 'US'");
94             rs = ps.executeQuery();
95             if (rs.next())
96                 System.out.println(", COUNTRY is then: " + rs.getString(1));
97         
98             ps = connCS.prepareStatement ("select COUNTRY from CITIES where CITY_ID = 52" );
99             rs = ps.executeQuery();
100             if (rs.next())
101                 System.out.print("COUNTRY is first: " + rs.getString(1));
102             stmt = connCS.createStatement();
103             stmt.execute("update CITIES set COUNTRY='United States of America' where COUNTRY='United States'");
104             rs = ps.executeQuery();
105             if (rs.next())
106                 System.out.println(", COUNTRY is then: " + rs.getString(1));
107         
108             ps = connCS.prepareStatement ("select ECONOMY_SEATS_TAKEN from FLIGHTAVAILABILITY where FLIGHT_ID = 'AA1134' and FLIGHT_DATE='2004-03-31'" );
109             rs = ps.executeQuery();
110             if (rs.next())
111                 System.out.print("ECONOMY_SEATS_TAKEN is first: " + rs.getInt(1));
112             stmt = connCS.createStatement();
113             stmt.execute("update FLIGHTAVAILABILITY set ECONOMY_SEATS_TAKEN=20 where FLIGHT_ID = 'AA1134' and FLIGHT_DATE='2004-03-31'");
114             rs = ps.executeQuery();
115             if (rs.next())
116                 System.out.println(", ECONOMY_SEATS_TAKEN is then: " + rs.getString(1));
117         
118             ps = connCS.prepareStatement ("select AIRCRAFT from FLIGHTS where FLIGHT_ID = 'AA1183'" );
119             rs = ps.executeQuery();
120             if (rs.next())
121                 System.out.print("AIRCRAFT is first: " + rs.getString(1));
122             stmt = connCS.createStatement();
123             stmt.execute("update FLIGHTS set AIRCRAFT='B777' where FLIGHT_ID = 'AA1134'");
124             rs = ps.executeQuery();
125             if (rs.next())
126                 System.out.println(", AIRCRAFT is then: " + rs.getString(1));
127         
128             ps = connCS.prepareStatement ("select REGION from MAPS where MAP_NAME = 'BART'" );
129             rs = ps.executeQuery();
130             if (rs.next())
131                 System.out.print("REGION is first: " + rs.getString(1));
132             stmt = connCS.createStatement();
133             stmt.execute("update MAPS set REGION='San Francisco Bay Area' where MAP_NAME = 'BART'");
134             rs = ps.executeQuery();
135             if (rs.next())
136                 System.out.println(", REGION is then: " + rs.getString(1));
137     
138             // Flight_history is now has 1 row, because of TRIG1
139
stmt = connCS.createStatement();
140             ps = connCS.prepareStatement ("select STATUS from FLIGHTS_HISTORY where FLIGHT_ID = 'AA1134'" );
141             rs = ps.executeQuery();
142             if (rs.next())
143                 System.out.print("STATUS is first: " + rs.getString(1));
144             stmt = connCS.createStatement();
145             stmt.execute("update FLIGHTS_HISTORY set STATUS='over' where FLIGHT_ID='AA1134'");
146             rs = ps.executeQuery();
147             if (rs.next())
148                 System.out.println(", STATUS is then: " + rs.getString(1));
149
150         } catch (SQLException JavaDoc se) {
151             se.printStackTrace();
152         } catch (Exception JavaDoc e) {
153             e.printStackTrace();
154         }
155
156         // now delete....
157
try {
158             ps = null;
159
160             String JavaDoc tableName[] = {"AIRLINES","CITIES","COUNTRIES","FLIGHTAVAILABILITY","FLIGHTS","MAPS"};
161             for (int i = 0 ; i < 6; i++) {
162                 Statement JavaDoc stmt = connCS.createStatement();
163                 stmt.execute("delete from " + tableName[i]);
164                 System.out.println("deleted all from table " + tableName[i]);
165             }
166             // now quickly checking FLIGHTS_HISTORY -
167
// should now have a 2nd row because of trigger2
168
Statement JavaDoc stmt = connCS.createStatement();
169             rs = stmt.executeQuery("select STATUS from FLIGHTS_HISTORY where FLIGHT_ID IS NULL and STATUS <> 'over'");
170             // don't care if there are more than 1 rows...
171
if (rs.next())
172                 System.out.println("STATUS is here: " + rs.getString(1));
173             // now delete this one too
174
stmt.execute("delete from FLIGHTS_HISTORY");
175             System.out.println("deleted all from table FLIGHTS_HISTORY");
176         } catch (SQLException JavaDoc se) {
177             se.printStackTrace();
178         } catch (Exception JavaDoc e) {
179             e.printStackTrace();
180         }
181     
182         //select again
183
doSelect(connCS);
184
185     }
186
187     private static void doSelect(Connection JavaDoc connCS)
188     {
189         // now ensure we can select from all the tables
190
try {
191             PreparedStatement JavaDoc ps = null;
192
193             String JavaDoc tableName[] = {"AIRLINES","COUNTRIES","CITIES","FLIGHTAVAILABILITY","FLIGHTS","MAPS","FLIGHTS_HISTORY"};
194             for (int i = 0 ; i < 7; i++) {
195                 ps = connCS.prepareStatement ("select count(*) from " + tableName[i]);
196                 System.out.print("count for select * from table " + tableName[i]);
197                 ResultSet JavaDoc rs = ps.executeQuery();
198                 if (rs.next())
199                     System.out.println(": " + rs.getInt(1));
200                 else System.out.println(": 0");
201             }
202         } catch (SQLException JavaDoc se) {
203             se.printStackTrace();
204         } catch (Exception JavaDoc e) {
205             e.printStackTrace();
206         }
207     }
208
209 }
210
Popular Tags