1 package com.workingdogs.village;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.sql.Connection;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25
26 /***
27 * This class is used for doing SQL select statements on the database. It should not be used for doing modifications via
28 * update/delete/insert statements. If you would like to perform those functions, please use a <a
29 * href="TableDataSet.html">TableDataSet</a>.
30 *
31 * <P>
32 * Here is some example code for using a QueryDataSet.
33 * <PRE>
34 * QueryDataSet qds = new QueryDataSet ( connection, "SELECT * from my_table" );
35 * qds.fetchRecords(10); // fetch the first 10 records
36 * for ( int i = 0; i < qds.size(); i++ )
37 * {
38 * Record rec = qds.getRecord(i);
39 * int value = rec.getValue("column").asInt();
40 * System.out.println ( "The value is: " + value );
41 * }
42 * qds.close();
43 * </PRE>
44 * It is important to always remember to close() a QueryDataSet in order to free the allocated resources.
45 * </p>
46 *
47 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
48 * @version $Revision: 564 $
49 */
50 public class QueryDataSet
51 extends DataSet
52 {
53 /***
54 * Private...does nothing.
55 *
56 * @exception SQLException
57 * @exception DataSetException
58 */
59 public QueryDataSet()
60 throws SQLException, DataSetException
61 {
62 }
63
64 /***
65 * Creates a new QueryDataSet based on a connection and a select string
66 *
67 * @param conn
68 * @param selectStmt
69 *
70 * @exception SQLException
71 * @exception DataSetException
72 */
73 public QueryDataSet(Connection conn, String selectStmt)
74 throws SQLException, DataSetException
75 {
76 this.conn = conn;
77
78 selectString = new StringBuffer(selectStmt);
79
80 boolean ok = false;
81 try
82 {
83 stmt = conn.createStatement();
84 resultSet = stmt.executeQuery(selectStmt);
85 schema = new Schema();
86 schema.populate(resultSet.getMetaData(), null, conn);
87 ok = true;
88 }
89 finally
90 {
91 if (!ok)
92 {
93 try
94 {
95 close();
96 }
97 catch (Exception ignored)
98 {
99
100 }
101 }
102 }
103 }
104
105 /***
106 * Create a new QueryDataSet based on an existing resultSet
107 *
108 * @param resultSet
109 *
110 * @exception SQLException
111 * @exception DataSetException
112 */
113 public QueryDataSet(ResultSet resultSet)
114 throws SQLException, DataSetException
115 {
116 this.resultSet = resultSet;
117 schema = new Schema();
118 schema.populate(resultSet.getMetaData(), null, resultSet.getStatement().getConnection());
119 }
120
121 /***
122 * get the Select String that was used to create this QueryDataSet
123 *
124 * @return a select string
125 */
126 public String getSelectString()
127 {
128 return this.selectString.toString();
129 }
130 }