(i) Create a bean called JDBCQueryBean that is a modification of
JDBCBean. Instead of dealing only with a fixed query 'SELECT * FROM
Accounts', this bean should be capable of processing any query directed at the
Accounts/Stock table (depending upon which database you are using). The
code for the major method getQueryResults is supplied overleaf. In addition
to this method, the bean should provide read/write access to a property called
query that holds the current query (and has a default value of 'SELECT *
FROM Accounts'). Read access should also be provided to properties
numFields (holding the number of fields in the query) and numRows (the
number of rows in the query results).
(ii) Create a simple HTML page that uses a text field in a form to accept the
user's query and pass it on to a JSP called JDBCQuery.jsp.
(iii) Possibly using JDBC.jsp as a starting point, produce a JSP that accepts
the query from the above HTML page and then uses the bean to display the
results of the query in a table.
public static Vector getQueryResults()
throws SQLException
{
results = statement.executeQuery(getQuery());
metaData = results.getMetaData();
numFields = metaData.getColumnCount();
queryResults = new Vector();
fieldNames = new Vector();
dataTypes = new Vector();
for (int i=1; i<=numfields;>
fieldNames.add(metaData.getColumnName(i));
while (results.next())
{
for (int i=1; i<=numfields;>
{
int colType = metaData.getColumnType(i);
switch (colType)
{
case Types.INTEGER:
queryResults.add(results.getInt(i));
dataTypes.add(“integer”);
break;
case Types.VARCHAR:
queryResults.add(
results.getString(i));
dataTypes.add(“string”);
break;
case Types.NUMERIC:
queryResults.add(
results.getFloat(i));
dataTypes.add(“float”);
break;
default: //Hopefully, will never happen!
queryResults.add(
results.getString(i));
dataTypes.add(“string”);
}
}
}
return queryResults;