📄 threaddaoimpljdbc.java
字号:
* ThreadBody, ThreadVoteCount, ThreadVoteTotalStars, ThreadCreationDate, ThreadLastPostDate,
* ThreadType, ThreadOption, ThreadStatus, ThreadHasPoll, ThreadViewCount,
* ThreadReplyCount, ThreadIcon, ThreadDuration
* Excluded columns:
*/
private Collection getBeans_withSortSupport_limit_noscroll(int offset, int rowsToReturn, String sort, String order, boolean enable)
throws IllegalArgumentException, DatabaseException {
if (offset < 0) throw new IllegalArgumentException("The offset < 0 is not allowed.");
if (rowsToReturn <= 0) throw new IllegalArgumentException("The rowsToReturn <= 0 is not allowed.");
if ((!sort.equals("ThreadLastPostDate")) &&
(!sort.equals("ThreadCreationDate")) &&
(!sort.equals("MemberName")) &&
(!sort.equals("ThreadReplyCount")) &&
(!sort.equals("ForumID")) &&
(!sort.equals("ThreadViewCount")) ) {
throw new IllegalArgumentException("Cannot sort, reason: dont understand the criteria '" + sort + "'.");
}
if ((!order.equals("ASC")) &&
(!order.equals("DESC")) ) {
throw new IllegalArgumentException("Cannot sort, reason: dont understand the order '" + order + "'.");
}
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
Collection retValue = new ArrayList();
StringBuffer sql = new StringBuffer(512);
sql.append("SELECT ThreadID, ForumID, MemberName, LastPostMemberName, ThreadTopic, ThreadBody, ThreadVoteCount, ThreadVoteTotalStars, ThreadCreationDate, ThreadLastPostDate, ThreadType, ThreadOption, ThreadStatus, ThreadHasPoll, ThreadViewCount, ThreadReplyCount, ThreadIcon, ThreadDuration");
sql.append(" FROM " + TABLE_NAME);
if (enable) {
sql.append(" WHERE ThreadStatus <> 1 ");
} else {// disable
sql.append(" WHERE ThreadStatus = 1 ");
}
sql.append(" ORDER BY " + sort + " " + order);// ColumnName, ASC|DESC
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
statement.setMaxRows(offset + rowsToReturn);
resultSet = statement.executeQuery();
int rowIndex = -1;
while (resultSet.next()) {
rowIndex++;
if (rowIndex < offset) continue;
ThreadBean bean = new ThreadBean();
bean.setThreadID(resultSet.getInt("ThreadID"));
bean.setForumID(resultSet.getInt("ForumID"));
bean.setMemberName(resultSet.getString("MemberName"));
bean.setLastPostMemberName(resultSet.getString("LastPostMemberName"));
bean.setThreadTopic(resultSet.getString("ThreadTopic"));
bean.setThreadBody(resultSet.getString("ThreadBody"));
bean.setThreadVoteCount(resultSet.getInt("ThreadVoteCount"));
bean.setThreadVoteTotalStars(resultSet.getInt("ThreadVoteTotalStars"));
bean.setThreadCreationDate(resultSet.getTimestamp("ThreadCreationDate"));
bean.setThreadLastPostDate(resultSet.getTimestamp("ThreadLastPostDate"));
bean.setThreadType(resultSet.getInt("ThreadType"));
bean.setThreadOption(resultSet.getInt("ThreadOption"));
bean.setThreadStatus(resultSet.getInt("ThreadStatus"));
bean.setThreadHasPoll(resultSet.getInt("ThreadHasPoll"));
bean.setThreadViewCount(resultSet.getInt("ThreadViewCount"));
bean.setThreadReplyCount(resultSet.getInt("ThreadReplyCount"));
bean.setThreadIcon(resultSet.getString("ThreadIcon"));
bean.setThreadDuration(resultSet.getInt("ThreadDuration"));
retValue.add(bean);
}
return retValue;
} catch(SQLException sqle) {
log.error("Sql Execution Error!", sqle);
throw new DatabaseException("Error executing SQL in ThreadDAOImplJDBC.getBeans_withSortSupport_limit_noscroll.");
} finally {
DBUtils.closeResultSet(resultSet);
DBUtils.resetStatement(statement);
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
/*
* Included columns: ThreadID, ForumID, MemberName, LastPostMemberName, ThreadTopic,
* ThreadBody, ThreadVoteCount, ThreadVoteTotalStars, ThreadCreationDate, ThreadLastPostDate,
* ThreadType, ThreadOption, ThreadStatus, ThreadHasPoll, ThreadViewCount,
* ThreadReplyCount, ThreadIcon, ThreadDuration
* Excluded columns:
*/
private Collection getBeans_withSortSupport_limit_general(int offset, int rowsToReturn, String sort, String order, boolean enable)
throws IllegalArgumentException, DatabaseException {
if (offset < 0) throw new IllegalArgumentException("The offset < 0 is not allowed.");
if (rowsToReturn <= 0) throw new IllegalArgumentException("The rowsToReturn <= 0 is not allowed.");
if ((!sort.equals("ThreadLastPostDate")) &&
(!sort.equals("ThreadCreationDate")) &&
(!sort.equals("MemberName")) &&
(!sort.equals("ThreadReplyCount")) &&
(!sort.equals("ForumID")) &&
(!sort.equals("ThreadViewCount")) ) {
throw new IllegalArgumentException("Cannot sort, reason: dont understand the criteria '" + sort + "'.");
}
if ((!order.equals("ASC")) &&
(!order.equals("DESC")) ) {
throw new IllegalArgumentException("Cannot sort, reason: dont understand the order '" + order + "'.");
}
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
Collection retValue = new ArrayList();
StringBuffer sql = new StringBuffer(512);
sql.append("SELECT ThreadID, ForumID, MemberName, LastPostMemberName, ThreadTopic, ThreadBody, ThreadVoteCount, ThreadVoteTotalStars, ThreadCreationDate, ThreadLastPostDate, ThreadType, ThreadOption, ThreadStatus, ThreadHasPoll, ThreadViewCount, ThreadReplyCount, ThreadIcon, ThreadDuration");
sql.append(" FROM " + TABLE_NAME);
if (enable) {
sql.append(" WHERE ThreadStatus <> 1 ");
} else {// disable
sql.append(" WHERE ThreadStatus = 1 ");
}
sql.append(" ORDER BY " + sort + " " + order);// ColumnName, ASC|DESC
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
statement.setMaxRows(offset + rowsToReturn);
try {
statement.setFetchSize(rowsToReturn);
} catch (SQLException sqle) {
//do nothing, postgreSQL doesnt support this method
}
resultSet = statement.executeQuery();
boolean loop = resultSet.absolute(offset + 1);// the absolute method begin with 1 instead of 0 as in the LIMIT clause
while (loop) {
ThreadBean bean = new ThreadBean();
bean.setThreadID(resultSet.getInt("ThreadID"));
bean.setForumID(resultSet.getInt("ForumID"));
bean.setMemberName(resultSet.getString("MemberName"));
bean.setLastPostMemberName(resultSet.getString("LastPostMemberName"));
bean.setThreadTopic(resultSet.getString("ThreadTopic"));
bean.setThreadBody(resultSet.getString("ThreadBody"));
bean.setThreadVoteCount(resultSet.getInt("ThreadVoteCount"));
bean.setThreadVoteTotalStars(resultSet.getInt("ThreadVoteTotalStars"));
bean.setThreadCreationDate(resultSet.getTimestamp("ThreadCreationDate"));
bean.setThreadLastPostDate(resultSet.getTimestamp("ThreadLastPostDate"));
bean.setThreadType(resultSet.getInt("ThreadType"));
bean.setThreadOption(resultSet.getInt("ThreadOption"));
bean.setThreadStatus(resultSet.getInt("ThreadStatus"));
bean.setThreadHasPoll(resultSet.getInt("ThreadHasPoll"));
bean.setThreadViewCount(resultSet.getInt("ThreadViewCount"));
bean.setThreadReplyCount(resultSet.getInt("ThreadReplyCount"));
bean.setThreadIcon(resultSet.getString("ThreadIcon"));
bean.setThreadDuration(resultSet.getInt("ThreadDuration"));
retValue.add(bean);
if (retValue.size() == rowsToReturn) break;// Fix the Sybase bug
loop = resultSet.next();
}//while
return retValue;
} catch(SQLException sqle) {
log.error("Sql Execution Error!", sqle);
throw new DatabaseException("Error executing SQL in ThreadDAOImplJDBC.getBeans_withSortSupport_limit_general.");
} finally {
DBUtils.closeResultSet(resultSet);
DBUtils.resetStatement(statement);
DBUtils.closeStatement(statement);
DBUtils.closeConnection(connection);
}
}
public Collection getEnableBeans_inForum_withSortSupport_limit(int forumID, int offset, int rowsToReturn, String sort, String order)
throws IllegalArgumentException, DatabaseException {
if (DBUtils.getDatabaseType() == DBUtils.DATABASE_MYSQL) {
return getBeans_inForum_withSortSupport_limit_mysql(forumID, offset, rowsToReturn, sort, order, true);
} else if (DBUtils.getDatabaseType() == DBUtils.DATABASE_NOSCROLL) {
return getBeans_inForum_withSortSupport_limit_noscroll(forumID, offset, rowsToReturn, sort, order, true);
}
return getBeans_inForum_withSortSupport_limit_general(forumID, offset, rowsToReturn, sort, order, true);
}
public Collection getDisableBeans_inForum_withSortSupport_limit(int forumID, int offset, int rowsToReturn, String sort, String order)
throws IllegalArgumentException, DatabaseException {
if (DBUtils.getDatabaseType() == DBUtils.DATABASE_MYSQL) {
return getBeans_inForum_withSortSupport_limit_mysql(forumID, offset, rowsToReturn, sort, order, false);
} else if (DBUtils.getDatabaseType() == DBUtils.DATABASE_NOSCROLL) {
return getBeans_inForum_withSortSupport_limit_noscroll(forumID, offset, rowsToReturn, sort, order, false);
}
return getBeans_inForum_withSortSupport_limit_general(forumID, offset, rowsToReturn, sort, order, false);
}
/*
* Included columns: ThreadID, MemberName, LastPostMemberName, ThreadTopic, ThreadBody,
* ThreadVoteCount, ThreadVoteTotalStars, ThreadCreationDate, ThreadLastPostDate, ThreadType,
* ThreadOption, ThreadStatus, ThreadHasPoll, ThreadViewCount, ThreadReplyCount,
* ThreadIcon, ThreadDuration
* Excluded columns: ForumID
*/
/**
* Note: This is a customized method
*/
private Collection getBeans_inForum_withSortSupport_limit_mysql(int forumID, int offset, int rowsToReturn, String sort, String order, boolean enable)
throws IllegalArgumentException, DatabaseException {
if (offset < 0) throw new IllegalArgumentException("The offset < 0 is not allowed.");
if (rowsToReturn <= 0) throw new IllegalArgumentException("The rowsToReturn <= 0 is not allowed.");
if ((!sort.equals("ThreadLastPostDate")) &&
(!sort.equals("ThreadCreationDate")) &&
(!sort.equals("MemberName")) &&
(!sort.equals("ThreadReplyCount")) &&
(!sort.equals("ThreadViewCount")) ) {
throw new IllegalArgumentException("Cannot sort, reason: dont understand the criteria '" + sort + "'.");
}
if ((!order.equals("ASC")) &&
(!order.equals("DESC")) ) {
throw new IllegalArgumentException("Cannot sort, reason: dont understand the order '" + order + "'.");
}
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
Collection retValue = new ArrayList();
StringBuffer sql = new StringBuffer(512);
sql.append("SELECT ThreadID, MemberName, LastPostMemberName, ThreadTopic, ThreadBody, ThreadVoteCount, ThreadVoteTotalStars, ThreadCreationDate, ThreadLastPostDate, ThreadType, ThreadOption, ThreadStatus, ThreadHasPoll, ThreadViewCount, ThreadReplyCount, ThreadIcon, ThreadDuration");
sql.append(" FROM " + TABLE_NAME);
sql.append(" WHERE ForumID = ?");
if (enable) {
sql.append(" AND ThreadStatus <> 1 ");
} else {//disable
sql.append(" AND ThreadStatus = 1 ");
}
sql.append(" ORDER BY " + sort + " " + order);// ColumnName, ASC|DESC
sql.append(" LIMIT ?, ?");
try {
connection = DBUtils.getConnection();
statement = connection.prepareStatement(sql.toString());
statement.setInt(1, forumID);
statement.setInt(2, offset);
statement.setInt(3, rowsToReturn);
resultSet = statement.executeQuery();
while (resultSet.next()) {
ThreadBean bean = new ThreadBean();
bean.setThreadID(resultSet.getInt("ThreadID"));
bean.setForumID(forumID);
bean.setMemberName(resultSet.getString("MemberName"));
bean.setLastPostMemberName(resultSet.getString("LastPostMemberName"));
bean.setThreadTopic(resultSet.getString("ThreadTopic"));
bean.setThreadBody(resultSet.getString("ThreadBody"));
bean.setThreadVoteCount(resultSet.getInt("ThreadVoteCount"));
bean.setThreadVoteTotalStars(resultSet.getInt("ThreadVoteTotalStars"));
bean.setThreadCreationDate(resultSet.getTimestamp("ThreadCreationDate"));
bean.setThreadLastPostDate(resultSet.getTimestamp("ThreadLastPostDate"));
bean.setThreadType(resultSet.getInt("ThreadType"));
bean.setThreadOption(resultSet.getInt("ThreadOption"));
bean.setThreadStatus(resultSet.getInt("ThreadStatus"));
bean.setThreadHasPoll(resultSet.getInt("ThreadHasPoll"));
bean.setThreadViewCount(resultSet.getInt("ThreadViewCount"));
bean.setThreadReplyCount(resultSet.getInt("ThreadReplyCount"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -