📄 groupswebhelper.java
字号:
// primary key column(s)
statement.setInt(4, groupID);
if (statement.executeUpdate() != 1) {
throw new BadInputException("Cannot update table Groups where primary key = (" + groupID + ").");
}
m_dirty = true;
} catch(SQLException sqle) {
sqle.printStackTrace();
throw new DatabaseException("Error executing SQL in GroupsWebHelper.update.");
} finally {
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
/*
// @todo: copy this method for derived class
public static void updateGroupOwner(int groupID, // primary key
String groupOwnerName, Timestamp groupModifiedDate)
throws BadInputException, DatabaseException, ForeignKeyNotFoundException {
net.myvietnam.webplugin.mvnforum.db.GroupsWebHelper.updateOwner(groupID, // primary key
groupOwnerName, groupModifiedDate);
}
*/
/*
* Included columns: GroupOwnerID, GroupOwnerName, GroupModifiedDate
* Excluded columns: GroupID, GroupName, GroupDesc, GroupOption, GroupCreationDate
*/
protected static void updateOwner(int groupID, // primary key
String groupOwnerName, Timestamp groupModifiedDate)
throws BadInputException, DatabaseException, ForeignKeyNotFoundException {
int groupOwnerID = 0;//MUST init to 0
GroupsBean bean = getBean(groupID); // @todo: comment or delete this line if no alternate key are included
try {
// @todo: modify the parameter list as needed
// If this method does not change the foreign key columns, you can comment this block of code.
groupOwnerID = 0;
if (groupOwnerName.length() > 0) {// have group owner
MemberWebHelper.findByAlternateKey_MemberName(groupOwnerName);
try {
groupOwnerID = MemberWebHelper.getMemberIDFromMemberName(groupOwnerName);
} catch (BadInputException ex) {
// This exception should never be thrown
throw new ObjectNotFoundException("ASSERTION: This should never happen.");
}
}
} catch(ObjectNotFoundException e) {
throw new ForeignKeyNotFoundException("Foreign key refers to table 'Member' does not exist. Cannot update table 'Groups'.");
}
Connection connection = null;
PreparedStatement statement = null;
StringBuffer sql = new StringBuffer(512);
sql.append("UPDATE " + TABLE_NAME + " SET GroupOwnerID = ?, GroupOwnerName = ?, GroupModifiedDate = ?");
sql.append(" WHERE GroupID = ?");
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
// // column(s) to update
statement.setInt(1, groupOwnerID);
statement.setString(2, groupOwnerName);
statement.setTimestamp(3, groupModifiedDate);
// primary key column(s)
statement.setInt(4, groupID);
if (statement.executeUpdate() != 1) {
throw new BadInputException("Cannot update table Groups where primary key = (" + groupID + ").");
}
m_dirty = true;
} catch(SQLException sqle) {
sqle.printStackTrace();
throw new DatabaseException("Error executing SQL in GroupsWebHelper.update.");
} finally {
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
/*
// @todo: copy this method for derived class
public static GroupsBean getGroup(int groupID)
throws BadInputException, DatabaseException {
return net.myvietnam.webplugin.mvnforum.db.GroupsWebHelper.getBean(groupID);
}
*/
/*
* Included columns: GroupOwnerID, GroupOwnerName, GroupName, GroupDesc, GroupOption,
* GroupCreationDate, GroupModifiedDate
* Excluded columns: GroupID
*/
protected static GroupsBean getBean(int groupID)
throws BadInputException, DatabaseException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
StringBuffer sql = new StringBuffer(512);
sql.append("SELECT GroupOwnerID, GroupOwnerName, GroupName, GroupDesc, GroupOption, GroupCreationDate, GroupModifiedDate");
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 BadInputException("Cannot find the row in table Groups where primary key = (" + groupID + ").");
}
GroupsBean bean = new GroupsBean();
bean.setGroupID(groupID);
bean.setGroupOwnerID(resultSet.getInt("GroupOwnerID"));
bean.setGroupOwnerName(resultSet.getString("GroupOwnerName"));
bean.setGroupName(resultSet.getString("GroupName"));
bean.setGroupDesc(resultSet.getString("GroupDesc"));
bean.setGroupOption(resultSet.getInt("GroupOption"));
bean.setGroupCreationDate(resultSet.getTimestamp("GroupCreationDate"));
bean.setGroupModifiedDate(resultSet.getTimestamp("GroupModifiedDate"));
return bean;
} catch(SQLException sqle) {
sqle.printStackTrace();
throw new DatabaseException("Error executing SQL in GroupsWebHelper.getBean(pk).");
} finally {
DBUtils.closeResultSet(resultSet);
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
/*
// @todo: copy this method for derived class
public static Collection getGroups()
throws DatabaseException {
return net.myvietnam.webplugin.mvnforum.db.GroupsWebHelper.getBeans();
}
*/
/*
* Included columns: GroupID, GroupOwnerID, GroupOwnerName, GroupName, GroupDesc,
* GroupOption, GroupCreationDate, GroupModifiedDate
* Excluded columns:
*/
protected static Collection getBeans()
throws DatabaseException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
Collection retValue = new ArrayList();
StringBuffer sql = new StringBuffer(512);
sql.append("SELECT GroupID, GroupOwnerID, GroupOwnerName, GroupName, GroupDesc, GroupOption, GroupCreationDate, GroupModifiedDate");
sql.append(" FROM " + TABLE_NAME);
//sql.append(" WHERE "); // @todo: uncomment as needed
//sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
resultSet = statement.executeQuery();
while (resultSet.next()) {
GroupsBean bean = new GroupsBean();
bean.setGroupID(resultSet.getInt("GroupID"));
bean.setGroupOwnerID(resultSet.getInt("GroupOwnerID"));
bean.setGroupOwnerName(resultSet.getString("GroupOwnerName"));
bean.setGroupName(resultSet.getString("GroupName"));
bean.setGroupDesc(resultSet.getString("GroupDesc"));
bean.setGroupOption(resultSet.getInt("GroupOption"));
bean.setGroupCreationDate(resultSet.getTimestamp("GroupCreationDate"));
bean.setGroupModifiedDate(resultSet.getTimestamp("GroupModifiedDate"));
retValue.add(bean);
}
return retValue;
} catch(SQLException sqle) {
sqle.printStackTrace();
throw new DatabaseException("Error executing SQL in GroupsWebHelper.getBeans.");
} finally {
DBUtils.closeResultSet(resultSet);
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
/*
// @todo: copy this method for derived class
public static int getNumberOfGroups()
throws AssertionException, DatabaseException {
return net.myvietnam.webplugin.mvnforum.db.GroupsWebHelper.getNumberOfBeans();
}
*/
protected static int getNumberOfBeans()
throws AssertionException, DatabaseException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
StringBuffer sql = new StringBuffer(512);
sql.append("SELECT Count(*)");
sql.append(" FROM " + TABLE_NAME);
//sql.append(" WHERE "); // @todo: uncomment as needed
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
resultSet = statement.executeQuery();
if (!resultSet.next()) {
throw new AssertionException("Assertion in GroupsWebHelper.getNumberOfBeans.");
}
return resultSet.getInt(1);
} catch(SQLException sqle) {
sqle.printStackTrace();
throw new DatabaseException("Error executing SQL in GroupsWebHelper.getNumberOfBeans.");
} finally {
DBUtils.closeResultSet(resultSet);
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
/************************************************
* Customized methods come below
************************************************/
/** @todo check if this method work with other DBMS other than MySql (check case-sensitive) */
public static final int getGroupIDFromGroupName(String groupName)
throws BadInputException, DatabaseException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
String sql = "SELECT GroupID FROM " + TABLE_NAME + " WHERE GroupName = ?";
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql);
statement.setString(1, groupName);
resultSet = statement.executeQuery();
if(!resultSet.next()) {
throw new BadInputException("Cannot find the row in table Group where GroupName = " + groupName);
}
return resultSet.getInt(1);
} catch(SQLException sqle) {
sqle.printStackTrace();
throw new DatabaseException("There is an error when trying to get a row in 'Group' table.");
} finally {
DBUtils.closeResultSet(resultSet);
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
}// end of class GroupsWebHelper
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -