0N/A/*
3853N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
3853N/A
3853N/Aimport java.sql.Connection;
3853N/Aimport java.sql.DriverManager;
3853N/Aimport java.sql.ResultSet;
3853N/Aimport java.sql.ResultSetMetaData;
3853N/Aimport java.sql.SQLException;
3853N/Aimport java.sql.Statement;
3853N/Aimport java.sql.Types;
3853N/Aimport java.util.ArrayList;
3853N/Aimport java.util.List;
3853N/Aimport javax.swing.table.AbstractTableModel;
3853N/A
0N/A
0N/A/**
0N/A * An adaptor, transforming the JDBC interface to the TableModel interface.
0N/A *
0N/A * @author Philip Milne
0N/A */
3853N/A@SuppressWarnings("serial")
0N/Apublic class JDBCAdapter extends AbstractTableModel {
3853N/A
3853N/A Connection connection;
3853N/A Statement statement;
3853N/A ResultSet resultSet;
3853N/A String[] columnNames = {};
3853N/A List<List<Object>> rows = new ArrayList<List<Object>>();
3853N/A ResultSetMetaData metaData;
0N/A
0N/A public JDBCAdapter(String url, String driverName,
3853N/A String user, String passwd) {
0N/A try {
0N/A Class.forName(driverName);
0N/A System.out.println("Opening db connection");
0N/A
0N/A connection = DriverManager.getConnection(url, user, passwd);
0N/A statement = connection.createStatement();
3853N/A } catch (ClassNotFoundException ex) {
0N/A System.err.println("Cannot find the database driver classes.");
0N/A System.err.println(ex);
3853N/A } catch (SQLException ex) {
0N/A System.err.println("Cannot connect to this database.");
0N/A System.err.println(ex);
0N/A }
3853N/A }
0N/A
0N/A public void executeQuery(String query) {
0N/A if (connection == null || statement == null) {
0N/A System.err.println("There is no database to execute the query.");
0N/A return;
0N/A }
0N/A try {
0N/A resultSet = statement.executeQuery(query);
0N/A metaData = resultSet.getMetaData();
0N/A
3853N/A int numberOfColumns = metaData.getColumnCount();
0N/A columnNames = new String[numberOfColumns];
0N/A // Get the column names and cache them.
0N/A // Then we can close the connection.
3853N/A for (int column = 0; column < numberOfColumns; column++) {
3853N/A columnNames[column] = metaData.getColumnLabel(column + 1);
0N/A }
0N/A
0N/A // Get all rows.
3853N/A rows = new ArrayList<List<Object>>();
0N/A while (resultSet.next()) {
3853N/A List<Object> newRow = new ArrayList<Object>();
0N/A for (int i = 1; i <= getColumnCount(); i++) {
3853N/A newRow.add(resultSet.getObject(i));
0N/A }
3853N/A rows.add(newRow);
0N/A }
0N/A // close(); Need to copy the metaData, bug in jdbc:odbc driver.
3853N/A
3853N/A // Tell the listeners a new table has arrived.
3853N/A fireTableChanged(null);
3853N/A } catch (SQLException ex) {
0N/A System.err.println(ex);
0N/A }
0N/A }
0N/A
0N/A public void close() throws SQLException {
0N/A System.out.println("Closing db connection");
0N/A resultSet.close();
0N/A statement.close();
0N/A connection.close();
0N/A }
0N/A
3853N/A @Override
0N/A protected void finalize() throws Throwable {
0N/A close();
0N/A super.finalize();
0N/A }
0N/A
0N/A //////////////////////////////////////////////////////////////////////////
0N/A //
0N/A // Implementation of the TableModel Interface
0N/A //
0N/A //////////////////////////////////////////////////////////////////////////
0N/A // MetaData
3853N/A @Override
0N/A public String getColumnName(int column) {
0N/A if (columnNames[column] != null) {
0N/A return columnNames[column];
0N/A } else {
0N/A return "";
0N/A }
0N/A }
0N/A
3853N/A @Override
3853N/A public Class<?> getColumnClass(int column) {
0N/A int type;
0N/A try {
3853N/A type = metaData.getColumnType(column + 1);
3853N/A } catch (SQLException e) {
0N/A return super.getColumnClass(column);
0N/A }
0N/A
3853N/A switch (type) {
3853N/A case Types.CHAR:
3853N/A case Types.VARCHAR:
3853N/A case Types.LONGVARCHAR:
3853N/A return String.class;
0N/A
3853N/A case Types.BIT:
3853N/A return Boolean.class;
0N/A
3853N/A case Types.TINYINT:
3853N/A case Types.SMALLINT:
3853N/A case Types.INTEGER:
3853N/A return Integer.class;
0N/A
3853N/A case Types.BIGINT:
3853N/A return Long.class;
0N/A
3853N/A case Types.FLOAT:
3853N/A case Types.DOUBLE:
3853N/A return Double.class;
0N/A
3853N/A case Types.DATE:
3853N/A return java.sql.Date.class;
0N/A
3853N/A default:
3853N/A return Object.class;
0N/A }
0N/A }
0N/A
3853N/A @Override
0N/A public boolean isCellEditable(int row, int column) {
0N/A try {
3853N/A return metaData.isWritable(column + 1);
3853N/A } catch (SQLException e) {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A public int getColumnCount() {
0N/A return columnNames.length;
0N/A }
0N/A
0N/A // Data methods
0N/A public int getRowCount() {
0N/A return rows.size();
0N/A }
0N/A
0N/A public Object getValueAt(int aRow, int aColumn) {
3853N/A List<Object> row = rows.get(aRow);
3853N/A return row.get(aColumn);
0N/A }
0N/A
0N/A public String dbRepresentation(int column, Object value) {
0N/A int type;
0N/A
0N/A if (value == null) {
0N/A return "null";
0N/A }
0N/A
0N/A try {
3853N/A type = metaData.getColumnType(column + 1);
3853N/A } catch (SQLException e) {
0N/A return value.toString();
0N/A }
0N/A
3853N/A switch (type) {
3853N/A case Types.INTEGER:
3853N/A case Types.DOUBLE:
3853N/A case Types.FLOAT:
3853N/A return value.toString();
3853N/A case Types.BIT:
3853N/A return ((Boolean) value).booleanValue() ? "1" : "0";
3853N/A case Types.DATE:
3853N/A return value.toString(); // This will need some conversion.
3853N/A default:
3853N/A return "\"" + value.toString() + "\"";
0N/A }
0N/A
0N/A }
0N/A
3853N/A @Override
0N/A public void setValueAt(Object value, int row, int column) {
0N/A try {
3853N/A String tableName = metaData.getTableName(column + 1);
0N/A // Some of the drivers seem buggy, tableName should not be null.
0N/A if (tableName == null) {
0N/A System.out.println("Table name returned null.");
0N/A }
0N/A String columnName = getColumnName(column);
0N/A String query =
3853N/A "update " + tableName + " set " + columnName + " = "
3853N/A + dbRepresentation(column, value) + " where ";
0N/A // We don't have a model of the schema so we don't know the
0N/A // primary keys or which columns to lock on. To demonstrate
0N/A // that editing is possible, we'll just lock on everything.
3853N/A for (int col = 0; col < getColumnCount(); col++) {
0N/A String colName = getColumnName(col);
0N/A if (colName.equals("")) {
0N/A continue;
0N/A }
0N/A if (col != 0) {
0N/A query = query + " and ";
0N/A }
3853N/A query = query + colName + " = " + dbRepresentation(col,
3853N/A getValueAt(row, col));
0N/A }
0N/A System.out.println(query);
0N/A System.out.println("Not sending update to database");
0N/A // statement.executeQuery(query);
3853N/A } catch (SQLException e) {
0N/A // e.printStackTrace();
0N/A System.err.println("Update failed");
0N/A }
3853N/A List<Object> dataRow = rows.get(row);
3853N/A dataRow.set(column, value);
0N/A
0N/A }
0N/A}