KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mvnforum > phpbb2mvnforum > db > jdbc > CategoryDAOImplJDBC


1 package org.mvnforum.phpbb2mvnforum.db.jdbc;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.sql.Timestamp JavaDoc;
8 import java.util.Collection JavaDoc;
9
10 import net.myvietnam.mvncore.exception.CreateException;
11 import net.myvietnam.mvncore.exception.DatabaseException;
12 import net.myvietnam.mvncore.exception.DuplicateKeyException;
13 import net.myvietnam.mvncore.exception.ObjectNotFoundException;
14
15 import org.mvnforum.phpbb2mvnforum.db.CategoryDAO;
16 import org.mvnforum.util.DBUtils;
17
18 public class CategoryDAOImplJDBC implements CategoryDAO {
19
20     public void findByPrimaryKey(int categoryID)
21         throws ObjectNotFoundException, DatabaseException {
22
23         Connection JavaDoc connection = null;
24         PreparedStatement JavaDoc statement = null;
25         ResultSet JavaDoc resultSet = null;
26         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
27         sql.append("SELECT CategoryID");
28         sql.append(" FROM " + TABLE_NAME);
29         sql.append(" WHERE CategoryID = ?");
30         try {
31             connection = DBUtils.getMvnConnection();
32             statement = connection.prepareStatement(sql.toString());
33             statement.setInt(1, categoryID);
34             resultSet = statement.executeQuery();
35             if (!resultSet.next()) {
36                 throw new ObjectNotFoundException("Cannot find the primary key (" + categoryID
37                         + ") in table 'category'.");
38             }
39         } catch (SQLException JavaDoc sqle) {
40             throw new DatabaseException("Error executing SQL in categoryDAOImplJDBC.findByPrimaryKey.");
41         } finally {
42             DBUtils.closeResultSet(resultSet);
43             DBUtils.closeStatement(statement);
44             DBUtils.closeConnection(connection);
45         }
46     }
47
48     public void findByAlternateKey_CategoryName(String JavaDoc categoryName)
49         throws ObjectNotFoundException, DatabaseException {
50
51         Connection JavaDoc connection = null;
52         PreparedStatement JavaDoc statement = null;
53         ResultSet JavaDoc resultSet = null;
54         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
55         sql.append("SELECT CategoryName");
56         sql.append(" FROM " + TABLE_NAME);
57         sql.append(" WHERE CategoryName = ?");
58         try {
59             connection = DBUtils.getMvnConnection();
60             statement = connection.prepareStatement(sql.toString());
61             statement.setString(1, categoryName);
62             resultSet = statement.executeQuery();
63             if (!resultSet.next()) {
64                 throw new ObjectNotFoundException("Cannot find the alternate key [CategoryName] (" + categoryName
65                         + ") in table 'category'.");
66             }
67         } catch (SQLException JavaDoc sqle) {
68             throw new DatabaseException("Error executing SQL in categoryDAOImplJDBC.findByAlternateKey_CategoryName.");
69         } finally {
70             DBUtils.closeResultSet(resultSet);
71             DBUtils.closeStatement(statement);
72             DBUtils.closeConnection(connection);
73         }
74     }
75
76     /*
77      * Included columns: CategoryID, ParentCategoryID, CategoryName, CategoryDesc, CategoryCreationDate,
78      * CategoryModifiedDate, CategoryOrder, CategoryOption, CategoryStatus
79      * Excluded columns:
80      */

81     public void create(int categoryID, int parentCategoryID, String JavaDoc categoryName,
82                         String JavaDoc categoryDesc, Timestamp JavaDoc categoryCreationDate, Timestamp JavaDoc categoryModifiedDate,
83                         int categoryOrder, int categoryOption, int categoryStatus)
84         throws CreateException, DatabaseException/*, DuplicateKeyException*/, DuplicateKeyException {
85
86         // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
87
// If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
88
// However, if primary key is a auto_increament column, then you can safely delete this block
89
try {
90             //Check if primary key already exists
91
findByPrimaryKey(categoryID);
92             //If so, then we have to throw an exception
93
throw new DuplicateKeyException("Primary key already exists. Cannot create new category with the same [CategoryID] (" + categoryID + ").");
94         } catch(ObjectNotFoundException e) {
95             //Otherwise we can go ahead
96
}
97
98         // @todo: Comment this try-catch block if the needed columns dont have attribute 'include'
99
// If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
100
try {
101             //Check if alternate key already exists
102
findByAlternateKey_CategoryName(categoryName);
103             //If so, then we have to throw an exception
104
throw new DuplicateKeyException("Alternate key already exists. Cannot create new category with the same [CategoryName] (" + categoryName + ").");
105         } catch(ObjectNotFoundException e) {
106             //Otherwise we can go ahead
107
}
108
109         Connection JavaDoc connection = null;
110         PreparedStatement JavaDoc statement = null;
111         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
112         sql.append("INSERT INTO " + TABLE_NAME + " (CategoryID, ParentCategoryID, CategoryName, CategoryDesc, CategoryCreationDate, CategoryModifiedDate, CategoryOrder, CategoryOption, CategoryStatus)");
113         sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
114         try {
115             connection = DBUtils.getMvnConnection();
116             statement = connection.prepareStatement(sql.toString());
117
118             statement.setInt(1, categoryID);
119             statement.setInt(2, parentCategoryID);
120             statement.setString(3, categoryName);
121             statement.setString(4, categoryDesc);
122             statement.setTimestamp(5, categoryCreationDate);
123             statement.setTimestamp(6, categoryModifiedDate);
124             statement.setInt(7, categoryOrder);
125             statement.setInt(8, categoryOption);
126             statement.setInt(9, categoryStatus);
127
128             if (statement.executeUpdate() != 1) {
129                 throw new CreateException("Error adding a row into table 'category'.");
130             }
131         } catch(SQLException JavaDoc sqle) {
132             throw new DatabaseException("Error executing SQL in categoryDAOImplJDBC.create.");
133         } finally {
134             DBUtils.closeStatement(statement);
135             DBUtils.closeConnection(connection);
136         }
137     }
138
139     public void createMultiple(Collection JavaDoc beans) {
140         // TODO Auto-generated method stub
141

142     }
143 }
144
Popular Tags