KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smile > stored > StoredProcedure


1 package smile.stored;
2
3 import java.util.*;
4 import java.text.*;
5 import java.sql.*;
6
7 /**
8  * StoredProcedure is an abstract class that all procedures that interact with
9  * the tools database use. Copyright 2002 Smile Les motoristes Internet
10  * http://www.smile.fr/ Contact cofax@smile.fr for further information
11  *
12  * @author Smile Les motoristes Internet
13  * @created March 22, 2002
14  */

15 public abstract class StoredProcedure {
16
17     /**
18      * Description of the Field
19      */

20     protected ResultSet psResult;
21
22     /**
23      * Description of the Field
24      */

25     protected Statement psStatement;
26
27     /**
28      * Description of the Field
29      */

30     protected Connection psConnection;
31
32     /**
33      * Description of the Field
34      */

35     protected HashMap data = null;
36
37     /**
38      * Description of the Field
39      */

40     protected String JavaDoc psName = "StoredProcedure";
41
42     /**
43      * Description of the Field
44      */

45     protected String JavaDoc action = "";
46
47     /**
48      * Description of the Field
49      */

50     protected String JavaDoc targetDataStore;
51
52     /**
53      * Description of the Field
54      */

55     protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
56
57     // protected static final String queryResultOk = "SELECT returnValue='0'";
58
/**
59      * Description of the Field
60      */

61     protected final static String JavaDoc queryResultOk = "select 0 as returnValue";
62
63     // verification des parametres
64
/**
65      * Description of the Method
66      *
67      * @exception SQLException
68      * Description of the Exception
69      */

70     public abstract void checkParams() throws SQLException;
71
72     // choix du type d'action
73

74     /**
75      * Description of the Method
76      *
77      * @exception SQLException
78      * Description of the Exception
79      */

80     public abstract void checkAction() throws SQLException;
81
82     // execution du corps de la proc
83

84     /**
85      * Description of the Method
86      *
87      * @exception SQLException
88      * Description of the Exception
89      */

90     public abstract void executeAction() throws SQLException;
91
92     /**
93      * Description of the Method
94      *
95      * @param initData
96      * Description of the Parameter
97      * @param con
98      * Description of the Parameter
99      */

100     public void init(HashMap initData, Connection con) {
101         setConnection(con);
102         data = utils.HashMapToUpperCase(initData);
103     }
104
105     /**
106      * Sets the targetDataStore attribute of the StoredProcedure object
107      *
108      * @param pTargetDataStore
109      * The new targetDataStore value
110      */

111     public void setTargetDataStore(String JavaDoc pTargetDataStore) {
112         this.targetDataStore = pTargetDataStore;
113     }
114
115     /**
116      * Sets the connection attribute of the StoredProcedure object
117      *
118      * @param con
119      * The new connection value
120      */

121     public void setConnection(Connection con) {
122         this.psConnection = con;
123     }
124
125     /**
126      * Gets the resultSet attribute of the StoredProcedure object
127      *
128      * @return The resultSet value
129      */

130     public ResultSet getResultSet() {
131         return psResult;
132     }
133
134     /**
135      * Description of the Method
136      *
137      * @exception SQLException
138      * Description of the Exception
139      */

140     public void close() throws SQLException {
141         try {
142             if (psResult != null) {
143                 psResult.close();
144             }
145             if (psStatement != null) {
146                 psStatement.close();
147             }
148         } catch (SQLException sqle) {
149             utils.log("!! SQLException when trying to close statement and resultset !!");
150             throw sqle;
151         }
152     }
153
154     /**
155      * Description of the Method
156      *
157      * @exception SQLException
158      * Description of the Exception
159      */

160     public void execute() throws SQLException {
161         checkParams();
162         checkAction();
163         executeAction();
164         return;
165     }
166
167     // execute query
168
/**
169      * Description of the Method
170      *
171      * @param query
172      * Description of the Parameter
173      * @exception SQLException
174      * Description of the Exception
175      */

176     protected void execStatement(String JavaDoc query) throws SQLException {
177         try {
178             psStatement = psConnection.createStatement();
179             psStatement.execute(query);
180         } catch (SQLException sqle) {
181             throw sqle;
182         }
183     }
184
185     // execute query et rempli psResult
186
/**
187      * Description of the Method
188      *
189      * @param query
190      * Description of the Parameter
191      * @exception SQLException
192      * Description of the Exception
193      */

194     protected void execStatementToRS(String JavaDoc query) throws SQLException {
195         try {
196             psStatement = psConnection.createStatement();
197             psStatement.execute(query);
198             psResult = psStatement.getResultSet();
199
200         } catch (SQLException sqle) {
201             throw sqle;
202         }
203     }
204
205     /**
206      * Description of the Method
207      *
208      * @param query
209      * Description of the Parameter
210      * @return Description of the Return Value
211      * @exception SQLException
212      * Description of the Exception
213      */

214     protected String JavaDoc execStatementToValue(String JavaDoc query) throws SQLException {
215         try {
216             psStatement = psConnection.createStatement();
217             psStatement.execute(query);
218             psResult = psStatement.getResultSet();
219             psResult.next();
220             return (psResult.getString(1));
221         } catch (SQLException sqle) {
222             throw sqle;
223         }
224     }
225
226     /**
227      * replace a quote by a double quote Used for sql queries in Java
228      *
229      * @param str
230      * Description of the Parameter
231      * @return Description of the Return Value
232      */

233     protected String JavaDoc doubleQuote(String JavaDoc str) {
234         return utils.replaceString(str, "'", "''");
235     }
236
237     /**
238      * get the maximum of an int column of a table1 Used for sql queries in Java
239      *
240      * @param tableName
241      * Description of the Parameter
242      * @param ColumnName
243      * Description of the Parameter
244      * @return The max value
245      * @exception SQLException
246      * Description of the Exception
247      */

248     protected String JavaDoc getMax(String JavaDoc tableName, String JavaDoc ColumnName) throws SQLException {
249         String JavaDoc vReturn = "";
250         String JavaDoc v_getMax = "";
251         v_getMax += "SELECT MAX(" + ColumnName + ") ";
252         v_getMax += "FROM " + tableName;
253
254         vReturn = execStatementToValue(v_getMax);
255         return (vReturn);
256     }
257
258 }
259
Popular Tags