KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > db > DeleteInfoDB


1 package sellwin.db;
2
3 import sellwin.domain.*;
4 import sellwin.utils.*;
5
6 import java.sql.*;
7 import java.util.*;
8
9 // SellWin http://sourceforge.net/projects/sellwincrm
10
//Contact support@open-app.com for commercial help with SellWin
11
//This software is provided "AS IS", without a warranty of any kind.
12

13
14 /**
15  * This class implements the DBInterface for
16  * the DeleteInfo class objects which are stored
17  * in the delete_info database table.
18  */

19 public class DeleteInfoDB extends DBType implements DBInterface {
20
21     private Connection con;
22
23     private final static String JavaDoc selectQuery =
24             "SELECT " +
25                 "class_name " +
26             "FROM delete_info " +
27             "WHERE pk=";
28
29     private final static String JavaDoc selectAllQuery =
30             "SELECT * FROM delete_info ORDER BY pk";
31
32     private final static String JavaDoc insertQuery =
33             "INSERT INTO delete_info VALUES ( ";
34
35     private final static String JavaDoc deleteQuery =
36             "DELETE FROM delete_info WHERE pk =";
37
38     private final static String JavaDoc deleteAllQuery =
39             "DELETE FROM delete_info";
40
41     /**
42      * a do-nothing constructor but necessary to
43      * do the operations offered by this class
44      */

45     public DeleteInfoDB() {
46     }
47
48     /**
49      * construct using a db type
50      * @param dbType the database type
51      */

52     public DeleteInfoDB(int dbType) {
53         DB_TYPE=dbType;
54     }
55
56     /**
57      * a constructor that accepts an existing Connection
58      * to use for future operations
59      *
60      * @param con the Connection to use
61      */

62     public DeleteInfoDB(Connection con) {
63         this.con = con;
64     }
65
66     /**
67      * get the Connection in use
68      *
69      * @return the Connection in use
70      */

71     public Connection getConnection() {
72         return this.con;
73     }
74
75     /**
76      * set the Connection to use
77      *
78      * @param con the Connection to use for any future IO's
79      */

80     public void setConnection(Connection con)
81         throws SQLException {
82
83         this.con = con;
84     }
85
86     /**
87      * select a single delete_info row using the passed
88      * primary key
89      *
90      * @param pk the primary key we are searching with
91      * @return the DeleteInfo(s) that were selected
92      * @exception java.sql.SQLException
93      */

94     public final Object JavaDoc selectRow(Object JavaDoc pk)
95         throws SQLException {
96
97         DeleteInfo deleteInfo=null;
98         Statement stmt = null;
99         ResultSet rs = null;
100         Long JavaDoc l = (Long JavaDoc)pk;
101
102         String JavaDoc query = selectQuery + l.longValue();
103
104         try {
105             stmt = con.createStatement();
106             if (Prefs.DEBUG) LogWrite.write(query);
107             rs = stmt.executeQuery(query);
108
109             int i;
110             while (rs.next()) {
111                 deleteInfo = new DeleteInfo(l.longValue(), rs.getString(1));
112             }
113         } catch (SQLException e) {
114             throw e;
115         } finally {
116             try {
117                 if (rs != null) rs.close();
118             } catch (SQLException x) { throw x; }
119             try {
120                 if (stmt != null) stmt.close();
121             } catch (SQLException x) { throw x; }
122         }
123
124         return deleteInfo;
125     }
126
127
128     /**
129      * not implemented, not needed
130      *
131      * @param obj the object we want to update with
132      * @exception java.sql.SQLException
133      */

134     public void updateRow(Object JavaDoc obj)
135         throws SQLException {
136
137         DeleteInfo deleteInfo = (DeleteInfo)obj;
138     }
139
140     /**
141      * insert a new deleteInfo row using the passed
142      * DeleteInfo object as the column values.
143      *
144      * @param obj the object we are inserting
145      * @param load not used by this routine
146      * @return the newly assigned primary key of the new row
147      * @exception java.sql.SQLException
148      */

149     public long insertRow(Object JavaDoc obj, boolean load)
150         throws SQLException {
151
152         DeleteInfo deleteInfo = (DeleteInfo)obj;
153
154         Statement stmt = null;
155         StringBuffer JavaDoc query = new StringBuffer JavaDoc(insertQuery);
156
157         try {
158             stmt = con.createStatement();
159
160             query.append(deleteInfo.pk).append(",");
161             query.append(JDBC.quoteMore(deleteInfo.className));
162             query.append(")");
163
164             if (Prefs.DEBUG) LogWrite.write(query.toString());
165             int rc = stmt.executeUpdate(query.toString());
166         } catch (SQLException e) {
167             throw e;
168         } finally {
169             try { if (stmt != null) stmt.close();
170             } catch (SQLException x) { }
171         }
172
173         return deleteInfo.pk;
174     }
175
176     /**
177      * delete a single deleteInfo row using the passed
178      * primary key value
179      *
180      * @param ojb primary key stored in a Long
181      * @exception java.sql.SQLException
182      */

183     public final void deleteRow(Object JavaDoc obj)
184         throws SQLException {
185
186         long pkValue = ((Long JavaDoc)obj).longValue();
187         String JavaDoc query = deleteQuery + pkValue;
188     
189         Statement stmt = null;
190         try {
191             stmt = con.createStatement();
192             if (Prefs.DEBUG) LogWrite.write(query);
193             stmt.executeUpdate(query);
194         } catch (SQLException e) {
195             throw e;
196         } finally {
197             try { if (stmt != null) stmt.close();
198             } catch (SQLException x) { }
199         }
200     }
201
202     /**
203      * select all delete_info rows
204      *
205      * @return an ArrayList of DeleteInfo objects
206      * @exception java.sql.SQLException
207      */

208     public final ArrayList selectAllRows()
209         throws SQLException {
210
211         DeleteInfo deleteInfo;
212         Statement stmt = null;
213         ResultSet rs = null;
214         String JavaDoc query = selectAllQuery;
215         ArrayList list = new ArrayList();
216
217         try {
218             stmt = con.createStatement();
219             if (Prefs.DEBUG) LogWrite.write(query);
220             rs = stmt.executeQuery(query);
221
222             while (rs.next()) {
223                 deleteInfo = new DeleteInfo(rs.getLong(1), rs.getString(2));
224                 list.add(deleteInfo);
225             }
226         } catch (SQLException e) {
227             throw e;
228         } finally {
229             try {
230                 if (rs != null) rs.close();
231             } catch (SQLException x) { throw x; }
232             try {
233                 if (stmt != null) stmt.close();
234             } catch (SQLException x) { throw x; }
235         }
236
237         return list;
238     }
239
240     /**
241      * delete all deleteInfo rows
242      *
243      * @exception java.sql.SQLException
244      */

245     public final void deleteAllRows()
246         throws SQLException {
247
248         String JavaDoc query = deleteAllQuery;
249     
250         Statement stmt = null;
251         try {
252             stmt = con.createStatement();
253             if (Prefs.DEBUG) LogWrite.write(query);
254             stmt.executeUpdate(query);
255         } catch (SQLException e) {
256             throw e;
257         } finally {
258             try { if (stmt != null) stmt.close();
259             } catch (SQLException x) { }
260         }
261     }
262     /**
263      * truncate the whole table
264      *
265      * @exception java.sql.SQLException
266      */

267     public final void truncate()
268         throws SQLException {
269
270         String JavaDoc query = "truncate table delete_info";
271    
272         Statement stmt = null;
273         try {
274             stmt = con.createStatement();
275             if (Prefs.DEBUG) LogWrite.write(query);
276             stmt.executeUpdate(query);
277         } catch (SQLException e) {
278             throw e;
279         } finally {
280             try { if (stmt != null) stmt.close();
281             } catch (SQLException x) { }
282         }
283     }
284
285 }
286
Popular Tags