从数据库中查出的记录,怎么放到JAVA集合类中?优雅的解决方式。
展开全部
你好,连同对数据库的查询,我写的代码如下:
Statement st=conn.createStatement();
Result rs=st.executeQuery("select ZYLB,MON from MyTable");
Map<String,List<String>> map=new HashMap<String,List<String>>();
while(rs.next()){
String mon=rs.getString("MON");
List<String> list=map.get(mon);
if(null==list){
list=new ArrayList<String>();
}
list.add(rs.getString("ZYLB");
}
Statement st=conn.createStatement();
Result rs=st.executeQuery("select ZYLB,MON from MyTable");
Map<String,List<String>> map=new HashMap<String,List<String>>();
while(rs.next()){
String mon=rs.getString("MON");
List<String> list=map.get(mon);
if(null==list){
list=new ArrayList<String>();
}
list.add(rs.getString("ZYLB");
}
展开全部
使用Hashtable的简单示例如下,将1,2,3放到Hashtable中,他们的key分别
是”one”,”two”,”three”:
Hashtable numbers = new Hashtable();
numbers.put(“one”, new Integer(1));
numbers.put(“two”, new Integer(2));
numbers.put(“three”, new Integer(3));
要取出一个数,比如2,用相应的key:
Integer n = (Integer)numbers.get(“two”);
System.out.println(“two = ” + n);
是”one”,”two”,”three”:
Hashtable numbers = new Hashtable();
numbers.put(“one”, new Integer(1));
numbers.put(“two”, new Integer(2));
numbers.put(“three”, new Integer(3));
要取出一个数,比如2,用相应的key:
Integer n = (Integer)numbers.get(“two”);
System.out.println(“two = ” + n);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
List list = null;
Map map = new HashMap();
for ( int i = 0; i < length; i++) {
list = map.get(month[i]);
if ( list == null ) {
list = new ArrayList();
}
list.add(record[i]);
map.put(month[i], list);
}
Map map = new HashMap();
for ( int i = 0; i < length; i++) {
list = map.get(month[i]);
if ( list == null ) {
list = new ArrayList();
}
list.add(record[i]);
map.put(month[i], list);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询