KickJava   Java API By Example, From Geeks To Geeks.

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


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.ForeignKeyNotFoundException;
14 import net.myvietnam.mvncore.exception.ObjectNotFoundException;
15
16 import org.mvnforum.phpbb2mvnforum.db.PostDAO;
17 import org.mvnforum.util.DBUtils;
18
19
20 public class PostDAOImplJDBC implements PostDAO {
21
22     public void findByPrimaryKey(int postID)
23         throws ObjectNotFoundException, DatabaseException {
24
25         Connection JavaDoc connection = null;
26         PreparedStatement JavaDoc statement = null;
27         ResultSet JavaDoc resultSet = null;
28         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
29         sql.append("SELECT PostID");
30         sql.append(" FROM " + TABLE_NAME);
31         sql.append(" WHERE PostID = ?");
32         try {
33             connection = DBUtils.getMvnConnection();
34             statement = connection.prepareStatement(sql.toString());
35             statement.setInt(1, postID);
36             resultSet = statement.executeQuery();
37             if (!resultSet.next()) {
38                 throw new ObjectNotFoundException("Cannot find the primary key (" + postID + ") in table 'post'.");
39             }
40         } catch (SQLException JavaDoc sqle) {
41             throw new DatabaseException("Error executing SQL in postDAOImplJDBC.findByPrimaryKey.");
42         } finally {
43             DBUtils.closeResultSet(resultSet);
44             DBUtils.closeStatement(statement);
45             DBUtils.closeConnection(connection);
46         }
47     }
48     
49     /*
50      * Included columns: PostID, ParentPostID, ForumID, ThreadID, MemberID,
51      * MemberName, LastEditMemberName, PostTopic, PostBody, PostCreationDate,
52      * PostLastEditDate, PostCreationIP, PostLastEditIP, PostEditCount, PostFormatOption,
53      * PostOption, PostStatus, PostIcon, PostAttachCount
54      * Excluded columns:
55      */

56     public void create(int postID, int parentPostID, int forumID,
57                         int threadID, int memberID, String JavaDoc memberName,
58                         String JavaDoc lastEditMemberName, String JavaDoc postTopic, String JavaDoc postBody,
59                         Timestamp JavaDoc postCreationDate, Timestamp JavaDoc postLastEditDate, String JavaDoc postCreationIP,
60                         String JavaDoc postLastEditIP, int postEditCount, int postFormatOption,
61                         int postOption, int postStatus, String JavaDoc postIcon,
62                         int postAttachCount)
63         throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException {
64
65         // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
66
// If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
67
// However, if primary key is a auto_increament column, then you can safely delete this block
68
try {
69             //Check if primary key already exists
70
findByPrimaryKey(postID);
71             //If so, then we have to throw an exception
72
throw new DuplicateKeyException("Primary key already exists. Cannot create new post with the same [PostID] (" + postID + ").");
73         } catch(ObjectNotFoundException e) {
74             //Otherwise we can go ahead
75
}
76         Connection JavaDoc connection = null;
77         PreparedStatement JavaDoc statement = null;
78         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
79         sql.append("INSERT INTO " + TABLE_NAME + " (PostID, ParentPostID, ForumID, ThreadID, MemberID, MemberName, LastEditMemberName, PostTopic, PostBody, PostCreationDate, PostLastEditDate, PostCreationIP, PostLastEditIP, PostEditCount, PostFormatOption, PostOption, PostStatus, PostIcon, PostAttachCount)");
80         sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
81         try {
82             connection = DBUtils.getMvnConnection();
83             statement = connection.prepareStatement(sql.toString());
84
85             statement.setInt(1, postID);
86             statement.setInt(2, parentPostID);
87             statement.setInt(3, forumID);
88             statement.setInt(4, threadID);
89             statement.setInt(5, memberID);
90             statement.setString(6, memberName);
91             statement.setString(7, lastEditMemberName);
92             statement.setString(8, postTopic);
93             statement.setString(9, postBody);
94             statement.setTimestamp(10, postCreationDate);
95             statement.setTimestamp(11, postLastEditDate);
96             statement.setString(12, postCreationIP);
97             statement.setString(13, postLastEditIP);
98             statement.setInt(14, postEditCount);
99             statement.setInt(15, postFormatOption);
100             statement.setInt(16, postOption);
101             statement.setInt(17, postStatus);
102             statement.setString(18, postIcon);
103             statement.setInt(19, postAttachCount);
104
105             if (statement.executeUpdate() != 1) {
106                 throw new CreateException("Error adding a row into table 'post'.");
107             }
108         } catch(SQLException JavaDoc sqle) {
109             throw new DatabaseException("Error executing SQL in postDAOImplJDBC.create.");
110         } finally {
111             DBUtils.closeStatement(statement);
112             DBUtils.closeConnection(connection);
113         }
114     }
115
116     public void createMultiple(Collection JavaDoc beans) {
117         // TODO Auto-generated method stub
118

119     }
120 }
121
Popular Tags