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.PhpbbPost; 14 import org.mvnforum.phpbb2mvnforum.db.PhpbbPostDAO; 15 import org.mvnforum.util.DBUtils; 16 17 public class PhpbbPostDAOImpl implements PhpbbPostDAO { 18 19 25 public int getPosterIDFromPostID(int post_id) 26 throws ObjectNotFoundException, DatabaseException { 27 28 Connection connection = null; 29 PreparedStatement statement = null; 30 ResultSet resultSet = null; 31 StringBuffer sql = new StringBuffer (512); 32 sql.append("SELECT poster_id"); 33 sql.append(" FROM " + TABLE_NAME); 34 sql.append(" WHERE post_id = ?"); 35 int posterID = 0; 36 try { 37 connection = DBUtils.getPhpbbConnection(); 38 statement = connection.prepareStatement(sql.toString()); 39 statement.setInt(1, post_id); 40 resultSet = statement.executeQuery(); 41 if (!resultSet.next()) { 42 throw new ObjectNotFoundException("Cannot find the row in table phpbb_posts where primary key = (" 43 + post_id + ")."); 44 } 45 46 posterID = resultSet.getInt("poster_id"); 47 } catch (SQLException sqle) { 48 throw new DatabaseException("Error executing SQL in phpbb_postsDAOImplJDBC.getBean(pk)."); 49 } finally { 50 DBUtils.closeResultSet(resultSet); 51 DBUtils.closeStatement(statement); 52 DBUtils.closeConnection(connection); 53 } 54 return posterID; 55 } 56 57 63 public Collection getBeans() 64 throws DatabaseException { 65 66 Connection connection = null; 67 PreparedStatement statement = null; 68 ResultSet resultSet = null; 69 Collection retValue = new ArrayList (); 70 StringBuffer sql = new StringBuffer (512); 71 sql 72 .append("SELECT post_id, topic_id, forum_id, poster_id, post_time, poster_ip, post_username, enable_bbcode, enable_html, enable_smilies, enable_sig, post_edit_time, post_edit_count"); 73 sql.append(" FROM " + TABLE_NAME); 74 try { 77 connection = DBUtils.getPhpbbConnection(); 78 statement = connection.prepareStatement(sql.toString()); 79 resultSet = statement.executeQuery(); 80 while (resultSet.next()) { 81 PhpbbPost bean = new PhpbbPost(); 82 bean.setpost_id(resultSet.getInt("post_id")); 83 bean.settopic_id(resultSet.getInt("topic_id")); 84 bean.setforum_id(resultSet.getInt("forum_id")); 85 bean.setposter_id(resultSet.getInt("poster_id")); 86 bean.setpost_time(resultSet.getInt("post_time")); 87 bean.setposter_ip(resultSet.getString("poster_ip")); 88 bean.setpost_username(resultSet.getString("post_username")); 89 bean.setenable_bbcode(resultSet.getInt("enable_bbcode")); 90 bean.setenable_html(resultSet.getInt("enable_html")); 91 bean.setenable_smilies(resultSet.getInt("enable_smilies")); 92 bean.setenable_sig(resultSet.getInt("enable_sig")); 93 bean.setpost_edit_time(resultSet.getInt("post_edit_time")); 94 bean.setpost_edit_count(resultSet.getInt("post_edit_count")); 95 retValue.add(bean); 96 } 97 return retValue; 98 } catch (SQLException sqle) { 99 throw new DatabaseException("Error executing SQL in phpbb_postsDAOImplJDBC.getBeans."); 100 } finally { 101 DBUtils.closeResultSet(resultSet); 102 DBUtils.closeStatement(statement); 103 DBUtils.closeConnection(connection); 104 } 105 } 106 107 113 public Collection getPostIDsFromTopicID(int topicID) 114 throws DatabaseException { 115 116 Connection connection = null; 117 PreparedStatement statement = null; 118 ResultSet resultSet = null; 119 Collection retValue = new ArrayList (); 120 StringBuffer sql = new StringBuffer (512); 121 sql.append("SELECT post_id"); 122 sql.append(" FROM " + TABLE_NAME); 123 sql.append(" WHERE topic_id = ?"); 124 try { 125 connection = DBUtils.getPhpbbConnection(); 126 statement = connection.prepareStatement(sql.toString()); 127 statement.setInt(1, topicID); 128 resultSet = statement.executeQuery(); 129 130 while (resultSet.next()) { 131 132 PhpbbPost bean = new PhpbbPost(); 133 bean.setpost_id(resultSet.getInt("post_id")); 134 retValue.add(bean); 135 } 136 return retValue; 137 } catch (SQLException sqle) { 138 throw new DatabaseException("Error executing SQL in phpbb_postsDAOImplJDBC.getBean(pk)."); 139 } finally { 140 DBUtils.closeResultSet(resultSet); 141 DBUtils.closeStatement(statement); 142 DBUtils.closeConnection(connection); 143 } 144 } 145 146 public long getPostTimeFromPostID(int post_id) throws ObjectNotFoundException, DatabaseException { 147 Connection connection = null; 149 PreparedStatement statement = null; 150 ResultSet resultSet = null; 151 StringBuffer sql = new StringBuffer (512); 152 sql.append("SELECT post_time"); 153 sql.append(" FROM " + TABLE_NAME); 154 sql.append(" WHERE post_id = ?"); 155 try { 156 connection = DBUtils.getPhpbbConnection(); 157 statement = connection.prepareStatement(sql.toString()); 158 statement.setInt(1, post_id); 159 resultSet = statement.executeQuery(); 160 if(!resultSet.next()) { 161 throw new ObjectNotFoundException("Cannot find the row in table phpbb_posts where primary key = (" + post_id + ")."); 162 } 163 return resultSet.getLong("post_time"); 164 } catch(SQLException sqle) { 165 throw new DatabaseException("Error executing SQL in phpbb_postsDAOImplJDBC.getBean(pk)."); 166 } finally { 167 DBUtils.closeResultSet(resultSet); 168 DBUtils.closeStatement(statement); 169 DBUtils.closeConnection(connection); 170 } 171 172 173 } 174 175 public Collection getBeansByThreadID(int threadID) 176 throws DatabaseException { 177 Connection connection = null; 179 PreparedStatement statement = null; 180 ResultSet resultSet = null; 181 Collection retValue = new ArrayList (); 182 StringBuffer sql = new StringBuffer (512); 183 sql 184 .append("SELECT post_id, topic_id, forum_id, poster_id, post_time, poster_ip, post_username, enable_bbcode, enable_html, enable_smilies, enable_sig, post_edit_time, post_edit_count"); 185 sql.append(" FROM " + TABLE_NAME); 186 sql.append(" WHERE topic_id = ?"); 187 try { 189 connection = DBUtils.getPhpbbConnection(); 190 statement = connection.prepareStatement(sql.toString()); 191 statement.setInt(1, threadID); 192 resultSet = statement.executeQuery(); 193 while (resultSet.next()) { 194 PhpbbPost bean = new PhpbbPost(); 195 bean.setpost_id(resultSet.getInt("post_id")); 196 bean.settopic_id(resultSet.getInt("topic_id")); 197 bean.setforum_id(resultSet.getInt("forum_id")); 198 bean.setposter_id(resultSet.getInt("poster_id")); 199 bean.setpost_time(resultSet.getInt("post_time")); 200 bean.setposter_ip(resultSet.getString("poster_ip")); 201 bean.setpost_username(resultSet.getString("post_username")); 202 bean.setenable_bbcode(resultSet.getInt("enable_bbcode")); 203 bean.setenable_html(resultSet.getInt("enable_html")); 204 bean.setenable_smilies(resultSet.getInt("enable_smilies")); 205 bean.setenable_sig(resultSet.getInt("enable_sig")); 206 bean.setpost_edit_time(resultSet.getInt("post_edit_time")); 207 bean.setpost_edit_count(resultSet.getInt("post_edit_count")); 208 retValue.add(bean); 209 } 210 return retValue; 211 } catch (SQLException sqle) { 212 throw new DatabaseException("Error executing SQL in phpbb_postsDAOImplJDBC.getBeans."); 213 } finally { 214 DBUtils.closeResultSet(resultSet); 215 DBUtils.closeStatement(statement); 216 DBUtils.closeConnection(connection); 217 } 218 } 219 220 } 221 | Popular Tags |