/* * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* * This source code is provided to illustrate the usage of a given feature * or technique and has been deliberately simplified. Additional steps * required for a production-quality application, such as security checks, * input validation and proper error handling, might not be present in * this sample code.
*/
/** * An adaptor, transforming the JDBC interface to the TableModel interface. * * @author Philip Milne
*/
@SuppressWarnings("serial") publicclass JDBCAdapter extends AbstractTableModel {
publicvoid executeQuery(String query) { if (connection == null || statement == null) {
System.err.println("There is no database to execute the query."); return;
} try {
resultSet = statement.executeQuery(query);
metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
columnNames = new String[numberOfColumns]; // Get the column names and cache them. // Then we can close the connection. for (int column = 0; column < numberOfColumns; column++) {
columnNames[column] = metaData.getColumnLabel(column + 1);
}
// Get all rows.
rows = new ArrayList<List<Object>>(); while (resultSet.next()) {
List<Object> newRow = new ArrayList<Object>(); for (int i = 1; i <= getColumnCount(); i++) {
newRow.add(resultSet.getObject(i));
}
rows.add(newRow);
} // close(); Need to copy the metaData, bug in jdbc:odbc driver.
// Tell the listeners a new table has arrived.
fireTableChanged(null);
} catch (SQLException ex) {
System.err.println(ex);
}
}
switch (type) { case Types.INTEGER: case Types.DOUBLE: case Types.FLOAT: return value.toString(); case Types.BIT: return ((Boolean) value).booleanValue() ? "1" : "0"; case Types.DATE: return value.toString(); // This will need some conversion. default: return"\"" + value.toString() + "\"";
}
}
@Override publicvoid setValueAt(Object value, int row, int column) { try {
String tableName = metaData.getTableName(column + 1); // Some of the drivers seem buggy, tableName should not be null. if (tableName == null) {
System.out.println("Table name returned null.");
}
String columnName = getColumnName(column);
String query = "update " + tableName + " set " + columnName + " = "
+ dbRepresentation(column, value) + " where "; // We don't have a model of the schema so we don't know the // primary keys or which columns to lock on. To demonstrate // that editing is possible, we'll just lock on everything. for (int col = 0; col < getColumnCount(); col++) {
String colName = getColumnName(col); if (colName.equals("")) { continue;
} if (col != 0) {
query = query + " and ";
}
query = query + colName + " = " + dbRepresentation(col,
getValueAt(row, col));
}
System.out.println(query);
System.out.println("Not sending update to database"); // statement.executeQuery(query);
} catch (SQLException e) { // e.printStackTrace();
System.err.println("Update failed");
}
List<Object> dataRow = rows.get(row);
dataRow.set(column, value);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.