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.CategoryDAO; 16 import org.mvnforum.util.DBUtils; 17 18 public class CategoryDAOImplJDBC implements CategoryDAO { 19 20 public void findByPrimaryKey(int categoryID) 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 CategoryID"); 28 sql.append(" FROM " + TABLE_NAME); 29 sql.append(" WHERE CategoryID = ?"); 30 try { 31 connection = DBUtils.getMvnConnection(); 32 statement = connection.prepareStatement(sql.toString()); 33 statement.setInt(1, categoryID); 34 resultSet = statement.executeQuery(); 35 if (!resultSet.next()) { 36 throw new ObjectNotFoundException("Cannot find the primary key (" + categoryID 37 + ") in table 'category'."); 38 } 39 } catch (SQLException sqle) { 40 throw new DatabaseException("Error executing SQL in categoryDAOImplJDBC.findByPrimaryKey."); 41 } finally { 42 DBUtils.closeResultSet(resultSet); 43 DBUtils.closeStatement(statement); 44 DBUtils.closeConnection(connection); 45 } 46 } 47 48 public void findByAlternateKey_CategoryName(String categoryName) 49 throws ObjectNotFoundException, DatabaseException { 50 51 Connection connection = null; 52 PreparedStatement statement = null; 53 ResultSet resultSet = null; 54 StringBuffer sql = new StringBuffer (512); 55 sql.append("SELECT CategoryName"); 56 sql.append(" FROM " + TABLE_NAME); 57 sql.append(" WHERE CategoryName = ?"); 58 try { 59 connection = DBUtils.getMvnConnection(); 60 statement = connection.prepareStatement(sql.toString()); 61 statement.setString(1, categoryName); 62 resultSet = statement.executeQuery(); 63 if (!resultSet.next()) { 64 throw new ObjectNotFoundException("Cannot find the alternate key [CategoryName] (" + categoryName 65 + ") in table 'category'."); 66 } 67 } catch (SQLException sqle) { 68 throw new DatabaseException("Error executing SQL in categoryDAOImplJDBC.findByAlternateKey_CategoryName."); 69 } finally { 70 DBUtils.closeResultSet(resultSet); 71 DBUtils.closeStatement(statement); 72 DBUtils.closeConnection(connection); 73 } 74 } 75 76 81 public void create(int categoryID, int parentCategoryID, String categoryName, 82 String categoryDesc, Timestamp categoryCreationDate, Timestamp categoryModifiedDate, 83 int categoryOrder, int categoryOption, int categoryStatus) 84 throws CreateException, DatabaseException, DuplicateKeyException { 85 86 try { 90 findByPrimaryKey(categoryID); 92 throw new DuplicateKeyException("Primary key already exists. Cannot create new category with the same [CategoryID] (" + categoryID + ")."); 94 } catch(ObjectNotFoundException e) { 95 } 97 98 try { 101 findByAlternateKey_CategoryName(categoryName); 103 throw new DuplicateKeyException("Alternate key already exists. Cannot create new category with the same [CategoryName] (" + categoryName + ")."); 105 } catch(ObjectNotFoundException e) { 106 } 108 109 Connection connection = null; 110 PreparedStatement statement = null; 111 StringBuffer sql = new StringBuffer (512); 112 sql.append("INSERT INTO " + TABLE_NAME + " (CategoryID, ParentCategoryID, CategoryName, CategoryDesc, CategoryCreationDate, CategoryModifiedDate, CategoryOrder, CategoryOption, CategoryStatus)"); 113 sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); 114 try { 115 connection = DBUtils.getMvnConnection(); 116 statement = connection.prepareStatement(sql.toString()); 117 118 statement.setInt(1, categoryID); 119 statement.setInt(2, parentCategoryID); 120 statement.setString(3, categoryName); 121 statement.setString(4, categoryDesc); 122 statement.setTimestamp(5, categoryCreationDate); 123 statement.setTimestamp(6, categoryModifiedDate); 124 statement.setInt(7, categoryOrder); 125 statement.setInt(8, categoryOption); 126 statement.setInt(9, categoryStatus); 127 128 if (statement.executeUpdate() != 1) { 129 throw new CreateException("Error adding a row into table 'category'."); 130 } 131 } catch(SQLException sqle) { 132 throw new DatabaseException("Error executing SQL in categoryDAOImplJDBC.create."); 133 } finally { 134 DBUtils.closeStatement(statement); 135 DBUtils.closeConnection(connection); 136 } 137 } 138 139 public void createMultiple(Collection beans) { 140 142 } 143 } 144 | Popular Tags |