/* * DBSnapshotGridModel.java * * Copyright 2000 Eliad Technologies, Inc. All Rights Reserved. * * This software is the proprietary information of Eliad Technologies, Inc. */ package example02; import java.util.*; import java.sql.*; import com.eliad.model.*; /** * This class fills a GridModel with data from a database * @author Daniel Rosenblatt * @version 1.0 */ public class DBSnapshotGridModel extends GenericGridModel { private Connection connection_; private Statement statement_; private String query_; public DBSnapshotGridModel(String url,String driverName) throws ClassNotFoundException, SQLException { this(url,driverName,"",""); } public DBSnapshotGridModel(String url,String driverName,String user,String passwd) throws ClassNotFoundException, SQLException { Class.forName(driverName); connection_ = DriverManager.getConnection(url, user, passwd); statement_ = connection_.createStatement(); } public void setQuery(String query) { if (connection_ == null || statement_ == null) throw new IllegalStateException("There is no database or statement to execute the query."); try { query_ = query; ResultSet resultSet = statement_.executeQuery(query); ResultSetMetaData metaData = resultSet.getMetaData(); Vector rows = new Vector(); int colCount = metaData.getColumnCount(); // Get all rows. while (resultSet.next()) { Vector newRow = new Vector(colCount); for (int i = 1; i <= colCount; i++) newRow.add(resultSet.getObject(i)); rows.add(newRow); } resultSet.close(); setDataVector(rows); } catch (SQLException ex) { System.err.println(ex); } } public void close() throws SQLException { statement_.close(); connection_.close(); } protected void finalize() throws Throwable { try { close(); } finally { super.finalize(); } } }