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.Collection ; 8 9 import org.mvnforum.util.DBUtils; 10 11 import net.myvietnam.mvncore.exception.CreateException; 12 import net.myvietnam.mvncore.exception.DatabaseException; 13 import net.myvietnam.mvncore.exception.DuplicateKeyException; 14 import net.myvietnam.mvncore.exception.ForeignKeyNotFoundException; 15 import net.myvietnam.mvncore.exception.ObjectNotFoundException; 16 17 import com.mvnforum.db.MemberPermissionDAO; 18 19 public class MemberPermissionDAOImplJDBC implements MemberPermissionDAO { 20 21 public void findByPrimaryKey(int memberID, int permission) 22 throws ObjectNotFoundException, DatabaseException { 23 25 Connection connection = null; 26 PreparedStatement statement = null; 27 ResultSet resultSet = null; 28 StringBuffer sql = new StringBuffer (512); 29 sql.append("SELECT MemberID, Permission"); 30 sql.append(" FROM " + TABLE_NAME); 31 sql.append(" WHERE MemberID = ? AND Permission = ?"); 32 try { 33 connection = DBUtils.getMvnConnection(); 34 statement = connection.prepareStatement(sql.toString()); 35 statement.setInt(1, memberID); 36 statement.setInt(2, permission); 37 resultSet = statement.executeQuery(); 38 if (!resultSet.next()) { 39 throw new ObjectNotFoundException("Cannot find the primary key (" + memberID + ", " + permission + ") in table 'MemberPermission'."); 40 } 41 } catch (SQLException sqle) { 42 throw new DatabaseException("Error executing SQL in MemberPermissionDAOImplJDBC.findByPrimaryKey."); 43 } finally { 44 DBUtils.closeResultSet(resultSet); 45 DBUtils.closeStatement(statement); 46 DBUtils.closeConnection(connection); 47 } 48 } 49 50 public void create(int memberID, int permission) 51 throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException { 52 try { 53 findByPrimaryKey(memberID, permission); 55 throw new DuplicateKeyException("Primary key already exists. Cannot create new MemberPermission with the same [MemberID, Permission] (" + memberID + ", " + permission + ")."); 57 } catch (ObjectNotFoundException e) { 58 } 60 61 if (memberID != 0) { 62 DAOFactory factory = new DAOFactory (); 63 try { 64 factory.getMemberDAO().findByPrimaryKey(memberID); 65 } catch (ObjectNotFoundException e) { 66 e.printStackTrace(); 68 } catch (DatabaseException e) { 69 e.printStackTrace(); 71 } 72 } 73 74 Connection connection = null; 75 PreparedStatement statement = null; 76 StringBuffer sql = new StringBuffer (512); 77 sql.append("INSERT INTO " + TABLE_NAME + " (MemberID, Permission)"); 78 sql.append(" VALUES (?, ?)"); 79 try { 80 connection = DBUtils.getMvnConnection(); 81 statement = connection.prepareStatement(sql.toString()); 82 83 statement.setInt(1, memberID); 84 statement.setInt(2, permission); 85 86 if (statement.executeUpdate() != 1) { 87 throw new CreateException("Error adding a row into table 'MemberPermission'."); 88 } 89 } catch (SQLException sqle) { 90 throw new DatabaseException("Error executing SQL in MemberPermissionDAOImplJDBC.create."); 91 } finally { 92 DBUtils.closeStatement(statement); 93 DBUtils.closeConnection(connection); 94 } 95 96 97 } 98 99 public void delete(int memberID, int permission) 100 throws DatabaseException, ObjectNotFoundException { 101 103 } 104 105 public void delete_inMember(int memberID) 106 throws DatabaseException { 107 109 } 110 111 public Collection getBeans_inMember(int memberID) 112 throws DatabaseException { 113 return null; 115 } 116 117 } 118 | Popular Tags |