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

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