public long add(RoleBean bean) throws ApplicationException, DuplicateRecordException { log.debug("Model add Started"); Connection conn = null; int pk = 0; RoleBean duplicataRole = findByName(bean.getName()); // Check if create Role already exist if (duplicataRole != null) { throw new DuplicateRecordException("Role already exists"); } try { conn = JDBCDataSource.getConnection(); pk = nextPK(); // Get auto-generated next primary key System.out.println(pk + " in ModelJDBC"); conn.setAutoCommit(false); // Begin transaction PreparedStatement pstmt = conn.prepareStatement("INSERT INTO demo_ors.st_role VALUES(?,?,?,?,?,?,?)"); pstmt.setInt(1, pk); pstmt.setString(2, bean.getName()); pstmt.setString(3, bean.getDescription()); pstmt.setString(4, bean.getCreatedBy()); pstmt.setString(5, bean.getModifiedBy()); pstmt.setTimestamp(6, bean.getCreatedDatetime()); pstmt.setTimestamp(7, bean.getModifiedDatetime()); pstmt.executeUpdate(); conn.commit(); // End transaction pstmt.close(); } catch (Exception e) { e.printStackTrace(); log.error("Database Exception..", e); try { conn.rollback(); } catch (Exception ex) { throw new ApplicationException("Exception : add rollback exception " + ex.getMessage()); } throw new ApplicationException("Exception : Exception in add Role"); } finally { JDBCDataSource.closeConnection(conn); } log.debug("Model add End"); return pk; }
public void update(RoleBean bean) throws ApplicationException, DuplicateRecordException { log.debug("Model update Started"); Connection conn = null; RoleBean duplicataRole = findByName(bean.getName()); // Check if updated Role already exist if (duplicataRole != null && duplicataRole.getId() != bean.getId()) { throw new DuplicateRecordException("Role already exists"); } try { conn = JDBCDataSource.getConnection(); conn.setAutoCommit(false); // Begin transaction PreparedStatement pstmt = conn.prepareStatement( "UPDATE demo_ors.st_role SET NAME=?,DESCRIPTION=?,CREATED_BY=?,MODIFIED_BY=?,CREATED_DATETIME=?,MODIFIED_DATETIME=? WHERE ID=?"); pstmt.setString(1, bean.getName()); pstmt.setString(2, bean.getDescription()); pstmt.setString(3, bean.getCreatedBy()); pstmt.setString(4, bean.getModifiedBy()); pstmt.setTimestamp(5, bean.getCreatedDatetime()); pstmt.setTimestamp(6, bean.getModifiedDatetime()); pstmt.setLong(7, bean.getId()); pstmt.executeUpdate(); conn.commit(); // End transaction pstmt.close(); } catch (Exception e) { log.error("Database Exception..", e); try { conn.rollback(); } catch (Exception ex) { throw new ApplicationException("Exception : Delete rollback exception " + ex.getMessage()); } throw new ApplicationException("Exception in updating Role "); } finally { JDBCDataSource.closeConnection(conn); } log.debug("Model update End"); }