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.util.ArrayList ; 8 import java.util.Collection ; 9 10 import net.myvietnam.mvncore.exception.DatabaseException; 11 import net.myvietnam.mvncore.exception.ObjectNotFoundException; 12 13 import org.mvnforum.phpbb2mvnforum.db.PhpbbTopics; 14 import org.mvnforum.phpbb2mvnforum.db.PhpbbTopicsDAO; 15 import org.mvnforum.util.DBUtils; 16 17 public class PhpbbTopicsDAOImpl implements PhpbbTopicsDAO { 18 19 25 public Collection getBeans() throws DatabaseException { 26 27 Connection connection = null; 28 PreparedStatement statement = null; 29 ResultSet resultSet = null; 30 Collection retValue = new ArrayList (); 31 StringBuffer sql = new StringBuffer (512); 32 sql.append("SELECT topic_id, forum_id, topic_title, topic_poster, topic_time, topic_views, topic_replies, topic_status, topic_vote, topic_type, topic_first_post_id, topic_last_post_id, topic_moved_id"); 33 sql.append(" FROM " + TABLE_NAME); 34 try { 37 connection = DBUtils.getPhpbbConnection(); 38 statement = connection.prepareStatement(sql.toString()); 39 resultSet = statement.executeQuery(); 40 while (resultSet.next()) { 41 PhpbbTopics bean = new PhpbbTopics(); 42 bean.settopic_id(resultSet.getInt("topic_id")); 43 bean.setforum_id(resultSet.getInt("forum_id")); 44 bean.settopic_title(resultSet.getString("topic_title")); 45 bean.settopic_poster(resultSet.getInt("topic_poster")); 46 bean.settopic_time(resultSet.getInt("topic_time")); 47 bean.settopic_views(resultSet.getInt("topic_views")); 48 bean.settopic_replies(resultSet.getInt("topic_replies")); 49 bean.settopic_status(resultSet.getInt("topic_status")); 50 bean.settopic_vote(resultSet.getInt("topic_vote")); 51 bean.settopic_type(resultSet.getInt("topic_type")); 52 bean.settopic_first_post_id(resultSet.getInt("topic_first_post_id")); 53 bean.settopic_last_post_id(resultSet.getInt("topic_last_post_id")); 54 bean.settopic_moved_id(resultSet.getInt("topic_moved_id")); 55 retValue.add(bean); 56 } 57 return retValue; 58 } catch(SQLException sqle) { 59 throw new DatabaseException("Error executing SQL in phpbb_topicsDAOImplJDBC.getBeans."); 60 } finally { 61 DBUtils.closeResultSet(resultSet); 62 DBUtils.closeStatement(statement); 63 DBUtils.closeConnection(connection); 64 } 65 } 66 67 public Collection getBeansByForumID(int forumID) 68 throws DatabaseException { 69 Connection connection = null; 71 PreparedStatement statement = null; 72 ResultSet resultSet = null; 73 Collection retValue = new ArrayList (); 74 StringBuffer sql = new StringBuffer (512); 75 sql.append("SELECT topic_id, forum_id, topic_title, topic_poster, topic_time, topic_views, topic_replies, topic_status, topic_vote, topic_type, topic_first_post_id, topic_last_post_id, topic_moved_id"); 76 sql.append(" FROM " + TABLE_NAME); 77 sql.append(" WHERE forum_id = ?"); 78 try { 80 connection = DBUtils.getPhpbbConnection(); 81 statement = connection.prepareStatement(sql.toString()); 82 statement.setInt(1, forumID); 83 resultSet = statement.executeQuery(); 84 while (resultSet.next()) { 85 PhpbbTopics bean = new PhpbbTopics(); 86 bean.settopic_id(resultSet.getInt("topic_id")); 87 bean.setforum_id(resultSet.getInt("forum_id")); 88 bean.settopic_title(resultSet.getString("topic_title")); 89 bean.settopic_poster(resultSet.getInt("topic_poster")); 90 bean.settopic_time(resultSet.getInt("topic_time")); 91 bean.settopic_views(resultSet.getInt("topic_views")); 92 bean.settopic_replies(resultSet.getInt("topic_replies")); 93 bean.settopic_status(resultSet.getInt("topic_status")); 94 bean.settopic_vote(resultSet.getInt("topic_vote")); 95 bean.settopic_type(resultSet.getInt("topic_type")); 96 bean.settopic_first_post_id(resultSet.getInt("topic_first_post_id")); 97 bean.settopic_last_post_id(resultSet.getInt("topic_last_post_id")); 98 bean.settopic_moved_id(resultSet.getInt("topic_moved_id")); 99 retValue.add(bean); 100 } 101 return retValue; 102 } catch(SQLException sqle) { 103 throw new DatabaseException("Error executing SQL in phpbb_topicsDAOImplJDBC.getBeans."); 104 } finally { 105 DBUtils.closeResultSet(resultSet); 106 DBUtils.closeStatement(statement); 107 DBUtils.closeConnection(connection); 108 } 109 } 110 111 public PhpbbTopics getBeans(int topicID) throws ObjectNotFoundException, DatabaseException { 112 Connection connection = null; 114 PreparedStatement statement = null; 115 ResultSet resultSet = null; 116 StringBuffer sql = new StringBuffer (512); 117 sql.append("SELECT topic_id, forum_id, topic_title, topic_poster, topic_time, topic_views, topic_replies, topic_status, topic_vote, topic_type, topic_first_post_id, topic_last_post_id, topic_moved_id"); 118 sql.append(" FROM " + TABLE_NAME); 119 sql.append(" WHERE topic_id = ?"); 120 try { 121 connection = DBUtils.getPhpbbConnection(); 122 statement = connection.prepareStatement(sql.toString()); 123 statement.setInt(1, topicID); 124 resultSet = statement.executeQuery(); 125 if(!resultSet.next()) { 126 throw new ObjectNotFoundException("Cannot find the row in table phpbb_topics where primary key = (" + topicID + ")."); 127 } 128 129 PhpbbTopics bean = new PhpbbTopics(); 130 bean.settopic_id(resultSet.getInt("topic_id")); 133 bean.setforum_id(resultSet.getInt("forum_id")); 134 bean.settopic_title(resultSet.getString("topic_title")); 135 bean.settopic_poster(resultSet.getInt("topic_poster")); 136 bean.settopic_time(resultSet.getInt("topic_time")); 137 bean.settopic_views(resultSet.getInt("topic_views")); 138 bean.settopic_replies(resultSet.getInt("topic_replies")); 139 bean.settopic_status(resultSet.getInt("topic_status")); 140 bean.settopic_vote(resultSet.getInt("topic_vote")); 141 bean.settopic_type(resultSet.getInt("topic_type")); 142 bean.settopic_first_post_id(resultSet.getInt("topic_first_post_id")); 143 bean.settopic_last_post_id(resultSet.getInt("topic_last_post_id")); 144 bean.settopic_moved_id(resultSet.getInt("topic_moved_id")); 145 return bean; 146 } catch(SQLException sqle) { 147 throw new DatabaseException("Error executing SQL in phpbb_topicsDAOImplJDBC.getBean(pk)."); 148 } finally { 149 DBUtils.closeResultSet(resultSet); 150 DBUtils.closeStatement(statement); 151 DBUtils.closeConnection(connection); 152 } 153 } 154 } 155 | Popular Tags |