1 package com.sslexplorer.jdbc; 2 3 import java.sql.PreparedStatement ; 4 import java.sql.ResultSet ; 5 import java.sql.SQLException ; 6 import java.sql.Timestamp ; 7 import java.util.Calendar ; 8 9 20 public class JDBCPreparedStatement { 21 22 String key; 23 PreparedStatement ps; 24 JDBCDatabaseEngine con; 25 JDBCConnectionImpl impl; 26 String sql; 27 boolean transaction; 28 29 JDBCPreparedStatement(String key, 30 String sql, 31 JDBCDatabaseEngine con, 32 JDBCConnectionImpl impl, 33 boolean transaction) throws SQLException { 34 35 this.key = key; 36 this.con = con; 37 this.impl = impl; 38 this.sql = sql; 39 this.ps = impl.aquirePreparedStatement(key, sql); 40 this.transaction = transaction; 41 } 42 43 public JDBCConnectionImpl getConnection() { 44 return impl; 45 } 46 47 public void reprepare(String key, String sql) throws SQLException { 48 if(this.ps != null) { 49 impl.releasePreparedStatement(this.key, ps); 50 } 51 this.key = key; 52 this.sql = sql; 53 this.ps = impl.aquirePreparedStatement(key, sql); 54 reset(); 55 } 56 57 public boolean execute() throws SQLException { 58 return ps.execute(); 59 } 60 61 public void reset() throws SQLException { 62 ps.clearParameters(); 63 } 64 65 public ResultSet executeQuery() throws SQLException { 66 return ps.executeQuery(); 67 68 } 69 70 public int executeUpdate() throws SQLException { 71 return ps.executeUpdate(); 72 73 } 74 75 public PreparedStatement getPreparedStatement() { 76 return ps; 77 } 78 79 84 public void releasePreparedStatement() throws SQLException { 85 ps.clearParameters(); 86 impl.releasePreparedStatement(key, ps); 87 if(!transaction) { 88 con.releaseConnection(impl); 89 } 90 } 91 92 public void setTimestamp(int idx, Calendar calendar ) throws SQLException { 93 ps.setTimestamp ( idx, new Timestamp ( calendar.getTimeInMillis () ) ); 94 } 95 96 public void setString(int idx, String str) throws SQLException { 97 ps.setString(idx, str); 98 } 99 100 public void setNull(int idx, int parameterType) throws SQLException { 101 ps.setNull(idx, parameterType); 102 } 103 104 public void setInt(int idx, int val) throws SQLException { 105 ps.setInt(idx, val); 106 } 107 108 public void setLong(int idx, long val) throws SQLException { 109 ps.setLong(idx, val); 110 } 111 112 public String toString() { 113 return ps.toString(); 114 } 115 116 public void startTransaction() throws SQLException { 117 if(transaction) { 118 throw new SQLException ("Transaction already started."); 119 } 120 transaction = true; 121 impl.setAutoCommit(false); 122 } 123 124 public void endTransaction() throws SQLException { 125 if(!transaction) { 126 throw new SQLException ("Transaction already not started."); 127 } 128 transaction = false; 129 impl.setAutoCommit(true); 130 con.releaseConnection(impl); 131 } 132 133 public void rollback() throws SQLException { 134 impl.rollback(); 135 } 136 137 public void commit() throws SQLException { 138 impl.commit(); 139 } 140 } 141 | Popular Tags |