blob: f6178fb69fa4438f3231b6dc8d4877991c3ba76f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-1998 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Sun Microsystems nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 */
34
35/**
36 * An adaptor, transforming the JDBC interface to the TableModel interface.
37 *
38 * @author Philip Milne
39 */
40
41import java.util.Vector;
42import java.sql.*;
43import javax.swing.table.AbstractTableModel;
44import javax.swing.event.TableModelEvent;
45
46public class JDBCAdapter extends AbstractTableModel {
47 Connection connection;
48 Statement statement;
49 ResultSet resultSet;
50 String[] columnNames = {};
51 Vector rows = new Vector();
52 ResultSetMetaData metaData;
53
54 public JDBCAdapter(String url, String driverName,
55 String user, String passwd) {
56 try {
57 Class.forName(driverName);
58 System.out.println("Opening db connection");
59
60 connection = DriverManager.getConnection(url, user, passwd);
61 statement = connection.createStatement();
62 }
63 catch (ClassNotFoundException ex) {
64 System.err.println("Cannot find the database driver classes.");
65 System.err.println(ex);
66 }
67 catch (SQLException ex) {
68 System.err.println("Cannot connect to this database.");
69 System.err.println(ex);
70 }
71 }
72
73 public void executeQuery(String query) {
74 if (connection == null || statement == null) {
75 System.err.println("There is no database to execute the query.");
76 return;
77 }
78 try {
79 resultSet = statement.executeQuery(query);
80 metaData = resultSet.getMetaData();
81
82 int numberOfColumns = metaData.getColumnCount();
83 columnNames = new String[numberOfColumns];
84 // Get the column names and cache them.
85 // Then we can close the connection.
86 for(int column = 0; column < numberOfColumns; column++) {
87 columnNames[column] = metaData.getColumnLabel(column+1);
88 }
89
90 // Get all rows.
91 rows = new Vector();
92 while (resultSet.next()) {
93 Vector newRow = new Vector();
94 for (int i = 1; i <= getColumnCount(); i++) {
95 newRow.addElement(resultSet.getObject(i));
96 }
97 rows.addElement(newRow);
98 }
99 // close(); Need to copy the metaData, bug in jdbc:odbc driver.
100 fireTableChanged(null); // Tell the listeners a new table has arrived.
101 }
102 catch (SQLException ex) {
103 System.err.println(ex);
104 }
105 }
106
107 public void close() throws SQLException {
108 System.out.println("Closing db connection");
109 resultSet.close();
110 statement.close();
111 connection.close();
112 }
113
114 protected void finalize() throws Throwable {
115 close();
116 super.finalize();
117 }
118
119 //////////////////////////////////////////////////////////////////////////
120 //
121 // Implementation of the TableModel Interface
122 //
123 //////////////////////////////////////////////////////////////////////////
124
125 // MetaData
126
127 public String getColumnName(int column) {
128 if (columnNames[column] != null) {
129 return columnNames[column];
130 } else {
131 return "";
132 }
133 }
134
135 public Class getColumnClass(int column) {
136 int type;
137 try {
138 type = metaData.getColumnType(column+1);
139 }
140 catch (SQLException e) {
141 return super.getColumnClass(column);
142 }
143
144 switch(type) {
145 case Types.CHAR:
146 case Types.VARCHAR:
147 case Types.LONGVARCHAR:
148 return String.class;
149
150 case Types.BIT:
151 return Boolean.class;
152
153 case Types.TINYINT:
154 case Types.SMALLINT:
155 case Types.INTEGER:
156 return Integer.class;
157
158 case Types.BIGINT:
159 return Long.class;
160
161 case Types.FLOAT:
162 case Types.DOUBLE:
163 return Double.class;
164
165 case Types.DATE:
166 return java.sql.Date.class;
167
168 default:
169 return Object.class;
170 }
171 }
172
173 public boolean isCellEditable(int row, int column) {
174 try {
175 return metaData.isWritable(column+1);
176 }
177 catch (SQLException e) {
178 return false;
179 }
180 }
181
182 public int getColumnCount() {
183 return columnNames.length;
184 }
185
186 // Data methods
187
188 public int getRowCount() {
189 return rows.size();
190 }
191
192 public Object getValueAt(int aRow, int aColumn) {
193 Vector row = (Vector)rows.elementAt(aRow);
194 return row.elementAt(aColumn);
195 }
196
197 public String dbRepresentation(int column, Object value) {
198 int type;
199
200 if (value == null) {
201 return "null";
202 }
203
204 try {
205 type = metaData.getColumnType(column+1);
206 }
207 catch (SQLException e) {
208 return value.toString();
209 }
210
211 switch(type) {
212 case Types.INTEGER:
213 case Types.DOUBLE:
214 case Types.FLOAT:
215 return value.toString();
216 case Types.BIT:
217 return ((Boolean)value).booleanValue() ? "1" : "0";
218 case Types.DATE:
219 return value.toString(); // This will need some conversion.
220 default:
221 return "\""+value.toString()+"\"";
222 }
223
224 }
225
226 public void setValueAt(Object value, int row, int column) {
227 try {
228 String tableName = metaData.getTableName(column+1);
229 // Some of the drivers seem buggy, tableName should not be null.
230 if (tableName == null) {
231 System.out.println("Table name returned null.");
232 }
233 String columnName = getColumnName(column);
234 String query =
235 "update "+tableName+
236 " set "+columnName+" = "+dbRepresentation(column, value)+
237 " where ";
238 // We don't have a model of the schema so we don't know the
239 // primary keys or which columns to lock on. To demonstrate
240 // that editing is possible, we'll just lock on everything.
241 for(int col = 0; col<getColumnCount(); col++) {
242 String colName = getColumnName(col);
243 if (colName.equals("")) {
244 continue;
245 }
246 if (col != 0) {
247 query = query + " and ";
248 }
249 query = query + colName +" = "+
250 dbRepresentation(col, getValueAt(row, col));
251 }
252 System.out.println(query);
253 System.out.println("Not sending update to database");
254 // statement.executeQuery(query);
255 }
256 catch (SQLException e) {
257 // e.printStackTrace();
258 System.err.println("Update failed");
259 }
260 Vector dataRow = (Vector)rows.elementAt(row);
261 dataRow.setElementAt(value, column);
262
263 }
264}