1 package club.wpia.gigi.database;
4 import java.sql.ParameterMetaData;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.Timestamp;
10 public class GigiPreparedStatement implements AutoCloseable {
12 private PreparedStatement target;
14 private GigiResultSet rs;
16 protected GigiPreparedStatement(PreparedStatement preparedStatement) {
17 target = preparedStatement;
20 public GigiPreparedStatement(String stmt) {
24 public GigiPreparedStatement(String stmt, boolean scroll) {
26 target = DatabaseConnection.getInstance().prepareInternal(stmt, scroll);
27 } catch (SQLException e) {
32 public GigiResultSet executeQuery() {
34 return rs = new GigiResultSet(target.executeQuery());
35 } catch (SQLException e) {
41 public void executeUpdate() {
43 int updated = target.executeUpdate();
45 throw new Error("FATAL: multiple or no data updated: " + updated);
47 } catch (SQLException e) {
53 public boolean executeMaybeUpdate() {
55 int updated = target.executeUpdate();
57 throw new Error("More than one record (" + updated + ") updated.");
60 } catch (SQLException e) {
66 public boolean execute() {
68 return target.execute();
69 } catch (SQLException e) {
75 public void setInt(int parameterIndex, int x) {
77 target.setInt(parameterIndex, x);
78 } catch (SQLException e) {
84 public void setString(int parameterIndex, String x) {
86 target.setString(parameterIndex, x);
87 } catch (SQLException e) {
93 public void setEnum(int parameterIndex, DBEnum x) {
95 target.setString(parameterIndex, x.getDBName());
96 } catch (SQLException e) {
102 public void setDate(int parameterIndex, Date x) {
104 target.setDate(parameterIndex, x);
105 } catch (SQLException e) {
111 public void setTimestamp(int parameterIndex, Timestamp x) {
113 target.setTimestamp(parameterIndex, x);
114 } catch (SQLException e) {
120 public int lastInsertId() {
122 ResultSet rs = target.getGeneratedKeys();
124 int id = rs.getInt(1);
127 } catch (SQLException e) {
133 public void setBoolean(int parameterIndex, boolean x) {
135 target.setBoolean(parameterIndex, x);
136 } catch (SQLException e) {
142 public ParameterMetaData getParameterMetaData() {
144 return target.getParameterMetaData();
145 } catch (SQLException e) {
151 private void handleSQL(SQLException e) {
152 // TODO Auto-generated method stub
157 public void close() {
158 GigiResultSet r = rs;
162 PreparedStatement tg = target;
165 DatabaseConnection.getInstance().returnStatement(tg);
166 } catch (SQLException e) {