📄 companydaoimpljdbc.java
字号:
try {
//Check if alternate key already exists
findByAlternateKey_CompanySpaceName(companySpaceName);
//If so, then we have to throw an exception
throw new DuplicateKeyException("Alternate key already exists. Cannot create new Company with the same [CompanySpaceName] (" + companySpaceName + ").");
} catch(ObjectNotFoundException e) {
//Otherwise we can go ahead
}
try {
// @todo: modify the parameter list as needed
// You may have to regenerate this method if the needed columns dont have attribute 'include'
DAOFactory.getGroupsDAO().findByPrimaryKey(groupID);
} catch(ObjectNotFoundException e) {
throw new ForeignKeyNotFoundException("Foreign key refers to table 'Groups' does not exist. Cannot create new Company.");
}
Connection connection = null;
PreparedStatement statement = null;
StringBuffer sql = new StringBuffer(512);
sql.append("INSERT INTO " + TABLE_NAME + " (GroupID, CompanyName, CompanyAddress, CompanyCity, CompanyCAP, CompanyProvince, CompanyRegion, CompanyPhone, CompanyFax, CompanyWebsite, CompanyEmail, CompanySpaceName, CompanySpaceHeader, CompanySpaceFooter, CompanyVATNumber, CompanyLogo, CompanyCss, CompanyCreationDate, CompanyModifiedDate)");
sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
statement.setInt(1, groupID);
statement.setString(2, companyName);
statement.setString(3, companyAddress);
statement.setString(4, companyCity);
statement.setString(5, companyCAP);
statement.setString(6, companyProvince);
statement.setString(7, companyRegion);
statement.setString(8, companyPhone);
statement.setString(9, companyFax);
statement.setString(10, companyWebsite);
statement.setString(11, companyEmail);
statement.setString(12, companySpaceName);
statement.setString(13, companySpaceHeader);
statement.setString(14, companySpaceFooter);
statement.setString(15, companyVATNumber);
statement.setString(16, companyLogo);
statement.setString(17, companyCss);
statement.setTimestamp(18, companyCreationDate);
statement.setTimestamp(19, companyModifiedDate);
if (statement.executeUpdate() != 1) {
throw new CreateException("Error adding a row into table 'Company'.");
}
m_dirty = true;
} catch(SQLException sqle) {
log.error("Sql Execution Error!", sqle);
throw new DatabaseException("Error executing SQL in CompanyDAOImplJDBC.create.");
} finally {
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
public void delete(int companyID)
throws DatabaseException, ObjectNotFoundException {
Connection connection = null;
PreparedStatement statement = null;
StringBuffer sql = new StringBuffer(512);
sql.append("DELETE FROM " + TABLE_NAME);
sql.append(" WHERE CompanyID = ?");
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
statement.setInt(1, companyID);
if (statement.executeUpdate() != 1) {
throw new ObjectNotFoundException("Cannot delete a row in table Company where primary key = (" + companyID + ").");
}
m_dirty = true;
} catch(SQLException sqle) {
log.error("Sql Execution Error!", sqle);
throw new DatabaseException("Error executing SQL in CompanyDAOImplJDBC.delete.");
} finally {
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
/*
* Included columns: CompanyName, CompanyAddress, CompanyCity, CompanyCAP, CompanyProvince,
* CompanyRegion, CompanyPhone, CompanyFax, CompanyWebsite, CompanyEmail,
* CompanySpaceName, CompanySpaceHeader, CompanySpaceFooter, CompanyVATNumber, CompanyModifiedDate
* Excluded columns: CompanyID, GroupID, CompanyLogo, CompanyCss, CompanyCreationDate
*/
public void updateCompanyInfo(int companyID, // primary key
String companyName, String companyAddress, String companyCity,
String companyCAP, String companyProvince, String companyRegion,
String companyPhone, String companyFax, String companyWebsite,
String companyEmail, String companySpaceName, String companySpaceHeader,
String companySpaceFooter, String companyVATNumber, Timestamp companyModifiedDate)
throws ObjectNotFoundException, DatabaseException, DuplicateKeyException {
CompanyBean bean = getCompany(companyID);
if ( !companyName.equals(bean.getCompanyName()) ) {
// Company tries to change its alternate key <CompanyName>, so we must check if it already exist
try {
findByAlternateKey_CompanyName(companyName);
throw new DuplicateKeyException("Alternate key [CompanyName] (" + companyName + ")already exists. Cannot update Company.");
} catch(ObjectNotFoundException e) {
//Otherwise we can go ahead
}
}
if ( !companyEmail.equals(bean.getCompanyEmail()) ) {
// Company tries to change its alternate key <CompanyEmail>, so we must check if it already exist
try {
findByAlternateKey_CompanyEmail(companyEmail);
throw new DuplicateKeyException("Alternate key [CompanyEmail] (" + companyEmail + ")already exists. Cannot update Company.");
} catch(ObjectNotFoundException e) {
//Otherwise we can go ahead
}
}
if ( !companySpaceName.equals(bean.getCompanySpaceName()) ) {
// Company tries to change its alternate key <CompanySpaceName>, so we must check if it already exist
try {
findByAlternateKey_CompanySpaceName(companySpaceName);
throw new DuplicateKeyException("Alternate key [CompanySpaceName] (" + companySpaceName + ")already exists. Cannot update Company.");
} catch(ObjectNotFoundException e) {
//Otherwise we can go ahead
}
}
Connection connection = null;
PreparedStatement statement = null;
StringBuffer sql = new StringBuffer(512);
sql.append("UPDATE " + TABLE_NAME + " SET CompanyName = ?, CompanyAddress = ?, CompanyCity = ?, CompanyCAP = ?, CompanyProvince = ?, CompanyRegion = ?, CompanyPhone = ?, CompanyFax = ?, CompanyWebsite = ?, CompanyEmail = ?, CompanySpaceName = ?, CompanySpaceHeader = ?, CompanySpaceFooter = ?, CompanyVATNumber = ?, CompanyModifiedDate = ?");
sql.append(" WHERE CompanyID = ?");
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
// // column(s) to update
statement.setString(1, companyName);
statement.setString(2, companyAddress);
statement.setString(3, companyCity);
statement.setString(4, companyCAP);
statement.setString(5, companyProvince);
statement.setString(6, companyRegion);
statement.setString(7, companyPhone);
statement.setString(8, companyFax);
statement.setString(9, companyWebsite);
statement.setString(10, companyEmail);
statement.setString(11, companySpaceName);
statement.setString(12, companySpaceHeader);
statement.setString(13, companySpaceFooter);
statement.setString(14, companyVATNumber);
statement.setTimestamp(15, companyModifiedDate);
// primary key column(s)
statement.setInt(16, companyID);
if (statement.executeUpdate() != 1) {
throw new ObjectNotFoundException("Cannot update table Company where primary key = (" + companyID + ").");
}
m_dirty = true;
} catch(SQLException sqle) {
log.error("Sql Execution Error!", sqle);
throw new DatabaseException("Error executing SQL in CompanyDAOImplJDBC.updateCompanyInfo.");
} finally {
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
public void updateCompanyLogo(int companyID, // primary key
String companyLogo)
throws ObjectNotFoundException, DatabaseException {
Connection connection = null;
PreparedStatement statement = null;
StringBuffer sql = new StringBuffer(512);
sql.append("UPDATE " + TABLE_NAME + " SET CompanyLogo = ?");
sql.append(" WHERE CompanyID = ?");
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
// // column(s) to update
statement.setString(1, companyLogo);
// primary key column(s)
statement.setInt(2, companyID);
if (statement.executeUpdate() != 1) {
throw new ObjectNotFoundException("Cannot update table Company where primary key = (" + companyID + ").");
}
m_dirty = true;
} catch(SQLException sqle) {
log.error("Sql Execution Error!", sqle);
throw new DatabaseException("Error executing SQL in CompanyDAOImplJDBC.updateCompanyLogo.");
} finally {
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
public void updateCompanyCss(int companyID, // primary key
String companyCss)
throws ObjectNotFoundException, DatabaseException {
Connection connection = null;
PreparedStatement statement = null;
StringBuffer sql = new StringBuffer(512);
sql.append("UPDATE " + TABLE_NAME + " SET CompanyCss = ?");
sql.append(" WHERE CompanyID = ?");
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
// // column(s) to update
statement.setString(1, companyCss);
// primary key column(s)
statement.setInt(2, companyID);
if (statement.executeUpdate() != 1) {
throw new ObjectNotFoundException("Cannot update table Company where primary key = (" + companyID + ").");
}
m_dirty = true;
} catch(SQLException sqle) {
log.error("Sql Execution Error!", sqle);
throw new DatabaseException("Error executing SQL in CompanyDAOImplJDBC.updateCompanyCss.");
} finally {
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
public int getCompanyIDFromGroupID(int groupID)
throws ObjectNotFoundException, DatabaseException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
StringBuffer sql = new StringBuffer(512);
sql.append("SELECT CompanyID");
sql.append(" FROM " + TABLE_NAME);
sql.append(" WHERE GroupID = ?");
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
statement.setInt(1, groupID);
resultSet = statement.executeQuery();
if(!resultSet.next()) {
throw new ObjectNotFoundException("Cannot find the row in table Company where alternate key [GroupID] = (" + groupID + ").");
}
return resultSet.getInt("CompanyID");
} catch(SQLException sqle) {
log.error("Sql Execution Error!", sqle);
throw new DatabaseException("Error executing SQL in CompanyDAOImplJDBC.getCompanyIDFromGroupID(ak).");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -