JSP Addressbook app for VIP Express (circa Jun 2002)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

152 lines
3.6 KiB

  1. // DataQueryBean.java
  2. // JavaBean that will generate an SQL query for the collection of data from the
  3. // specified schema. The JDBC connection parameters and SQL strings are loaded
  4. // from property files.
  5. package com.bullseyecomputing.beans;
  6. // import the essentials
  7. import java.io.*;
  8. import java.sql.*;
  9. import java.util.*;
  10. public class DataQueryBean {
  11. private ClassLoader loader;
  12. private Connection dbConnection;
  13. private Statement dbTransaction;
  14. private String jdbcProperties = "jdbc.properties";
  15. private String schema;
  16. private String sqlProperties;
  17. private String sqlListSelect, sqlDetailSelect;
  18. private String filter, orderBy, limit;
  19. // Constructor
  20. public DataQueryBean() throws Exception {
  21. Properties props = new Properties();
  22. // Grab our ClassLoader
  23. loader = this.getClass().getClassLoader();
  24. // Use the ClassLoader to locate and load the property file.
  25. try {
  26. props.load(loader.getResourceAsStream(jdbcProperties));
  27. }
  28. // Handle the exception if the file is unavailable
  29. catch(IOException ioException) {
  30. ioException.printStackTrace();
  31. }
  32. // Get the JDBC parameters
  33. String jdbcDriver = props.getProperty("jdbcDriver");
  34. String jdbcURL = props.getProperty("jdbcURL");
  35. // Get the User parameters
  36. String jdbcUser = props.getProperty("jdbcUser");
  37. String jdbcPassword = props.getProperty("jdbcPassword");
  38. // Create a JDBC connection
  39. Class.forName(jdbcDriver);
  40. dbConnection = DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPassword);
  41. }
  42. // Constructor
  43. // Schema
  44. public void setSchema(String schema) {
  45. Properties props = new Properties();
  46. this.schema = schema;
  47. sqlProperties = schema + "Query.properties";
  48. // Load the SQL Properties
  49. try {
  50. props.load(loader.getResourceAsStream(sqlProperties));
  51. }
  52. catch(IOException ioException) {
  53. ioException.printStackTrace();
  54. }
  55. // Get the SQL strings
  56. sqlListSelect = props.getProperty("sqlListSelect");
  57. sqlDetailSelect = props.getProperty("sqlDetailSelect");
  58. }
  59. // Schema
  60. // Filter
  61. public void setFilter(String filterString) {
  62. filter = filterString;
  63. sqlListSelect += " where " + filter;
  64. }
  65. public String getFilter() {
  66. return filter;
  67. }
  68. // Filter
  69. // OrderBy
  70. public void setOrderBy(String orderByString) {
  71. orderBy = orderByString;
  72. sqlListSelect += " order by " + orderBy;
  73. }
  74. public String getOrderBy() {
  75. return orderBy;
  76. }
  77. // OrderBy
  78. // Limit
  79. public void setLimit(String limit) {
  80. this.limit = limit;
  81. sqlListSelect += " limit " + limit;
  82. }
  83. public String getLimit() {
  84. return limit;
  85. }
  86. // Limit
  87. // getDataList
  88. public ArrayList getDataList() throws SQLException {
  89. ArrayList dataList = new ArrayList();
  90. dbTransaction = dbConnection.createStatement();
  91. ResultSet results = dbTransaction.executeQuery(sqlListSelect);
  92. while(results.next()) {
  93. ListDataBean data = new ListDataBean();
  94. // Set the Bean values
  95. data.setId(Integer.toString(results.getInt(1)));
  96. data.setName(results.getString(2));
  97. data.setStreet(results.getString(3));
  98. data.setCity(results.getString(4));
  99. data.setState(results.getString(5));
  100. data.setZip(results.getString(6));
  101. // handle empty values
  102. String plus4 = results.getString(7).trim();
  103. data.setPlus4(plus4.equals("") ? "&nbsp" : plus4);
  104. dataList.add(data);
  105. }
  106. dbTransaction.close();
  107. return dataList;
  108. }
  109. // getDataList
  110. // Finalize
  111. protected void finalize() {
  112. try {
  113. dbConnection.close();
  114. }
  115. catch(SQLException sqlException) {
  116. sqlException.printStackTrace();
  117. }
  118. }
  119. // Finalize
  120. }
  121. // class