我想要复制ResultSet的对象 怎么弄?
前台需要显示,所以吧结果集内的对象取出放入list在前台显示同时提供倒出Excel文件的按钮我用的POI来生成Excel文件,需要ResultSet对象所以我想,复制一个...
前台需要显示,所以吧结果集内的对象取出放入list在前台显示
同时提供倒出Excel文件的按钮
我用的POI来生成Excel文件,需要ResultSet对象
所以我想,复制一个ResultSet对象,一个用于显示,一个用于导出,以上
javaBean 我有想过,可是我的需求是有好多表以及javaBean对象
所以我需要一个通用的接口。而不是针对某一个JavaBean 展开
同时提供倒出Excel文件的按钮
我用的POI来生成Excel文件,需要ResultSet对象
所以我想,复制一个ResultSet对象,一个用于显示,一个用于导出,以上
javaBean 我有想过,可是我的需求是有好多表以及javaBean对象
所以我需要一个通用的接口。而不是针对某一个JavaBean 展开
展开全部
不用复制resultset 你创建一个javabean 把结果集全部封装进去
给你一段代码
ArrayList list = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getInstance().getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlcmd.toString());
while (rs.next()) {
//这是一个报表对象类,我把结果集的内容全部放进去
EmpInfoReportBO tempbo = new EmpInfoReportBO();
tempbo.setCOMPANY(rs.getString("COMPANY"));
tempbo.setORG_NAME(rs.getString("ORG_NAME"));
tempbo.setOLD_EMPNO(rs.getString("OLD_EMPNO"));
tempbo.setEMP_NAME(rs.getString("EMP_NAME"));
tempbo.setRP_ID(rs.getInt("RP_ID"));
tempbo.setEMP_SEQ_ID(rs.getInt("EMP_SEQ_ID"));
tempbo.setRP_DATE(rs.getString("RP_DATE"));
tempbo.setRP_TYPE(rs.getString("RP_TYPE"));
tempbo.setRP_CATEGORY(rs.getString("RP_CATEGORY"));
tempbo.setRP_CAUSE(rs.getString("RP_CAUSE"));
tempbo.setRP_NOTE(rs.getString("RP_NOTE"));
tempbo.setGENDER(rs.getString("GENDER"));
tempbo.setPosition_Name(rs.getString("POSITION_NAME"));
tempbo.setDuty(rs.getString("Duty"));
tempbo.setEMP_RankName(rs.getString("RANK_NAME"));
tempbo.setAge(rs.getString("AGE"));
//然后把这个对象tempbo放入ArrayList 并返回ArrayList
list.add(tempbo);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
this.closeResultSet(rs);
this.closeStatement(stmt);
this.closeConnection(conn);
}
return list;
//然后我再转EXCEL的类中的parse方法中传入了刚才的ArrayList
public void parse(String title, ArrayList rcountlist,
ServletOutputStream os) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFFont f = wb.createFont();
// f.setFontName("GB-2312");
f.setFontHeightInPoints((short) 12);
HSSFCellStyle headstyle = wb.createCellStyle();
headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
HSSFCellStyle rowheadstyle = wb.createCellStyle();
rowheadstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFCellStyle cs = wb.createCellStyle();
cs.setFont(f);
//设置单元格背景色
cs.setFillBackgroundColor(HSSFColor.BLUE.index);
cs.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cs.setBorderLeft((short) 1); //左边框
cs.setBorderRight((short) 1); //右边框
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER); //居中对齐
cs.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
HSSFCellStyle ratecs = wb.createCellStyle();
ratecs.setFillForegroundColor(HSSFColor.PINK.index);
ratecs.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
HSSFSheet sheet = wb.createSheet("new sheet");
//以下是设置各列宽度
sheet.setColumnWidth((short) 0, (short) 4800);
sheet.setColumnWidth((short) 1, (short) 4800);
sheet.setColumnWidth((short) 2, (short) 4800);
sheet.setColumnWidth((short) 3, (short) 3800);
sheet.setColumnWidth((short) 4, (short) 3800);
sheet.setColumnWidth((short) 5, (short) 3800);
sheet.setColumnWidth((short) 6, (short) 3800);
sheet.setColumnWidth((short) 7, (short) 3800);
sheet.setColumnWidth((short) 8, (short) 3800);
sheet.setColumnWidth((short) 9, (short) 3800);
sheet.setColumnWidth((short) 10, (short) 3800);
sheet.setColumnWidth((short) 11, (short) 3800);
sheet.setColumnWidth((short) 12, (short) 3800);
sheet.setColumnWidth((short) 13, (short) 4800);
sheet.setColumnWidth((short) 14, (short) 6800);
sheet.setColumnWidth((short) 15, (short) 15800);
sheet.setColumnWidth((short) 16, (short) 5800);
HSSFRow secondrow = sheet.createRow((short) 0);
//........表头信息我就省略了
//下边是你需要的代码 我把刚才那个list通过参数传进来
int line = 1;
for (int i = 0; i < rcountlist.size(); i++) {
EmpInfoReportBO tempbo = (EmpInfoReportBO)
rcountlist.get(i);
HSSFRow datarow = sheet.createRow((short) line);
this.createStringCell(datarow, (short) 0, tempbo.getCOMPANY(),
rowheadstyle);
this.createStringCell(datarow, (short) 3, tempbo.getRP_DATE(),
rowheadstyle);
this.createStringCell(datarow, (short) 4, tempbo.getOLD_EMPNO(),
rowheadstyle);
this.createStringCell(datarow, (short) 5, tempbo.getEMP_NAME(),
rowheadstyle);
this.createStringCell(datarow, (short) 6, tempbo.getGENDER(),
rowheadstyle);
this.createStringCell(datarow, (short) 7, tempbo.getAge(),
rowheadstyle);
this.createStringCell(datarow, (short) 8, tempbo.getORG_NAME(),
rowheadstyle);
this.createStringCell(datarow, (short) 9, tempbo.getPosition_Name(),
rowheadstyle);
this.createStringCell(datarow, (short) 10, tempbo.getDuty(),
rowheadstyle);
this.createStringCell(datarow, (short) 11, tempbo.getEMP_RankName(),
rowheadstyle);
this.createStringCell(datarow, (short) 12,
tempbo.getRP_CAUSE(),
rowheadstyle);
this.createStringCell(datarow, (short) 13,
tempbo.getRP_TYPE(), rowheadstyle);
this.createStringCell(datarow, (short) 14,
tempbo.getRP_CATEGORY(), rowheadstyle);
this.createStringCell(datarow, (short) 16, tempbo.getRP_NOTE(),
rowheadstyle);
line++;
}
wb.write(os);
}
//另外还有个方法用来创建单元格。这个方法是自己写的,你也可以用POI的方法。意思明白就好
private static void createStringCell(HSSFRow row, short index, String value,
HSSFCellStyle cs) {
HSSFCell cell = row.createCell(index);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(value);
cell.setCellStyle(cs);
}
回答完毕
-------------------------------------------------------------
那也很简单啊,你别用ArrayList了用HashMap就好
给你一段代码
ArrayList list = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getInstance().getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlcmd.toString());
while (rs.next()) {
//这是一个报表对象类,我把结果集的内容全部放进去
EmpInfoReportBO tempbo = new EmpInfoReportBO();
tempbo.setCOMPANY(rs.getString("COMPANY"));
tempbo.setORG_NAME(rs.getString("ORG_NAME"));
tempbo.setOLD_EMPNO(rs.getString("OLD_EMPNO"));
tempbo.setEMP_NAME(rs.getString("EMP_NAME"));
tempbo.setRP_ID(rs.getInt("RP_ID"));
tempbo.setEMP_SEQ_ID(rs.getInt("EMP_SEQ_ID"));
tempbo.setRP_DATE(rs.getString("RP_DATE"));
tempbo.setRP_TYPE(rs.getString("RP_TYPE"));
tempbo.setRP_CATEGORY(rs.getString("RP_CATEGORY"));
tempbo.setRP_CAUSE(rs.getString("RP_CAUSE"));
tempbo.setRP_NOTE(rs.getString("RP_NOTE"));
tempbo.setGENDER(rs.getString("GENDER"));
tempbo.setPosition_Name(rs.getString("POSITION_NAME"));
tempbo.setDuty(rs.getString("Duty"));
tempbo.setEMP_RankName(rs.getString("RANK_NAME"));
tempbo.setAge(rs.getString("AGE"));
//然后把这个对象tempbo放入ArrayList 并返回ArrayList
list.add(tempbo);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
this.closeResultSet(rs);
this.closeStatement(stmt);
this.closeConnection(conn);
}
return list;
//然后我再转EXCEL的类中的parse方法中传入了刚才的ArrayList
public void parse(String title, ArrayList rcountlist,
ServletOutputStream os) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFFont f = wb.createFont();
// f.setFontName("GB-2312");
f.setFontHeightInPoints((short) 12);
HSSFCellStyle headstyle = wb.createCellStyle();
headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
HSSFCellStyle rowheadstyle = wb.createCellStyle();
rowheadstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFCellStyle cs = wb.createCellStyle();
cs.setFont(f);
//设置单元格背景色
cs.setFillBackgroundColor(HSSFColor.BLUE.index);
cs.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cs.setBorderLeft((short) 1); //左边框
cs.setBorderRight((short) 1); //右边框
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER); //居中对齐
cs.setVerticalAlignment(HSSFCellStyle.VERTICAL_JUSTIFY);
HSSFCellStyle ratecs = wb.createCellStyle();
ratecs.setFillForegroundColor(HSSFColor.PINK.index);
ratecs.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
HSSFSheet sheet = wb.createSheet("new sheet");
//以下是设置各列宽度
sheet.setColumnWidth((short) 0, (short) 4800);
sheet.setColumnWidth((short) 1, (short) 4800);
sheet.setColumnWidth((short) 2, (short) 4800);
sheet.setColumnWidth((short) 3, (short) 3800);
sheet.setColumnWidth((short) 4, (short) 3800);
sheet.setColumnWidth((short) 5, (short) 3800);
sheet.setColumnWidth((short) 6, (short) 3800);
sheet.setColumnWidth((short) 7, (short) 3800);
sheet.setColumnWidth((short) 8, (short) 3800);
sheet.setColumnWidth((short) 9, (short) 3800);
sheet.setColumnWidth((short) 10, (short) 3800);
sheet.setColumnWidth((short) 11, (short) 3800);
sheet.setColumnWidth((short) 12, (short) 3800);
sheet.setColumnWidth((short) 13, (short) 4800);
sheet.setColumnWidth((short) 14, (short) 6800);
sheet.setColumnWidth((short) 15, (short) 15800);
sheet.setColumnWidth((short) 16, (short) 5800);
HSSFRow secondrow = sheet.createRow((short) 0);
//........表头信息我就省略了
//下边是你需要的代码 我把刚才那个list通过参数传进来
int line = 1;
for (int i = 0; i < rcountlist.size(); i++) {
EmpInfoReportBO tempbo = (EmpInfoReportBO)
rcountlist.get(i);
HSSFRow datarow = sheet.createRow((short) line);
this.createStringCell(datarow, (short) 0, tempbo.getCOMPANY(),
rowheadstyle);
this.createStringCell(datarow, (short) 3, tempbo.getRP_DATE(),
rowheadstyle);
this.createStringCell(datarow, (short) 4, tempbo.getOLD_EMPNO(),
rowheadstyle);
this.createStringCell(datarow, (short) 5, tempbo.getEMP_NAME(),
rowheadstyle);
this.createStringCell(datarow, (short) 6, tempbo.getGENDER(),
rowheadstyle);
this.createStringCell(datarow, (short) 7, tempbo.getAge(),
rowheadstyle);
this.createStringCell(datarow, (short) 8, tempbo.getORG_NAME(),
rowheadstyle);
this.createStringCell(datarow, (short) 9, tempbo.getPosition_Name(),
rowheadstyle);
this.createStringCell(datarow, (short) 10, tempbo.getDuty(),
rowheadstyle);
this.createStringCell(datarow, (short) 11, tempbo.getEMP_RankName(),
rowheadstyle);
this.createStringCell(datarow, (short) 12,
tempbo.getRP_CAUSE(),
rowheadstyle);
this.createStringCell(datarow, (short) 13,
tempbo.getRP_TYPE(), rowheadstyle);
this.createStringCell(datarow, (short) 14,
tempbo.getRP_CATEGORY(), rowheadstyle);
this.createStringCell(datarow, (short) 16, tempbo.getRP_NOTE(),
rowheadstyle);
line++;
}
wb.write(os);
}
//另外还有个方法用来创建单元格。这个方法是自己写的,你也可以用POI的方法。意思明白就好
private static void createStringCell(HSSFRow row, short index, String value,
HSSFCellStyle cs) {
HSSFCell cell = row.createCell(index);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(value);
cell.setCellStyle(cs);
}
回答完毕
-------------------------------------------------------------
那也很简单啊,你别用ArrayList了用HashMap就好
展开全部
如果只是简单的复制的话,用clone()就可以了
首先要实现Cloneable接口,然后在重载clone方法,最后在clone()方法中调用了super.clone(),这也意味着无论clone类的继承结构是什么样的,super.clone()直接或间接调用了java.lang.Object类的clone()方法。
首先要实现Cloneable接口,然后在重载clone方法,最后在clone()方法中调用了super.clone(),这也意味着无论clone类的继承结构是什么样的,super.clone()直接或间接调用了java.lang.Object类的clone()方法。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
stmt.execute("select * from tablename;select * from tablename");//这里使用两个相同的sql语句查询两个相同的数据集
ResultSet rs=stmt.getResultSet();
stmt.getMoreResults();//指向下一个数据集
ResultSet rs1=stmt.getResultSet();
这样得到的就是两个相同查询语句得到的结果
用CachedRowSet,例:
rs = stmt.executeQuery("select col1, col2 from test_table");
// create a new rowset and populate it...
CachedRowSet crs = new CachedRowSet();
crs.populate(rs);
CachedRowSet crs2 = (CachedRowSet)crs.clone();
ResultSet rs=stmt.getResultSet();
stmt.getMoreResults();//指向下一个数据集
ResultSet rs1=stmt.getResultSet();
这样得到的就是两个相同查询语句得到的结果
用CachedRowSet,例:
rs = stmt.executeQuery("select col1, col2 from test_table");
// create a new rowset and populate it...
CachedRowSet crs = new CachedRowSet();
crs.populate(rs);
CachedRowSet crs2 = (CachedRowSet)crs.clone();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没明白你问的是什么意思,也没想明白复制一个ResultSet有什么用。。。能否补充下
理论上来说ResultSet是一个集合,那里面存放的就应该是对象,就能够用循环或者迭代的方法取出来,然后就可以存放到另外一个集合里面,不过我想不出来这样有什么意义。。。。。
理论上来说ResultSet是一个集合,那里面存放的就应该是对象,就能够用循环或者迭代的方法取出来,然后就可以存放到另外一个集合里面,不过我想不出来这样有什么意义。。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是一个接口,接口是不可以通过new来实例化对象的。
clonable接口也要自己实现,因此clone不大现实。
clonable接口也要自己实现,因此clone不大现实。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询