怎能将ResultSet结果集返回给一个数组?

请给个代码示例!!!
2024-12-22 16:03:42
推荐回答(3个)
回答1:

public String[] search(){
List list = new ArrayList();
String sql = "select name from user" ;
try {
conn = DBUtil.getConnection();//连接数据库的
ps = conn.prepareStatement(sql);

ResultSet rs= ps.executeQuery();
while(rs.next()){
list.add(rs.getString(1));
}

conn.commit();
} catch (SQLException e) {
e.printStackTrace();

return null ;
}catch(Exception e){
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
String user[] = (String[])list.toArray(new String[list.size()]);
return user;
}

回答2:

package com.declareol.trashbox;

import java.sql.ResultSet;
import java.util.Vector;
import com.declareol.commons.CommConnDataBase;

public class TrashBox {
public String[] strUser;

public void getTrashBox(String strName) {
CommConnDataBase CCDB = new CommConnDataBase();
ResultSet objRsInfo = null;
int intI = 0;
String strSelect = "select * from DOL_OFTEN_CASE";

List list=new ArrayList ();

try {
objRsInfo = CCDB.getResult(strSelect);
while (objRsInfo.next()) {
list.add(objRsInfo.getString(3));
intI++;
} catch (Exception e) {
System.out.print(e.getMessage());
} finally {
CCDB.Release();
}

}

public String[] putUser() {
return strUser;
}

}

回答3:

javax.servlet.jsp.jstl这个包里有个ResultSupport