我有一个 ResultSet
对象
ResultSet theInfo = stmt.executeQuery(sqlQuery);
但是我想要一个可以从另一个java类调用的函数
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;
}
我不完全确定如何解决这个问题。我想了解如何调用返回填充 vector 的 void 方法。这可能吗?
最佳答案
假设您有一个类 Demo,并且它具有遵循给定方法的 getVector 方法。
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;
}
}
现在在获取ResultSet之后调用getVector。
ResultSet theInfo = stmt.executeQuery(sqlQuery);
Demo demo =new Demo();
Vector vector=demo.getVetor(theInfo );
关于java - 如何从外部java类获取ResultSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20974316/