/Users/lyon/j4p/src/addBk/address/CachedDatabase.java

1    package addBk.address; 
2     
3    import java.sql.SQLException; 
4    import java.util.Vector; 
5     
6    final class CachedDatabase { 
7        private static CachedDatabase cdb = new CachedDatabase(); 
8     
9        public static CachedDatabase getCachedDatabase() { 
10           return cdb; 
11       } 
12    
13       private CachedDatabase() { 
14       } //singleton pattern 
15    
16       private Vector 
17               v = new Vector(); 
18    
19       private Vector 
20               addressVector = new Vector(); 
21    
22       SimpleSelect ss 
23               = new SimpleSelect(); 
24    
25       Vector rows = null; 
26    
27       public void init() { 
28           try { 
29               ss.safeConnect(); 
30               // The following is the table name! 
31               ss.getQuery("Addresses"); 
32               rows = ss.getRows(); 
33               ss.close(); 
34               initAddresses(); 
35           } catch (SQLException e) { 
36               e.printStackTrace(); 
37           } catch (ClassNotFoundException e) { 
38               e.printStackTrace(); 
39           } finally { 
40               System.out.println("could not connect to sql database, RDBMS setup correctly?" 
41                       + "program continues..."); 
42           } 
43    
44       } 
45    
46       public void initAddresses() { 
47           for (int i = 0; i < rows.size(); i++) { 
48               addAddress( 
49                       new AddressRecord((String[]) 
50                       rows.elementAt(i))); 
51           } 
52       } 
53    
54       public void addAddress(AddressRecord a) { 
55           addressVector.addElement(a); 
56       } 
57    
58       public void print() { 
59           for (int i = 0; i < 
60                   getSize(); i++) { 
61               (get(i)).print(); 
62           } 
63       } 
64    
65       public void print(String s[]) { 
66           for (int i = 0; i < s.length; i++) { 
67               System.out.print(s[i] + ","); 
68    
69           } 
70    
71       } 
72    
73       public static void main(String args[]) { 
74           CachedDatabase 
75                   cd = new CachedDatabase(); 
76           cd.init(); 
77           cd.print(); 
78    
79       } 
80    
81       public void sort() { 
82           Sort.bubble(addressVector); 
83       } 
84    
85       public void add(AddressRecord a) { 
86           v.addElement(a); 
87       } 
88    
89       public AddressRecord get(int i) { 
90           try { 
91               return 
92                       (AddressRecord) 
93                       addressVector.elementAt(i); 
94           } catch (ArrayIndexOutOfBoundsException e) { 
95               return new AddressRecord(); 
96           } 
97       } 
98    
99       public int getSize() { 
100          return addressVector.size(); 
101      } 
102  }