return list返回bean中的各值。
解决方案 »
- 求高手解决一个关于问题org.springframework.beans.factory.BeanCreationException
- Hibernate隔一段时间加载不了外表
- 关于增删改查的方法定义
- EXTJS问题
- 一百万以上文件检索效率 求帮助
- ibatis isEqual 等动态标签中使用 boolean类型,应该 如何比较?
- xstream对象转xml遇到特殊字符怎么处理方便些 在线等
- 求救!我的Tomcat5.0,到底怎么了?没有一个JSP程序可以运行的,都是出现相同的错误!
- 有没有关于j2ee好一点的免费资料!
- S2SH 框架 sql/hql语句放在哪一层合适
- servlet中流的问题
- 求一个最简单的webservice+ajax最简单的例子~
如果你要判断返回的List中的对象类型的话,可以用instanceof来判断,再分别把不同的对象放在不同的集合中进行传递
while (this.rs.next())
{
//创建一个CollectPolicyTable对象
CollectPolicyTable a = new CollectPolicyTable();
a.setPolicy_id(this.rs.getInt(1));
a.setPolicy_name(this.rs.getString(2));
a.setPolicy_creatTime(this.rs.getDate(3));
a.setPolicy_creator(this.rs.getString(4));
a.setPolicy_status(this.rs.getInt(5));
a.setPolicy__re(this.rs.getString(6));
a.setPgent_id(this.rs.getInt(7));
a.setSeverity(this.rs.getString(8));
a.setPnalysis_id(this.rs.getInt(9));
//将对象加入LIST
list.add(a);}我的意思是 怎么样才能 同一个方法适用与不同表的查询
{
//创建一个CollectPolicyTable对象
CollectPolicyTable a = new CollectPolicyTable();
a.setPolicy_id(this.rs.getInt(1));
a.setPolicy_name(this.rs.getString(2));
a.setPolicy_creatTime(this.rs.getDate(3));
a.setPolicy_creator(this.rs.getString(4));
a.setPolicy_status(this.rs.getInt(5));
a.setPolicy__re(this.rs.getString(6));
a.setPgent_id(this.rs.getInt(7));
a.setSeverity(this.rs.getString(8));
a.setPnalysis_id(this.rs.getInt(9));
//将对象加入LIST
list.add(a);}
来实现的
没有找到更好的方法....
期待中.0.........
Object domain=null;
while(rs.next()){
domain=Class.forName("Domain").newInstance();
for(int i=1;i<=rsm.getColumnCount();i++){
String recordValue=rs.getString(rsm.getColumnName(i));
Method m=domain.getClass().getMethod("set"+rsm.getColumnName(i),new Class[]{recordValue.getClass()});
m.invoke(domain,new Object[]{recordValue});
}
list.add(domain);
}
public static ArrayList<LinkedHashMap> delRes(String sql) {
ArrayList<LinkedHashMap> list = new ArrayList<LinkedHashMap>(); Connection con = null;
PreparedStatement stmt = null;
ResultSetMetaData metaData=null;
try {
con = ConDB.getCon();
stmt = (PreparedStatement) con.prepareStatement(sql);
ResultSet result = stmt.executeQuery();
metaData=result.getMetaData();
while (result.next()) {
LinkedHashMap v = new LinkedHashMap();
for (int i = 1; i <= metaData.getColumnCount(); i++)
v.put(metaData.getColumnLabel(i),result.getObject(i));
list.add(v);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
这算是一个公共方法吧 传入一个sql语句返回一个ArrayList<LinkedHashMap>的结果集 该结果集是一个查询结果的LinkedHashMap集 如果你用的是JSF框架就可以把这个LinkedHashMap放到标签里去迭代输出
其中v.put(metaData.getColumnLabel(i),result.getObject(i));这句表示把数据库的字段名设为KEY 把该字段的值设为Value 如果在Eclipse当中这句可能会被提示要引用参数化 不用理它不影响使用 上面朋友们写的那些方法也是没错的 我以前也是那么写的只是后来我感觉局限性大了些 就改用这种方法了 我也是刚开始做J2EE 昨天才在这里注册的用户 希望在这里能和大家多交流学习 ^_^