01: package net.sourceforge.jaxor.impl;
02:
03: import net.sourceforge.jaxor.MetaRow;
04: import net.sourceforge.jaxor.QueryParams;
05: import net.sourceforge.jaxor.api.EntityResultSet;
06: import net.sourceforge.jaxor.api.JaxorContext;
07: import net.sourceforge.jaxor.api.JaxorPreparedStatement;
08: import net.sourceforge.jaxor.util.SystemException;
09:
10: import java.sql.Connection;
11: import java.sql.PreparedStatement;
12: import java.sql.ResultSet;
13: import java.sql.SQLException;
14:
15: /**
16: * Created By: Mike
17: * Date: Jan 13, 2004
18: * Time: 9:23:42 PM
19: *
20: * Last Checkin: $Author: mrettig $
21: * Date: $Date: 2004/01/24 18:02:37 $
22: * Revision: $Revision: 1.4 $
23: */
24: public class JaxorPreparedStatementImpl implements
25: JaxorPreparedStatement {
26:
27: private final MetaRow _metaRow;
28: private final QueryParams _params;
29: private final PreparedStatement _statement;
30: private final JaxorContext _context;
31: private final Connection _conn;
32:
33: public JaxorPreparedStatementImpl(MetaRow metaRow,
34: QueryParams params, String sql, JaxorContext context) {
35: _metaRow = metaRow;
36: _params = params;
37: _conn = context.getConnection();
38: try {
39: _statement = _conn.prepareStatement(sql);
40: } catch (SQLException e) {
41: throw new SystemException(e);
42: }
43: _context = context;
44: }
45:
46: public QueryParams getParams() {
47: return _params;
48: }
49:
50: public PreparedStatement getStatement() {
51: return _statement;
52: }
53:
54: public EntityResultSet executeAsEntityResultSet() {
55: ResultSet rs = execute();
56: return new EntityResultSetImpl(rs, _context, _metaRow);
57: }
58:
59: public ResultSet execute() {
60: _params.setArgs(_statement, _context.getMapperRegistry());
61: try {
62: return _statement.executeQuery();
63: } catch (SQLException e) {
64: throw new SystemException(e);
65: }
66: }
67:
68: /**
69: * Must be called to close the encapsulated prepared statement.
70: */
71: public void close() {
72: safeClose();
73: }
74:
75: private void safeClose() {
76: try {
77: if (_statement != null)
78: _statement.close();
79: _conn.close();
80: } catch (SQLException e) {
81: e.printStackTrace();
82: }
83: }
84: }
|