📄 dbunititerator.java
字号:
/*
* DbUnitIterator.java
*
* Created on 2001年7月4日, 下午1:11
*/
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 units in the system. Those wishing to integrate
* Jive into their own Unit system should also modify this class.
*/
public class DbUnitIterator implements Iterator, ListIterator {
/** DATABASE QUERIES **/
private static final String SUB_UNITS =
"SELECT djjg from u_djjg WHERE sjjg=? order by djjg";
/** DATABASE QUERIES **/
private static final String INSIDE_SUB_UNITS =
"SELECT djjg from u_djjg WHERE sjjg=? and inside='1' order by djjg";
private static final String NONINSIDE_SUB_UNITS =
"SELECT djjg from u_djjg WHERE sjjg=? and inside='0' order by djjg";
private int currentIndex = -1;
private String [] units;
private Unit parentUnit;
private ProfileManager profileManager;
protected DbUnitIterator( Unit parentUnit,ProfileManager profileManager) {
this.profileManager = profileManager;
this.parentUnit = parentUnit;
//We don't know how many results will be returned, so store them
//in an ArrayList.
ArrayList tempUnits = new ArrayList();
Connection con = null;
PreparedStatement pstmt = null;
String s_query = SUB_UNITS;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(s_query);
pstmt.setString( 1, parentUnit.getID());
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
tempUnits.add(rs.getString("djjg"));
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbUnitIterator:constructor()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//Now copy into an array.
units = new String[tempUnits.size()];
for (int i=0; i<units.length; i++) {
units[i] = (String)tempUnits.get(i);
}
}
/**
*
* @param parentUnit Unit
* @param profileManager ProfileManager
* @param type int 0:all subunit;1 inside subunit;2:nonInsideUnit
*/
protected DbUnitIterator( Unit parentUnit,ProfileManager profileManager,int type) {
this.profileManager = profileManager;
this.parentUnit = parentUnit;
//We don't know how many results will be returned, so store them
//in an ArrayList.
ArrayList tempUnits = new ArrayList();
Connection con = null;
PreparedStatement pstmt = null;
String s_query = type==0?SUB_UNITS:(type==1?INSIDE_SUB_UNITS:NONINSIDE_SUB_UNITS);
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(s_query);
pstmt.setString( 1, parentUnit.getID());
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
tempUnits.add(rs.getString("djjg"));
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbUnitIterator:constructor()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//Now copy into an array.
units = new String[tempUnits.size()];
for (int i=0; i<units.length; i++) {
units[i] = (String)tempUnits.get(i);
}
}
protected DbUnitIterator( Unit parentUnit, ProfileManager profileManager,
int startIndex, int numResults)
{
this.profileManager = profileManager;
String[] tempUnits = new String[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 unitCount = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(SUB_UNITS);
pstmt.setString( 1, parentUnit.getID());
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()) {
tempUnits[unitCount] = rs.getString("djjg");
unitCount++;
}
else {
break;
}
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbUnitIterator:constructor()-" + sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
units = new String[unitCount];
for (int i=0; i<unitCount; i++) {
units[i] = tempUnits[i];
}
}
/**
* Returns true if there are more units left to iteratate through forwards.
*/
public boolean hasNext() {
return (currentIndex+1 < units.length);
}
/**
* Returns true if there are more units left to iteratore through backwards.
*/
public boolean hasPrevious() {
return (currentIndex > 0);
}
/**
* Returns the next unit.
*
* @return the next units.
* @throws NoSuchElementException if there are no more elements to return.
*/
public Object next() throws java.util.NoSuchElementException {
Unit unit = null;
currentIndex++;
if (currentIndex >= units.length) {
throw new java.util.NoSuchElementException();
}
try {
unit = profileManager.getUnit(units[currentIndex]);
}
catch (UnitNotFoundException gnfe) {
gnfe.printStackTrace();
}
catch ( Exception e )
{
System.err.println( e );
}
return unit;
}
/**
* 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 Unit.
*/
public Object previous() throws java.util.NoSuchElementException {
Unit Unit = null;
currentIndex--;
if (currentIndex < 0) {
currentIndex++;
throw new java.util.NoSuchElementException();
}
try {
Unit = profileManager.getUnit(units[currentIndex]);
}
catch (UnitNotFoundException gnfe) {
System.err.println(gnfe);
}
return Unit;
}
/**
* 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 + -