📄 dbgroupiterator.java
字号:
/*
* DbGroupInterator.java
*
* Created on 2001年7月4日, 上午10:34
*/
package com.gs.db.dbimp;
import java.util.*;
//JDK1.1// import com.sun.java.util.collections.*;
import java.sql.*;
import com.gs.db.*;
/**
* Iterates through all the groups in the system.
*/
public class DbGroupIterator implements Iterator, ListIterator {
/** DATABASE QUERIES **/
private static final String ALL_GROUPS =
"SELECT groupid from gsGroup";
private static final String UNIT_GROUPS=
"SELECT groupid from gsGroup WHERE djjg=? ORDER BY priority ASC ";
private int currentIndex = -1;
private int [] groups;
private ProfileManager profileManager;
protected DbGroupIterator(ProfileManager profileManager) {
this.profileManager = profileManager;
//We don't know how many results will be returned, so store them
//in an ArrayList.
ArrayList tempGroups = new ArrayList();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ALL_GROUPS);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
tempGroups.add(new Integer(rs.getInt("groupid")));
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbGroupIterator:constructor()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
groups = new int[tempGroups.size()];
for (int i=0; i<groups.length; i++) {
groups[i] = ((Integer)tempGroups.get(i)).intValue();
}
}
protected DbGroupIterator(Unit assocUnit, ProfileManager profileManager) {
this.profileManager = profileManager;
//We don't know how many results will be returned, so store them
//in an ArrayList.
ArrayList tempGroups = new ArrayList();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UNIT_GROUPS);
pstmt.setString( 1, assocUnit.getID()+"");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
tempGroups.add(new Integer(rs.getInt("groupid")));
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbGroupIterator:constructor()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
groups = new int[tempGroups.size()];
for (int i=0; i<groups.length; i++) {
groups[i] = ((Integer)tempGroups.get(i)).intValue();
}
}
protected DbGroupIterator(ProfileManager profileManager, int startIndex,
int numResults)
{
this.profileManager = profileManager;
int[] tempResults = new int[numResults];
//It's very possible that there might not be as many results to get
//as we requested. Therefore, we keep track of how many results we
//get by keeping a count.
int resultCount = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ALL_GROUPS);
ResultSet rs = pstmt.executeQuery();
//Move to start of index
for (int i=0; i<startIndex; i++) {
rs.next();
}
//Now read in desired number of results
for (int i=0; i<numResults; i++) {
if (rs.next()) {
tempResults[resultCount] = rs.getInt("groupid");
resultCount++;
}
else {
break;
}
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbGroupIterator:constructor()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
groups = new int[resultCount];
for (int i=0; i<resultCount; i++) {
groups[i] = tempResults[i];
}
}
/**
* Returns true if there are more groups left to iteratate through.
*/
public boolean hasNext() {
return (currentIndex+1 < groups.length);
}
/**
* Returns the next Group.
*/
public Object next() throws java.util.NoSuchElementException {
Group group = null;
currentIndex++;
if (currentIndex >= groups.length) {
throw new java.util.NoSuchElementException();
}
try {
group = profileManager.getGroup(groups[currentIndex]);
}
catch (GroupNotFoundException gnfe) {
System.err.println(gnfe);
}
return group;
}
/**
* For security reasons, the remove operation is not supported. Use
* ProfileManager.deleteGroup() instead.
*
* @see ProfileManager
*/
public void remove() {
throw new UnsupportedOperationException();
}
/**
* Returns true if there are more groups left to iterate through backwards.
*/
public boolean hasPrevious() {
return (currentIndex > 0);
}
/**
* For security reasons, the add operation is not supported. Use
* ProfileManager instead.
*
* @see ProfileManager
*/
public void add(Object o) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
/**
* For security reasons, the set operation is not supported. Use
* ProfileManager instead.
*
* @see ProfileManager
*/
public void set(Object o) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
/**
* Returns the index number that would be returned with a call to next().
*/
public int nextIndex() {
return currentIndex+1;
}
/**
* Returns the previous group.
*/
public Object previous() throws java.util.NoSuchElementException {
Group group = null;
currentIndex--;
if (currentIndex < 0) {
currentIndex++;
throw new java.util.NoSuchElementException();
}
try {
group = profileManager.getGroup(groups[currentIndex]);
}
catch (GroupNotFoundException gnfe) {
System.err.println(gnfe);
}
return group;
}
/**
* Returns the index number that would be returned with a call to previous().
*/
public int previousIndex() {
return currentIndex-1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -