Java 如何优化从resultset返回的大量数据
我的程序是获取考试结果,并将其转换为excel。它包含了很多参加考试的人。我的问题是,当我的数据太大时,我无法将其转换为excel,如果我的数据低于70,它工作正常。70以上,只需在网页中打印,不转换为excel。我遇到了“连接太多”这样的错误。如何解决这个问题?提前谢谢。下面是我的代码:Java 如何优化从resultset返回的大量数据,java,sql,database,Java,Sql,Database,我的程序是获取考试结果,并将其转换为excel。它包含了很多参加考试的人。我的问题是,当我的数据太大时,我无法将其转换为excel,如果我的数据低于70,它工作正常。70以上,只需在网页中打印,不转换为excel。我遇到了“连接太多”这样的错误。如何解决这个问题?提前谢谢。下面是我的代码: String sql = "SELECT * FROM surveydata WHERE survey_wfverid=? LIMIT 100";// AND ID='c6500937-3a9e-42f2-b
String sql = "SELECT * FROM surveydata WHERE survey_wfverid=? LIMIT 100";// AND ID='c6500937-3a9e-42f2-b01a-c9174f9e2304'";
try{
pstatement = db.getDBConnection().prepareStatement(sql);
pstatement.setString(1, project_id);
rs = pstatement.executeQuery();
int proj_id = Integer.parseInt(project_id);
int wfstnodeid = 0;
if (proj_id==10869){
System.out.println("project_id 2 : " +proj_id);
strRecord = "\"ID\",\"Start\",\"End\",\"Department\",\"Topic\",\"Division\",\"Name\",\"StaffCode\",\"Designation\",\"TotalScore\",\"Answer\",,,,,,,,,,,,,,,,,,,,\n";
}
else if (proj_id==10293){
System.out.println("project_id 3 : " +proj_id);
strRecord = "\"ID\",\"Start\",\"End\",\"Department\",\"Topic\",\"Name\",\"StaffCode\",\"Designation\",\"TrainerName\",\"TotalScore\",\"Answer\",,,,,,,,,,,,,,,,,,,,\n";
}
pstatement2 = db.getDBConnection().prepareStatement("SELECT wfstnodeid FROM wfstation WHERE wfstdesc=? AND wfverid=?");
pstatement2.setString(1, "Start");
pstatement2.setString(2, project_id);
rs2 = pstatement2.executeQuery();
wfstnodeid = 0;
while(rs2.next()){
wfstnodeid = rs2.getInt("wfstnodeid");
System.out.println("wfstnodeid "+wfstnodeid);
}
rs2.close();
pstatement2.close();
while(rs.next()){
String department = null;
String topic = null;
String division = null;
int wflinktoid = 0;
int wfformid = 0;
int score = 0;
ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(rs.getBytes("DATA"));
ObjectInputStream localObjectInputStream = new ObjectInputStream(localByteArrayInputStream);
Map<String, String[]> map = (Map<String, String[]>)localObjectInputStream.readObject();
strRecord += "\"" + rs.getString("ID") + "\",";
strRecord += "\"" + rs.getTimestamp("START").toString() + "\",";
strRecord += "\"" + rs.getTimestamp("END").toString() + "\",";
department = map.get("department")[0];
topic = map.get("topic")[0];
strRecord += "\"" + department + "\",";
strRecord += "\"" + topic + "\",";
if(proj_id==10869){
if (map.get("division")==null){
division = "old samples";
}
else{
division = map.get("division")[0];
}
strRecord += "\"" + division + "\",";
strRecord += "\"" + map.get("11163")[0] + "\","; //name
strRecord += "\"" + map.get("11163")[1] + "\","; //staff code
strRecord += "\"" + map.get("11163")[2] + "\","; //designation
division = null;
}
else if(proj_id==10293){
strRecord += "\"" + map.get("10322")[0] + "\","; //name
strRecord += "\"" + map.get("10322")[1] + "\",";
strRecord += "\"" + map.get("10322")[2] + "\",";
strRecord += "\"" + map.get("10322")[3] + "\",";
}
//get routing topic -- start from here get topic id from wflinktoid
pstatement2 = db.getDBConnection().prepareStatement("SELECT wflinktoid FROM WFLINK WHERE WFLINKFROMID=? AND wfrule=?");
pstatement2.setInt(1, wfstnodeid);//station id
pstatement2.setString(2, "department == '" + department + "' && topic == '" + topic + "'");
rs2 = pstatement2.executeQuery();
wflinktoid = 0;
while(rs2.next()){
wflinktoid = rs2.getInt("wflinktoid");
}
rs2.close();
pstatement2.close();
db.closeDBConnection();
department = null;
topic = null;
pstatement2 = db.getDBConnection().prepareStatement("SELECT wfformid FROM wfsubsform where wfstationid=?");
pstatement2.setInt(1, wflinktoid);
rs2 = pstatement2.executeQuery();
while(rs2.next()){
wfformid = rs2.getInt("wfformid");
}
rs2.close();
pstatement2.close();
db.closeDBConnection();
pstatement2 = db.getDBConnection().prepareStatement("select q.* from survey_questions q, nodelist n where q.fieldid = n.nlnodeid and n.nlidatch=? and (q.exttype='checkbox' or q.exttype='radio') and q.answer is not null order by q.seq");
pstatement2.setInt(1, wfformid);
rs2 = pstatement2.executeQuery();
score = 0;
while(rs2.next()){
if(map.get(rs2.getString("fieldid"))==null){
strRecord += "\"" + map.get(rs2.getString("fieldid")) + "\",";
if(rs2.getString("ANSWER").trim().equals(map.get(rs2.getString("fieldid")))){
score += Integer.parseInt(rs2.getString("SCORE"));//get score
}
}
else{
String map_answer = "";
int y = 0;
while (y < map.get(rs2.getString("fieldid")).length){
map_answer += map.get(rs2.getString("fieldid"))[y] + ",";
y++;
}
map_answer = map_answer.substring(0, map_answer.length() - 1);
//compare answer
if(rs2.getString("ANSWER").trim().equals(map_answer)){
score += Integer.parseInt(rs2.getString("SCORE"));//get score
}
}
}
rs2.close();
pstatement2.close();
db.closeDBConnection();
strRecord += "\"" + score + "\",";
pstatement2 = db.getDBConnection().prepareStatement("select q.* from survey_questions q, nodelist n where q.fieldid = n.nlnodeid and n.nlidatch=? and (q.exttype='checkbox' or q.exttype='radio') and q.answer is not null order by q.seq");
pstatement2.setInt(1, wfformid);
rs2 = pstatement2.executeQuery();
wfformid = 0;
score = 0;
while(rs2.next()){
if(map.get(rs2.getString("fieldid"))==null){
strRecord += "\"" + map.get(rs2.getString("fieldid")) + "\",";
if(rs2.getString("ANSWER").trim().equals(map.get(rs2.getString("fieldid")))){
score += Integer.parseInt(rs2.getString("SCORE"));//get score
}
}
else{
String map_answer = "";
int y = 0;
while (y < map.get(rs2.getString("fieldid")).length){
map_answer += map.get(rs2.getString("fieldid"))[y] + ",";
y++;
}
map_answer = map_answer.substring(0, map_answer.length() - 1);
strRecord += "\"" + map_answer + "\","; //print out user answer
}
}
strRecord = strRecord.substring(0, strRecord.length() - 1);
strRecord += "\n";
rs2.close();
pstatement2.close();
db.closeDBConnection();
}//rs
output = strRecord;
rs.close();
pstatement.close();
db.closeDBConnection();
String sql=“从surveydata中选择*,其中survey\u wfverid=?限制100”//ID='c6500937-3a9e-42f2-b01a-c9174f9e2304';
试一试{
pstatement=db.getDBConnection().prepareStatement(sql);
pstatement.setString(1,项目id);
rs=pstatement.executeQuery();
int proj_id=Integer.parseInt(项目id);
int wfstnodeid=0;
如果(项目id==10869){
System.out.println(“项目id 2:+项目id”);
strRecord=“\'ID\”、\'Start\”、\'End\”、\'DEPARTY\、\'Topic\、\'DEVISITION\”、\'Name\、\'StaffCode\”、\'DESTATION\”、\'TOTALLSCORE\、\'ANSWERK\”、,,,,,,,,,,,,,,,,,,,,\n;
}
否则如果(项目id==10293){
System.out.println(“项目id 3:+项目id”);
strRecord=“\'ID\”、\'Start\”、\'End\”、\'Department\、\'Topic\、\'Name\、\'StaffCode\”、\'DESIGNTION\、\'TrainerName\、\'TOTALLSCORE\”、\'ANSWORT\,,,,,,,,,,,,,,,,,\n;
}
pstatement2=db.getDBConnection().prepareStatement(“从wfstation中选择wfstnodeid,其中wfstdesc=?和wfverid=?”;
pstatement2.设置字符串(1,“开始”);
pstatement2.设置字符串(2,项目id);
rs2=pstatement2.executeQuery();
wfstnodeid=0;
while(rs2.next()){
wfstnodeid=rs2.getInt(“wfstnodeid”);
System.out.println(“wfstnodeid”+wfstnodeid);
}
rs2.close();
pstatement2.close();
while(rs.next()){
字符串department=null;
字符串主题=null;
字符串除法=空;
int wflinktoid=0;
int wformid=0;
智力得分=0;
ByteArrayInputStream localByteArrayInputStream=新的ByteArrayInputStream(rs.getBytes(“数据”);
ObjectInputStream localObjectInputStream=新ObjectInputStream(localByteArrayInputStream);
Map Map=(Map)localObjectInputStream.readObject();
strRecord+=“\”+rs.getString(“ID”)+“\”,”;
strRecord+=“\”+rs.getTimestamp(“开始”).toString()+“\”,”;
strRecord+=“\”+rs.getTimestamp(“结束”).toString()+“\”,“;
department=map.get(“department”)[0];
topic=map.get(“topic”)[0];
STRECORD+=“\”+部门+“\”,“;
strRecord+=“\”+主题+“\”,“;
如果(项目id==10869){
if(map.get(“division”)==null){
分部=“旧样品”;
}
否则{
division=map.get(“division”)[0];
}
strRecord+=“\”+除法+“\”,“;
strRecord+=“\”+map.get(“11163”)[0]+“\”,“;//name
strRecord+=“\”+map.get(“11163”)[1]+“\”,“;//员工代码
strRecord+=“\”“+map.get(“11163”)[2]+“\”,“;//名称
除法=空;
}
否则如果(项目id==10293){
strRecord+=“\”+map.get(“10322”)[0]+“\”,“;//name
strRecord+=“\”+map.get(“10322”)[1]+“\”,”;
strRecord+=“\”+map.get(“10322”)[2]+“\”,”;
strRecord+=“\”+map.get(“10322”)[3]+“\”,”;
}
//获取路由主题--从这里开始从wflinktoid获取主题id
pstatement2=db.getDBConnection().prepareStatement(“从WFLINK中选择wflinktoid,其中WFLINKFROMID=?和wfrule=?”;
pstatement2.setInt(1,wfstnodeid);//站点id
pstatement2.setString(2,“部门=='”+department+“'&&topic=='“+topic+””);
rs2=pstatement2.executeQuery();
wflinktoid=0;
while(rs2.next()){
wflinktoid=rs2.getInt(“wflinktoid”);
}
rs2.close();
pstatement2.close();
db.closeDBConnection();
部门=空;
topic=null;
pstatement2=db.getDBConnection().prepareStatement(“从WFSubForm中选择wfformid,其中wfstationid=?”;
pstatement2.setInt(1,wflinktoid);
rs2=pstatement2.executeQuery();
while(rs2.next()){
wfformid=rs2.getInt(“wfformid”);
}
rs2.close();
pstatement2.close();
db.closeDBConnection();
pstatement2=db.getDBConnection().prepareStatement(“从调查问题q中选择q.*,节点列表n,其中q.fieldid=n.nlnodeid和n.nlidatch=?和(q.exttype='checkbox'或q.exttype='radio'),q.answer按q.seq的顺序不是空的”);
pstatement2.setInt(1,wformid);
rs2=pstatement2.executeQuery();
得分=0;
while(rs2.next()){
if(map.get(rs2.getString(“fieldid”))==null){
strRecord+=“\”+map.get(rs2.getString(“fieldid”))+“\”,”;
if(rs2.getString(“ANSWER”).trim().equals(map.get(rs2.getString(“fieldid”))){
score+=Integer.parseInt(rs2.getString(“score”);//获取分数
}
}
否则{
字符串映射_answer=“”;
int y=0;
while(yConnection connection = db.getDBConnection();
pstatement = connection.prepareStatement(sql);
// ...
pstatement2 = connection.prepareStatement("SELECT ...");
// ...
// and at the end (in the finally block):
db.closeDBConnection();