KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > jdbc > JDBCPreparedStatement


1 package com.sslexplorer.jdbc;
2
3 import java.sql.PreparedStatement JavaDoc;
4 import java.sql.ResultSet JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.sql.Timestamp JavaDoc;
7 import java.util.Calendar JavaDoc;
8
9 /**
10  * <p>Title: </p>
11  *
12  * <p>Description: </p>
13  *
14  * <p>Copyright: Copyright (c) 2004</p>
15  *
16  * <p>Company: </p>
17  *
18  * @author not attributable
19  */

20 public class JDBCPreparedStatement {
21
22     String JavaDoc key;
23     PreparedStatement JavaDoc ps;
24     JDBCDatabaseEngine con;
25     JDBCConnectionImpl impl;
26     String JavaDoc sql;
27     boolean transaction;
28
29     JDBCPreparedStatement(String JavaDoc key,
30                           String JavaDoc sql,
31                           JDBCDatabaseEngine con,
32                           JDBCConnectionImpl impl,
33                           boolean transaction) throws SQLException JavaDoc {
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 JavaDoc key, String JavaDoc sql) throws SQLException JavaDoc {
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 JavaDoc {
58         return ps.execute();
59     }
60
61     public void reset() throws SQLException JavaDoc {
62         ps.clearParameters();
63     }
64
65     public ResultSet JavaDoc executeQuery() throws SQLException JavaDoc {
66         return ps.executeQuery();
67             
68     }
69
70     public int executeUpdate() throws SQLException JavaDoc {
71         return ps.executeUpdate();
72             
73     }
74
75     public PreparedStatement JavaDoc getPreparedStatement() {
76         return ps;
77     }
78
79     /**
80      * Release the statement and the connection
81      *
82      * @throws SQLException
83      */

84     public void releasePreparedStatement() throws SQLException JavaDoc {
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 JavaDoc calendar ) throws SQLException JavaDoc {
93             ps.setTimestamp ( idx, new Timestamp JavaDoc ( calendar.getTimeInMillis () ) );
94     }
95
96     public void setString(int idx, String JavaDoc str) throws SQLException JavaDoc {
97         ps.setString(idx, str);
98     }
99
100     public void setNull(int idx, int parameterType) throws SQLException JavaDoc {
101         ps.setNull(idx, parameterType);
102     }
103
104     public void setInt(int idx, int val) throws SQLException JavaDoc {
105         ps.setInt(idx, val);
106     }
107
108     public void setLong(int idx, long val) throws SQLException JavaDoc {
109         ps.setLong(idx, val);
110     }
111     
112     public String JavaDoc toString() {
113         return ps.toString();
114     }
115
116     public void startTransaction() throws SQLException JavaDoc {
117         if(transaction) {
118             throw new SQLException JavaDoc("Transaction already started.");
119         }
120         transaction = true;
121         impl.setAutoCommit(false);
122     }
123
124     public void endTransaction() throws SQLException JavaDoc {
125         if(!transaction) {
126             throw new SQLException JavaDoc("Transaction already not started.");
127         }
128         transaction = false;
129         impl.setAutoCommit(true);
130         con.releaseConnection(impl);
131     }
132     
133     public void rollback() throws SQLException JavaDoc {
134         impl.rollback();
135     }
136     
137     public void commit() throws SQLException JavaDoc {
138         impl.commit();
139     }
140 }
141
Popular Tags