1 package org.mvnforum.phpbb2mvnforum.db.jdbc; 2 3 import java.sql.Connection ; 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.sql.Timestamp ; 8 import java.util.Collection ; 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 connection = null; 24 PreparedStatement statement = null; 25 ResultSet resultSet = null; 26 StringBuffer sql = new StringBuffer (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 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 54 public int createThread(int threadID, int forumID, String memberName, String lastPostMemberName, 55 String threadTopic, String threadBody, int threadVoteCount, int threadVoteTotalStars, 56 Timestamp threadCreationDate, Timestamp threadLastPostDate, int threadType, int threadOption, 57 int threadStatus, int threadHasPoll, int threadViewCount, int threadReplyCount, String threadIcon, 58 int threadDuration, int threadAttachCount) 59 throws CreateException, DatabaseException, DuplicateKeyException { 60 61 try { 65 findByPrimaryKey(threadID); 67 throw new DuplicateKeyException( 69 "Primary key already exists. Cannot create new thread with the same [ThreadID] (" + threadID + ")."); 70 } catch (ObjectNotFoundException e) { 71 } 73 74 Connection connection = null; 75 PreparedStatement statement = null; 76 StringBuffer sql = new StringBuffer (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 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 beans) { 119 121 } 122 123 } 124 | Popular Tags |