KickJava   Java API By Example, From Geeks To Geeks.

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


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.ThreadDAO;
16 import org.mvnforum.util.DBUtils;
17
18 public class ThreadDAOImplJDBC implements ThreadDAO {
19
20     public void findByPrimaryKey(int threadID)
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 ThreadID");
28         sql.append(" FROM " + TABLE_NAME);
29         sql.append(" WHERE ThreadID = ?");
30         try {
31             connection = DBUtils.getMvnConnection();
32             statement = connection.prepareStatement(sql.toString());
33             statement.setInt(1, threadID);
34             resultSet = statement.executeQuery();
35             if (!resultSet.next()) {
36                 throw new ObjectNotFoundException("Cannot find the primary key (" + threadID + ") in table 'thread'.");
37             }
38         } catch (SQLException JavaDoc sqle) {
39             throw new DatabaseException("Error executing SQL in threadDAOImplJDBC.findByPrimaryKey.");
40         } finally {
41             DBUtils.closeResultSet(resultSet);
42             DBUtils.closeStatement(statement);
43             DBUtils.closeConnection(connection);
44         }
45     }
46
47     /*
48      * Included columns: ThreadID, ForumID, MemberName, LastPostMemberName, ThreadTopic,
49      * ThreadBody, ThreadVoteCount, ThreadVoteTotalStars, ThreadCreationDate, ThreadLastPostDate,
50      * ThreadType, ThreadOption, ThreadStatus, ThreadHasPoll, ThreadViewCount,
51      * ThreadReplyCount, ThreadIcon, ThreadDuration, ThreadAttachCount
52      * Excluded columns:
53      */

54     public int createThread(int threadID, int forumID, String JavaDoc memberName, String JavaDoc lastPostMemberName,
55             String JavaDoc threadTopic, String JavaDoc threadBody, int threadVoteCount, int threadVoteTotalStars,
56             Timestamp JavaDoc threadCreationDate, Timestamp JavaDoc threadLastPostDate, int threadType, int threadOption,
57             int threadStatus, int threadHasPoll, int threadViewCount, int threadReplyCount, String JavaDoc threadIcon,
58             int threadDuration, int threadAttachCount)
59         throws CreateException, DatabaseException, DuplicateKeyException {
60
61         // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
62
// If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
63
// However, if primary key is a auto_increament column, then you can safely delete this block
64
try {
65             //Check if primary key already exists
66
findByPrimaryKey(threadID);
67             //If so, then we have to throw an exception
68
throw new DuplicateKeyException(
69                     "Primary key already exists. Cannot create new thread with the same [ThreadID] (" + threadID + ").");
70         } catch (ObjectNotFoundException e) {
71             //Otherwise we can go ahead
72
}
73
74         Connection JavaDoc connection = null;
75         PreparedStatement JavaDoc statement = null;
76         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
77         sql
78                 .append("INSERT INTO "
79                         + TABLE_NAME
80                         + " (ThreadID, ForumID, MemberName, LastPostMemberName, ThreadTopic, ThreadBody, ThreadVoteCount, ThreadVoteTotalStars, ThreadCreationDate, ThreadLastPostDate, ThreadType, ThreadOption, ThreadStatus, ThreadHasPoll, ThreadViewCount, ThreadReplyCount, ThreadIcon, ThreadDuration, ThreadAttachCount)");
81         sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
82         try {
83             connection = DBUtils.getMvnConnection();
84             statement = connection.prepareStatement(sql.toString());
85
86             statement.setInt(1, threadID);
87             statement.setInt(2, forumID);
88             statement.setString(3, memberName);
89             statement.setString(4, lastPostMemberName);
90             statement.setString(5, threadTopic);
91             statement.setString(6, threadBody);
92             statement.setInt(7, threadVoteCount);
93             statement.setInt(8, threadVoteTotalStars);
94             statement.setTimestamp(9, threadCreationDate);
95             statement.setTimestamp(10, threadLastPostDate);
96             statement.setInt(11, threadType);
97             statement.setInt(12, threadOption);
98             statement.setInt(13, threadStatus);
99             statement.setInt(14, threadHasPoll);
100             statement.setInt(15, threadViewCount);
101             statement.setInt(16, threadReplyCount);
102             statement.setString(17, threadIcon);
103             statement.setInt(18, threadDuration);
104             statement.setInt(19, threadAttachCount);
105
106             if (statement.executeUpdate() != 1) {
107                 throw new CreateException("Error adding a row into table 'thread'.");
108             }
109             return 0;
110         } catch (SQLException JavaDoc sqle) {
111             throw new DatabaseException("Error executing SQL in threadDAOImplJDBC.create.");
112         } finally {
113             DBUtils.closeStatement(statement);
114             DBUtils.closeConnection(connection);
115         }
116     }
117
118     public void createMultiple(Collection JavaDoc beans) {
119         // TODO Auto-generated method stub
120

121     }
122
123 }
124
Popular Tags