KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > db > AttendeeDB


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

12
13 /**
14  * This class implements the DBInterface for
15  * the Attendee class objects which are stored
16  * in the attendee database table.
17  */

18 public class AttendeeDB extends DBType implements DBInterface {
19     private Connection con;
20
21     private final static String JavaDoc selectQuery =
22             "SELECT " +
23                 "activity_pk, person_pk, " +
24                 "modified_by, modified_date " +
25             "FROM attendee" +
26             "WHERE pk=";
27
28     private final static String JavaDoc insertQuery =
29             "INSERT INTO attendee VALUES ( ";
30
31     private final static String JavaDoc deleteQuery =
32             "DELETE FROM attendee WHERE pk =";
33
34     /**
35      * a do-nothing constructor but necessary to
36      * do the operations offered by this class
37      *
38      */

39     public AttendeeDB() {
40     }
41
42     /**
43      * construct using a dbtype
44      * @param dbType the database type
45      */

46     public AttendeeDB(int dbType) {
47         DB_TYPE=dbType;
48     }
49
50     /**
51      * a constructor that accepts an existing Connection
52      * to use for future operations
53      *
54      * @param con the Connection to use
55      */

56     public AttendeeDB(Connection con) {
57         this.con = con;
58     }
59
60     /**
61      * get the Connection in use
62      *
63      * @return the Connection in use
64      */

65     public Connection getConnection() {
66         return this.con;
67     }
68
69     /**
70      * set the Connection to use
71      *
72      * @param con the Connection to use for any future IO's
73      */

74     public void setConnection(Connection con)
75         throws SQLException {
76
77         this.con = con;
78     }
79
80     /**
81      * select a single attendee row using the passed
82      * primary key
83      *
84      * @param pk the primary key we are searching with
85      * @return the Attendee(s) that were selected
86      * @exception java.sql.SQLException
87      */

88     public final Object JavaDoc selectRow(Object JavaDoc pk)
89         throws SQLException {
90
91         Attendee attendee = new Attendee();
92         attendee.setPK(((Long JavaDoc)pk).longValue());
93         Statement stmt = null;
94         ResultSet rs = null;
95         String JavaDoc query = selectQuery + attendee.getPK();
96
97         try {
98             stmt = con.createStatement();
99             if (Prefs.DEBUG) LogWrite.write(query);
100             rs = stmt.executeQuery(query);
101
102             int i;
103             while (rs.next()) {
104                 i=1;
105                 attendee.setActivityPK(rs.getLong(i)); i++;
106                 attendee.setPersonPK(rs.getLong(i)); i++;
107                 AddressDB pdb = new AddressDB(DB_TYPE);
108                 pdb.setConnection(getConnection());
109                 Address a = (Address)(pdb.selectRow(new Long JavaDoc(attendee.getPersonPK())));
110                 attendee.setPerson(a);
111                 attendee.setModifiedBy(rs.getString(i)); i++;
112                 attendee.setModifiedDate(rs.getDate(i));
113             }
114         } catch (SQLException e) {
115             throw e;
116         } finally {
117             try {
118                 if (rs != null) rs.close();
119             } catch (SQLException x) { throw x; }
120             try {
121                 if (stmt != null) stmt.close();
122             } catch (SQLException x) { throw x; }
123         }
124
125         return attendee;
126     }
127
128
129     /**
130      * not necessary at the moment
131      *
132      * @param name description
133      * @exception java.sql.SQLException
134      */

135     public void updateRow(Object JavaDoc obj)
136         throws SQLException {
137
138     }
139
140     /**
141      * insert a new attendee row using the passed
142      * Attendee object as the column values.
143      *
144      * @param obj the object we are inserting
145      * @return the newly assigned primary key of the new row
146      * @exception java.sql.SQLException
147      */

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

188     public final void deleteRow(Object JavaDoc obj)
189         throws SQLException {
190
191         long pkValue = ((Long JavaDoc)obj).longValue();
192         String JavaDoc query = deleteQuery + pkValue;
193     
194         Statement stmt = null;
195         try {
196             stmt = con.createStatement();
197             if (Prefs.DEBUG) LogWrite.write(query);
198             stmt.executeUpdate(query);
199         } catch (SQLException e) {
200             throw e;
201         } finally {
202             try { if (stmt != null) stmt.close();
203             } catch (SQLException x) { }
204         }
205     }
206     /**
207      * truncate the whole table
208      *
209      * @exception java.sql.SQLException
210      */

211     public final void truncate()
212         throws SQLException {
213
214         String JavaDoc query = "truncate table attendee";
215    
216         Statement stmt = null;
217         try {
218             stmt = con.createStatement();
219             if (Prefs.DEBUG) LogWrite.write(query);
220             stmt.executeUpdate(query);
221         } catch (SQLException e) {
222             throw e;
223         } finally {
224             try { if (stmt != null) stmt.close();
225             } catch (SQLException x) { }
226         }
227     }
228
229 }
230
Popular Tags