/Users/lyon/j4p/src/rdbms/DataBaseQuery.java

1    package rdbms; 
2     
3    import gui.html.HtmlViewer; 
4    import gui.html.HtmlSynthesizer; 
5     
6    import java.io.PrintStream; 
7    import java.sql.*; 
8    import java.util.Vector; 
9     
10   public final class DataBaseQuery { 
11    
12       private static DataBaseQuery dbq 
13               = new DataBaseQuery(); 
14       String dsn = "addresses"; 
15       String url = "jdbc:odbc:" + dsn; 
16       Connection connection = null; 
17       Statement statement = null; 
18       DatabaseMetaData dmd = null; 
19    
20       PrintStream out = System.out; 
21    
22    
23       String sqlQuery = 
24               "SELECT * FROM mailing;"; 
25       ResultSet rs = null; 
26       ResultSetMetaData rsmd = null; 
27       int colCount = 0; 
28    
29       private DataBaseQuery() { 
30           try { 
31               init(); 
32           } catch (Exception e) { 
33           } 
34       } 
35    
36       public void setOutputStream(PrintStream _out) { 
37           out = _out; 
38       } 
39    
40       public static DataBaseQuery getDataBaseQuery() { 
41           return dbq; 
42       } 
43    
44       public void init() throws Exception { 
45           // load driver 
46           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
47    
48           connection = DriverManager.getConnection(url); 
49           statement = connection.createStatement(); 
50           dmd = connection.getMetaData(); 
51    
52           rs = statement.executeQuery(sqlQuery); 
53           freshenMetaData(); 
54       } 
55    
56       public void freshenMetaData() throws Exception { 
57           System.out.println("fresenMetaData..."); 
58           rsmd = rs.getMetaData(); 
59           colCount = rsmd.getColumnCount(); 
60       } 
61    
62       public void printMetaData() throws Exception { 
63           out.println( 
64                   "Connected to:" + dmd.getURL()); 
65    
66       } 
67    
68    
69       private Vector getQuery() throws SQLException { 
70           System.out.println("getting query"); 
71           Vector v = new Vector(); 
72           while (rs.next()) { 
73               //freshenMetaData(); 
74               String s[] = new String[colCount]; 
75               for (int i = 1; i <= colCount; i++) { 
76                   s[i - 1] = rs.getString(i); 
77               } 
78               v.addElement(s); 
79           } 
80           return v; 
81       } 
82    
83       public String[][] getStringArray(Vector v) { 
84           String sa[][] = new String[v.size()][colCount]; 
85    
86           for (int x = 0; x < v.size(); x++) { 
87               String s[] = (String[]) (v.elementAt(x)); 
88               for (int y = 0; y < s.length; y++) 
89                   sa[x][y] = s[y]; 
90           } 
91           return sa; 
92       } 
93    
94       public void print(Vector v) { 
95           for (int i = 0; i < v.size(); i++) 
96               print((String[]) v.elementAt(i)); 
97       } 
98    
99       public void print(String a[]) { 
100          for (int i = 0; i < a.length; i++) 
101              out.print(a[i] + " "); 
102          out.println(); 
103      } 
104   
105      public void print(String a[][]) { 
106          if (a == null) { 
107              System.out.println("a is null, returning..."); 
108              return; 
109          } 
110          for (int x = 0; x < a.length; x++) { 
111              for (int y = 0; y < a[0].length; y++) 
112                  out.print(a[x][y] + " "); 
113              out.println(); 
114          } 
115      } 
116   
117      public String[][] get2DArray() { 
118          try { 
119              return getStringArray( 
120                      getQuery() 
121              ); 
122          } catch (Exception e) { 
123          } 
124          return null; 
125      } 
126   
127   
128      public void loadDriver() { 
129          try { 
130              //init(); 
131              printMetaData(); 
132              print( 
133                      getStringArray( 
134                              getQuery())); 
135          } catch (Exception e) { 
136              e.printStackTrace(); 
137          } 
138      } 
139   
140      public void print() { 
141          print(get2DArray()); 
142      } 
143   
144      public static void main(String[] args) { 
145          rdbms.DataBaseQuery dbq = 
146                  rdbms.DataBaseQuery.getDataBaseQuery(); 
147          System.out.println("this is a test from main"); 
148          String s[][]  = dbq.get2DArray(); 
149          HtmlViewer hv = new HtmlViewer(); 
150          HtmlSynthesizer hs = new HtmlSynthesizer(); 
151          hv.setString(hs.getHtml( 
152                  hs.getTable( 
153                          hs.getSheet(s)))); 
154          //dbq.setOutputStream(System.out); 
155          //dbq.print(); 
156      } 
157   
158  } 
159