/Users/lyon/j4p/src/net/server/servlets/SqlBean.java

1    package net.server.servlets; 
2     
3    import java.sql.*; 
4     
5    public class SqlBean { 
6        private String url = "jdbc:odbc:udb"; 
7        private String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
8        private String userId = ""; 
9        private String password = ""; 
10       private Connection c; 
11       private DatabaseMetaData dbmd; 
12       private boolean isReadOnly = false; 
13       private boolean usesLocalFiles = false; 
14       private String driverName = null; 
15       private String catalogName = null; 
16       private String productName = null; 
17    
18       public boolean getUsesLocalFiles() { 
19           return usesLocalFiles; 
20       } 
21    
22       public boolean getReadOnly() { 
23           return isReadOnly; 
24       } 
25    
26       public String getCatalogName() { 
27           return catalogName; 
28       } 
29    
30       public String getDriverName() { 
31           return driverName; 
32       } 
33    
34       public String getProductName() { 
35           return productName; 
36       } 
37    
38       public void setUserId(String _userId) { 
39           userId = _userId; 
40       } 
41    
42       public void setPassword(String _password) { 
43           password = _password; 
44       } 
45    
46       public void setUrl(String _url) { 
47           url = _url; 
48       } 
49    
50       public void setDriver(String _driver) { 
51           driver = _driver; 
52       } 
53    
54       public SqlBean() { 
55           init(); 
56       } 
57    
58       public void init() { 
59           try { 
60               Class.forName(driver); 
61               c = DriverManager.getConnection(url, userId, password); 
62               dbmd = c.getMetaData(); 
63               catalogName = c.getCatalog(); 
64               isReadOnly = c.isReadOnly(); 
65               usesLocalFiles = dbmd.usesLocalFiles(); 
66               driverName = dbmd.getDriverName(); 
67               productName = dbmd.getDatabaseProductName(); 
68           } catch (ClassNotFoundException e) { 
69               e.printStackTrace(); 
70           } catch (SQLException e) { 
71               e.printStackTrace(); 
72           } 
73    
74           System.out.println("Opened Connection:" + url); 
75       } 
76    
77       public void printInfo() { 
78           println("productName=" + productName); 
79           println("catalogName=" + catalogName); 
80           println("is ReadOnly=" + getReadOnly()); 
81           println("usesLocalFiles=" + getUsesLocalFiles()); 
82           println("driverName=" + driverName); 
83       } 
84    
85       public void close() { 
86           try { 
87               c.close(); 
88               System.out.println("closed connection"); 
89           } catch (SQLException e) { 
90               e.printStackTrace(); 
91           } 
92       } 
93    
94       public ResultSet query(String sql) { 
95           try { 
96               Statement s = c.createStatement(); 
97               ResultSet rs = s.executeQuery(sql); 
98               return rs; 
99           } catch (SQLException e) { 
100              e.printStackTrace(); 
101          } 
102   
103          return null; 
104      } 
105   
106      public ResultSetMetaData getResultSetMetaData(ResultSet rs) { 
107          try { 
108              return rs.getMetaData(); 
109          } catch (SQLException e) { 
110              e.printStackTrace(); 
111          } 
112   
113          return null; 
114      } 
115   
116      public int getColumnCount(ResultSetMetaData rsmd) { 
117          try { 
118              return rsmd.getColumnCount(); 
119          } catch (SQLException e) { 
120              e.printStackTrace(); 
121          } 
122   
123          return 0; 
124      } 
125   
126      public String[] getcolumnTypeNames(ResultSetMetaData rsmd) { 
127          int count = getColumnCount(rsmd); 
128          String sa [] = new String[count]; 
129   
130          try { 
131              for (int i = 0; i < sa.length; i++) { 
132                  sa[i] = rsmd.getColumnTypeName(i + 1); 
133              } 
134          } catch (SQLException e) { 
135              e.printStackTrace(); 
136          } 
137   
138          return sa; 
139      } 
140   
141      public String[] getRowAsString(ResultSet rs) { 
142          int N = getColumnCount( 
143                  getResultSetMetaData(rs)); 
144          String s[] = new String[N]; 
145   
146          try { 
147              for (int i = 0; i < N; i++) 
148                  s[i] = rs.getString(i + 1); 
149          } catch (SQLException e) { 
150              e.printStackTrace(); 
151          } 
152          return s; 
153      } 
154   
155      public int getResultSetIntegerColumnData(ResultSet rs, 
156                                               int columnIndex) { 
157          int columnValue = 0; 
158   
159          try { 
160              if (rs.next()) 
161                  columnValue = rs.getInt(columnIndex); 
162          } catch (SQLException e) { 
163              e.printStackTrace(); 
164          } 
165   
166          return columnValue; 
167      } 
168   
169      public void print(ResultSet rs) { 
170          int i; 
171          String cn[] = getColumnNames( 
172                  getResultSetMetaData(rs)); 
173          println(cn); 
174          boolean more = false; 
175          while (more = nextRow(rs)) 
176              println(getRowAsString(rs)); 
177      } 
178   
179      public void println(Object o) { 
180          System.out.println(o); 
181      } 
182   
183      public void println(String s[]) { 
184          for (int i = 0; i < s.length; i++) 
185              System.out.print(s[i] + '\t'); 
186          System.out.println(); 
187      } 
188   
189      public boolean nextRow(ResultSet rs) { 
190          try { 
191              return rs.next(); 
192          } catch (SQLException e) { 
193              return false; 
194          } 
195      } 
196   
197      public String[] getColumnNames(ResultSetMetaData rsmd) { 
198          String s [] = new String[getColumnCount(rsmd)]; 
199          try { 
200              for (int i = 1; i <= s.length; i++) 
201                  s[i - 1] = rsmd.getColumnLabel(i); 
202          } catch (SQLException e) { 
203              e.printStackTrace(); 
204          } 
205   
206          return s; 
207      } 
208   
209      public DatabaseMetaData getDatabaseMetaData() { 
210          return dbmd; 
211      } 
212   
213      public void insert(String sql) { 
214          try { 
215              Statement s = c.createStatement(); 
216              int insertResult = s.executeUpdate(sql); 
217          } catch (SQLException e) { 
218              e.printStackTrace(); 
219          } 
220      } 
221   
222      public static void main(String args[]) { 
223          SqlBean sb = new SqlBean(); 
224          sb.init(); 
225          sb.printInfo(); 
226          sb.insert("INSERT INTO mailinglist (firstname,lastname) values ('doug','lyon');"); 
227          sb.print(sb.query("SELECT FirstName, LastName FROM mailingList")); 
228          sb.close(); 
229      } 
230   
231  }