KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > WwdUtils


1 /*
2      Derby - WwdUtils.java - utilitity methods used by WwdEmbedded.java
3
4         Licensed to the Apache Software Foundation (ASF) under one
5            or more contributor license agreements. See the NOTICE file
6            distributed with this work for additional information
7            regarding copyright ownership. The ASF licenses this file
8            to you under the Apache License, Version 2.0 (the
9            "License"); you may not use this file except in compliance
10            with 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,
15            software distributed under the License is distributed on an
16            "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17            KIND, either express or implied. See the License for the
18            specific language governing permissions and limitations
19            under the License.
20
21 */

22
23 import java.io.*;
24 import java.sql.*;
25 public class WwdUtils {
26
27 /*****************
28 ** Asks user to enter a wish list item or 'exit' to exit the loop - returns
29 ** the string entered - loop should exit when the string 'exit' is returned
30 ******************/

31    public static String JavaDoc getWishItem() {
32       BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
33       String JavaDoc ans = "";
34       try
35       {
36          while ( ans.length() == 0 ) {
37             System.out.println("Enter wish-list item (enter exit to end): ");
38             ans = br.readLine();
39             if ( ans.length() == 0 )
40                System.out.print("Nothing entered: ");
41          }
42       } catch (java.io.IOException JavaDoc e) {
43          System.out.println("Could not read response from stdin");
44          }
45          return ans;
46     } /** END getWishItem ***/
47
48 /*** Check for WISH_LIST table ****/
49    public static boolean wwdChk4Table (Connection conTst ) throws SQLException {
50       boolean chk = true;
51       boolean doCreate = false;
52       try {
53          Statement s = conTst.createStatement();
54          s.execute("update WISH_LIST set ENTRY_DATE = CURRENT_TIMESTAMP, WISH_ITEM = 'TEST ENTRY' where 1=3");
55       } catch (SQLException sqle) {
56          String JavaDoc theError = (sqle).getSQLState();
57          // System.out.println(" Utils GOT: " + theError);
58
/** If table exists will get - WARNING 02000: No row was found **/
59          if (theError.equals("42X05")) // Table does not exist
60
{ return false;
61           } else if (theError.equals("42X14") || theError.equals("42821")) {
62              System.out.println("WwdChk4Table: Incorrect table definition. Drop table WISH_LIST and rerun this program");
63              throw sqle;
64           } else {
65              System.out.println("WwdChk4Table: Unhandled SQLException" );
66              throw sqle;
67           }
68       }
69       // System.out.println("Just got the warning - table exists OK ");
70
return true;
71    } /*** END wwdInitTable **/
72
73
74    public static void main (String JavaDoc[] args) {
75    // This method allows stand-alone testing of the getWishItem method
76
String JavaDoc answer;
77       do {
78          answer = getWishItem();
79          if (! answer.equals("exit")) {
80             System.out.println ("You said: " + answer);
81          }
82       } while (! answer.equals("exit")) ;
83    }
84
85 }
Popular Tags