谁能帮我把这个方法(通用的数据库查询方法)获取到的值遍历出来啊??数据库连接成功
实体类是foodInfo,有六个字段privateObjectfoodID;privateObjectfoodName;privateObjectremark;priva...
实体类是foodInfo,有六个字段
private Object foodID;
private Object foodName;
private Object remark;
private Object foodPrice;
private Object description;
private Object foodImage;
public HashMap ExcuteQuery(String select){
InitialContext context=null;
Connection connection=null;
DataSource source=null;
PreparedStatement PStatem=null;
ResultSet res=null;
HashMap tale=new HashMap();
try {
context = new InitialContext();
source=(DataSource) context.lookup("java:comp/env/jdbc/restrant");
connection=source.getConnection();
PStatem=connection.prepareStatement(select);
res=PStatem.executeQuery();
int column=PStatem.getMetaData().getColumnCount();
while(res.next()){
List list=new ArrayList();
for(int i=0;i<column;i++){
list.add(res.getObject(i));
}
tale.put(res.getObject(1), list);
}
System.out.println("数据库连接成功");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tale;
} 展开
private Object foodID;
private Object foodName;
private Object remark;
private Object foodPrice;
private Object description;
private Object foodImage;
public HashMap ExcuteQuery(String select){
InitialContext context=null;
Connection connection=null;
DataSource source=null;
PreparedStatement PStatem=null;
ResultSet res=null;
HashMap tale=new HashMap();
try {
context = new InitialContext();
source=(DataSource) context.lookup("java:comp/env/jdbc/restrant");
connection=source.getConnection();
PStatem=connection.prepareStatement(select);
res=PStatem.executeQuery();
int column=PStatem.getMetaData().getColumnCount();
while(res.next()){
List list=new ArrayList();
for(int i=0;i<column;i++){
list.add(res.getObject(i));
}
tale.put(res.getObject(1), list);
}
System.out.println("数据库连接成功");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tale;
} 展开
1个回答
2010-12-18
展开全部
// 默认你的column取对了,那么就是下面出的错了。。。。
for(int i=0;i<column;i++){
list.add(res.getObject(i));
}
从你后面的tale.put(res.getObject(1), list);来看你是知道ResultSet 编号从1开始的。
但你上面的for循环却是从0开始,应该就是这里错了。改成下面的就行了
for(int i=1;i<=column;i++){
list.add(res.getObject(i));
}
for(int i=0;i<column;i++){
list.add(res.getObject(i));
}
从你后面的tale.put(res.getObject(1), list);来看你是知道ResultSet 编号从1开始的。
但你上面的for循环却是从0开始,应该就是这里错了。改成下面的就行了
for(int i=1;i<=column;i++){
list.add(res.getObject(i));
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询