简体   繁体   中英

how to get a ResultSet from an outside java class

I have a ResultSet object

ResultSet theInfo = stmt.executeQuery(sqlQuery);

but I want to have a function that can be called from another java class

public Vector loadStuff(){
    try {
         while (theInfo.next()){
             aVector.addElement(new String(theInfo.getString("aColumn")));    // puts results into vectors
         }

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return aVector;
}

I'm not entirely sure how to go about this. I want to some how call a void method that returns a populated vector. is this possible?

Suppose you have a class Demo and it has method getVector follow the given approach.

class Demo {

public Vector getVector(ResultSet theInfo) {
    if(theInfo==null){
        throw new IllegalArgumentException("ResultSet is null");
    }
    Vector aVector = new Vector();
    try {
        while (theInfo.next()) {
            aVector.addElement(new String(theInfo.getString("aColumn"))); 
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return aVector;
}

}

Now call the getVector after the getting the ResultSet.

ResultSet theInfo = stmt.executeQuery(sqlQuery);

Demo demo =new Demo();

Vector vector=demo.getVetor(theInfo );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM