01: package jimm.datavision.source.sql;
02:
03: import jimm.datavision.ErrorHandler;
04: import jimm.datavision.source.DataCursor;
05: import java.util.List;
06: import java.util.ArrayList;
07: import java.sql.*;
08:
09: /**
10: * A concrete subclass of <code>DataCursor</code> that wraps a JDBC result set.
11: *
12: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
13: */
14: public class ResultSetRow extends DataCursor {
15:
16: protected PreparedStatement stmt;
17: protected ResultSet rset;
18: protected int numSelectables;
19: protected boolean noMoreData;
20:
21: ResultSetRow(Connection conn, SQLQuery query) throws SQLException {
22: // Suggested by Konstantin. Though it works for his Oracle driver,
23: // it doesn't work for my PostgreSQL driver. These args are also
24: // legal for prepared statements.
25: // stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
26: // ResultSet.CONCUR_READ_ONLY);
27:
28: String preparedStmtString = query.toPreparedStatementString();
29: if (preparedStmtString != null
30: && preparedStmtString.length() > 0) {
31: stmt = conn.prepareStatement(preparedStmtString);
32: query.setParameters(stmt);
33: rset = stmt.executeQuery();
34: numSelectables = query.getNumSelectables();
35: noMoreData = false;
36: } else {
37: numSelectables = 0;
38: noMoreData = true;
39: }
40: }
41:
42: public List readRowData() {
43: // Avoid calling rset.next() if it has already returned false. Doing so
44: // appears harmless in most cases but seems to be causing a problem
45: // with at least one user's DB2 JDBC driver.
46: if (noMoreData)
47: return null;
48:
49: try {
50: if (!rset.next()) {
51: noMoreData = true;
52: return null;
53: }
54: } catch (SQLException sqle) {
55: ErrorHandler.error(sqle);
56: return null;
57: }
58:
59: ArrayList list = new ArrayList();
60: try {
61: for (int i = 1; i <= numSelectables; ++i)
62: list.add(rset.getObject(i));
63: } catch (SQLException sqle) {
64: ErrorHandler.error(sqle);
65: }
66: return list;
67: }
68:
69: public void close() {
70: try {
71: if (rset != null)
72: rset.close();
73: if (stmt != null)
74: stmt.close();
75: } catch (SQLException sqle) {
76: ErrorHandler.error(sqle);
77: } finally {
78: rset = null;
79: stmt = null;
80: }
81: }
82:
83: }
|