views:

61

answers:

4

Hi, I have the following code:

static ArrayList<PreparedStatement> alPrepStmts = new ArrayList<PreparedStatement>();

static PreparedStatement m_stmtOne;
static PreparedStatement m_stmtTwo;
static PreparedStatement m_stmtThree;
...
static PreparedStatement m_stmtOneHundred;

private static void init() {
    alPrepStmts.add(m_stmtOne);
    alPrepStmts.add(m_stmtTwo);
    alPrepStmts.add(m_stmtThree);
    ...
    alPrepStmts.add(m_stmtOneHundred);
}

private static void work() {
    if(m_stmtOne == null) {
        // assign m_stmtOne
    }
    // use m_stmtOne
}

private static void close() throws SQLException {
    for(PreparedStatement ps : alPrepStmts) {
        if(ps != null) {
            ps.close();
            ps = null;
        }
    }
}

init() gets called once. work() and close() may get called several times. The problem is that after calling close(), the PreparedStatements are not set to null. The next time I call work(), m_stmtOne is not null, but is closed. I supposed I can check if m_stmtOne is open, but I am wondering how can I assign null to the members of the container.

I also tried using iterators and it does not work either. The if conditional below is never true.

private static void ClosePreparedStatements() throws SQLException {
    for(Iterator<PreparedStatement> it = alPrepStmts.iterator(); it.hasNext();) {
        PreparedStatement ps = (PreparedStatement)it.next();
        if(ps != null) {
            ps.close();
            ps = null;
        }
    }
}

I know I can close and assign every statement manually, but I'm just wondering what is the best way to assign null to elements in a container.

A: 

One way would be:

for(int i = 0; i < alPrepStmts.size(); i++) {
    if(alPrepStmts.get(i) != null) {
        alPrepStmts.get(i).close()
        alPrepStmts.set(i, null);
    }
}

Incidentally, if you have a List<PreparedStatement>, you don't need to cast each entry to PreparedStatement. That's what Java Generics are for in the first place.

Christian Mann
A: 
java.util.Collections.fill(List<? super T> list, T obj);

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#fill%28java.util.List,%20T%29

Matti
+1  A: 

Your assignment ps = null assigns only the local variable, it does not modify the original container. You should use the remove method of java.util.Iterator (in your second code snipped instead of ps = null) to remove an element from the container. Note this removes the element from the container, not replace it with null in the container.

Even if you remove the element from the container, it will not change the value of, e.g. m_stmtOne, which it sounds like you want to have happen also. You will have to do that some other way.

It also seems init() just adds 100 nulls to your list of prepared statements. Assigning a non-null value to m_stmtOne later will not change the list. In other words, m_stmtOne gets added to the list by value, not by reference.

Keith Randall
A: 

The for statement is creating a locally scoped variable. You set that null, but that does not set the list element or class member variable reference null, as you've probably realized.

EDIT: The more I look at this code , the more I don't understand what it is meant to do.

kaliatech
I just want to set the class member variables to null by iterating through a list.
foamboarder
There is no way to do that without using reflection and that is not what you want. Understanding your issue will require a better understanding of variable references in Java. Your code as shown in the question doesn't make much sense. Typically, you would not hold explicit member variable references in addition to holding those same objects in a list. Is there a specific reason you want to have a member variable for each prepared statement? Why not just keep them in the list, and only in the list?
kaliatech
I commented on my original post above...
foamboarder