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.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 connection = null; 26 PreparedStatement statement = null; 27 ResultSet resultSet = null; 28 StringBuffer sql = new StringBuffer (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 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 56 public void create(int postID, int parentPostID, int forumID, 57 int threadID, int memberID, String memberName, 58 String lastEditMemberName, String postTopic, String postBody, 59 Timestamp postCreationDate, Timestamp postLastEditDate, String postCreationIP, 60 String postLastEditIP, int postEditCount, int postFormatOption, 61 int postOption, int postStatus, String postIcon, 62 int postAttachCount) 63 throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException { 64 65 try { 69 findByPrimaryKey(postID); 71 throw new DuplicateKeyException("Primary key already exists. Cannot create new post with the same [PostID] (" + postID + ")."); 73 } catch(ObjectNotFoundException e) { 74 } 76 Connection connection = null; 77 PreparedStatement statement = null; 78 StringBuffer sql = new StringBuffer (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 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 beans) { 117 119 } 120 } 121 | Popular Tags |