1 package org.cacert.gigi.database;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Timestamp;
9 public class GigiPreparedStatement implements AutoCloseable {
11 private PreparedStatement target;
13 private GigiResultSet rs;
15 protected GigiPreparedStatement(PreparedStatement preparedStatement) {
16 target = preparedStatement;
19 public GigiPreparedStatement(String stmt) {
23 public GigiPreparedStatement(String stmt, boolean scroll) {
25 target = DatabaseConnection.getInstance().prepareInternal(stmt, scroll);
26 } catch (SQLException e) {
31 public GigiResultSet executeQuery() {
33 return rs = new GigiResultSet(target.executeQuery());
34 } catch (SQLException e) {
40 public void executeUpdate() {
42 int updated = target.executeUpdate();
44 throw new Error("FATAL: multiple or no data updated: " + updated);
46 } catch (SQLException e) {
52 public boolean executeMaybeUpdate() {
54 int updated = target.executeUpdate();
56 throw new Error("More than one record (" + updated + ") updated.");
59 } catch (SQLException e) {
65 public boolean execute() {
67 return target.execute();
68 } catch (SQLException e) {
74 public void setInt(int parameterIndex, int x) {
76 target.setInt(parameterIndex, x);
77 } catch (SQLException e) {
83 public void setString(int parameterIndex, String x) {
85 target.setString(parameterIndex, x);
86 } catch (SQLException e) {
92 public void setEnum(int parameterIndex, DBEnum x) {
94 target.setString(parameterIndex, x.getDBName());
95 } catch (SQLException e) {
101 public void setDate(int parameterIndex, Date x) {
103 target.setDate(parameterIndex, x);
104 } catch (SQLException e) {
110 public void setTimestamp(int parameterIndex, Timestamp x) {
112 target.setTimestamp(parameterIndex, x);
113 } catch (SQLException e) {
119 public int lastInsertId() {
121 ResultSet rs = target.getGeneratedKeys();
123 int id = rs.getInt(1);
126 } catch (SQLException e) {
132 public void setBoolean(int parameterIndex, boolean x) {
134 target.setBoolean(parameterIndex, x);
135 } catch (SQLException e) {
141 public int getParameterCount() {
143 return target.getParameterMetaData().getParameterCount();
144 } catch (SQLException e) {
149 private void handleSQL(SQLException e) {
150 // TODO Auto-generated method stub
155 public void close() {
156 GigiResultSet r = rs;
160 PreparedStatement tg = target;
163 DatabaseConnection.getInstance().returnStatement(tg);
164 } catch (SQLException e) {