📄 dbuseriterator.java
字号:
/*
* DbUserIterator.java
*
* Created on 2001年7月4日, 上午10:32
*/
package com.gs.db.dbimp;
import com.gs.db.*;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.ArrayList;
import java.sql.*;
/**
* An Iterator for all the users in the system. Those wishing to integrate
* Jive into their own User system should also modify this class.
*/
public class DbUserIterator implements Iterator, ListIterator {
/** DATABASE QUERIES **/
private static final String ALL_USERS =
"SELECT userid from gsUser where status>=0";
private int currentIndex = -1;
private int [] users;
private ProfileManager profileManager;
protected DbUserIterator(ProfileManager profileManager) {
this.profileManager = profileManager;
//We don't know how many results will be returned, so store them
//in an ArrayList.
ArrayList tempUsers = new ArrayList();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ALL_USERS);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
tempUsers.add(new Integer(rs.getInt("userid")));
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbUserIterator:constructor()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//Now copy into an array.
users = new int[tempUsers.size()];
for (int i=0; i<users.length; i++) {
users[i] = ((Integer)tempUsers.get(i)).intValue();
}
}
protected DbUserIterator(ProfileManager profileManager, int startIndex,
int numResults)
{
this.profileManager = profileManager;
int[] tempUsers = 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 userCount = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ALL_USERS);
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()) {
tempUsers[userCount] = rs.getInt("userid");
userCount++;
}
else {
break;
}
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbUserIterator:constructor()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
users = new int[userCount];
for (int i=0; i<userCount; i++) {
users[i] = tempUsers[i];
}
}
/**
* Returns true if there are more users left to iteratate through forwards.
*/
public boolean hasNext() {
return (currentIndex+1 < users.length);
}
/**
* Returns true if there are more users left to iteratore through backwards.
*/
public boolean hasPrevious() {
return (currentIndex > 0);
}
/**
* Returns the next User.
*
* @return the next User.
* @throws NoSuchElementException if there are no more elements to return.
*/
public Object next() throws java.util.NoSuchElementException {
User user = null;
currentIndex++;
if (currentIndex >= users.length) {
throw new java.util.NoSuchElementException();
}
try {
user = profileManager.getUser(users[currentIndex]);
}
catch (UserNotFoundException gnfe) {
gnfe.printStackTrace();
}
return user;
}
/**
* 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 remove operation is not supported. Use
* ProfileManager instead.
*
* @see ProfileManager
*/
public void remove() {
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 user.
*/
public Object previous() throws java.util.NoSuchElementException {
User user = null;
currentIndex--;
if (currentIndex < 0) {
currentIndex++;
throw new java.util.NoSuchElementException();
}
try {
user = profileManager.getUser(users[currentIndex]);
}
catch (UserNotFoundException gnfe) {
System.err.println(gnfe);
}
return user;
}
/**
* 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 + -