请高手帮我把这个方法得到的值遍历出来
publicHashMapExcuteQuery(Stringselect){InitialContextcontext=null;Connectionconnectio...
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;
}
实体类事foodInfo,有六个字段
private Object foodID;
private Object foodName;
private Object remark;
private Object foodPrice;
private Object description;
private Object foodImage;
不给力啊!!提示索引0超出范围
可不可以用实体存储数据 展开
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;
}
实体类事foodInfo,有六个字段
private Object foodID;
private Object foodName;
private Object remark;
private Object foodPrice;
private Object description;
private Object foodImage;
不给力啊!!提示索引0超出范围
可不可以用实体存储数据 展开
1个回答
展开全部
Map map=ExcuteQuery("...");
Iterator iter=map.entrySet().iterator();
while(iter.hashNext())
{
Map.Entry entry=(Map.Entry)iter.next();
Object key=entry.getKey();
List list=(List)entry.getValue();
for(int i=0;i<list.size();i++)
{
Object obj=list.get(i);
System.out.println("key="+key+" value="+obj);
}
}
Iterator iter=map.entrySet().iterator();
while(iter.hashNext())
{
Map.Entry entry=(Map.Entry)iter.next();
Object key=entry.getKey();
List list=(List)entry.getValue();
for(int i=0;i<list.size();i++)
{
Object obj=list.get(i);
System.out.println("key="+key+" value="+obj);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询