// DataQueryBean.java // JavaBean that will generate an SQL query for the collection of data from the // specified schema. The JDBC connection parameters and SQL strings are loaded // from property files. package com.bullseyecomputing.beans; // import the essentials import java.io.*; import java.sql.*; import java.util.*; public class DataQueryBean { private ClassLoader loader; private Connection dbConnection; private Statement dbTransaction; private String jdbcProperties = "jdbc.properties"; private String schema; private String sqlProperties; private String sqlListSelect, sqlDetailSelect; private String filter, orderBy, limit; // Constructor public DataQueryBean() throws Exception { Properties props = new Properties(); // Grab our ClassLoader loader = this.getClass().getClassLoader(); // Use the ClassLoader to locate and load the property file. try { props.load(loader.getResourceAsStream(jdbcProperties)); } // Handle the exception if the file is unavailable catch(IOException ioException) { ioException.printStackTrace(); } // Get the JDBC parameters String jdbcDriver = props.getProperty("jdbcDriver"); String jdbcURL = props.getProperty("jdbcURL"); // Get the User parameters String jdbcUser = props.getProperty("jdbcUser"); String jdbcPassword = props.getProperty("jdbcPassword"); // Create a JDBC connection Class.forName(jdbcDriver); dbConnection = DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPassword); } // Constructor // Schema public void setSchema(String schema) { Properties props = new Properties(); this.schema = schema; sqlProperties = schema + "Query.properties"; // Load the SQL Properties try { props.load(loader.getResourceAsStream(sqlProperties)); } catch(IOException ioException) { ioException.printStackTrace(); } // Get the SQL strings sqlListSelect = props.getProperty("sqlListSelect"); sqlDetailSelect = props.getProperty("sqlDetailSelect"); } // Schema // Filter public void setFilter(String filterString) { filter = filterString; sqlListSelect += " where " + filter; } public String getFilter() { return filter; } // Filter // OrderBy public void setOrderBy(String orderByString) { orderBy = orderByString; sqlListSelect += " order by " + orderBy; } public String getOrderBy() { return orderBy; } // OrderBy // Limit public void setLimit(String limit) { this.limit = limit; sqlListSelect += " limit " + limit; } public String getLimit() { return limit; } // Limit // getDataList public ArrayList getDataList() throws SQLException { ArrayList dataList = new ArrayList(); dbTransaction = dbConnection.createStatement(); ResultSet results = dbTransaction.executeQuery(sqlListSelect); while(results.next()) { ListDataBean data = new ListDataBean(); // Set the Bean values data.setId(Integer.toString(results.getInt(1))); data.setName(results.getString(2)); data.setStreet(results.getString(3)); data.setCity(results.getString(4)); data.setState(results.getString(5)); data.setZip(results.getString(6)); // handle empty values String plus4 = results.getString(7).trim(); data.setPlus4(plus4.equals("") ? " " : plus4); dataList.add(data); } dbTransaction.close(); return dataList; } // getDataList // Finalize protected void finalize() { try { dbConnection.close(); } catch(SQLException sqlException) { sqlException.printStackTrace(); } } // Finalize } // class