适合小项目哈,大项目一般都有自己的架构了
采用list和map的组合转换jdbc返回的结果集,list代表行,map代表列,以下为源码示例,我曾写的一个工具类的一个方法,有点基础应该都能看懂。
/**
*
* <p>select</p>
* <p>Description: 获得查询结果集</p>
* <p>Copyright: 2014-9-12</p>
* <p>Company: *** </p>
* @author bard email:178867729@qq.com
* @date 2014-9-12 下午2:27:08
* @version 1.0
* @param sql 查询语句
* @return id
* @throws FactoryException
*/
public List<Map> select(String sql){
Statement stmt = null;
ResultSet rs = null;
List<Map> list=new ArrayList<Map>();
try {
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
ResultSetMetaData rsmd = rs.getMetaData();
int counts = rsmd.getColumnCount();
Map<String, Object> map=new LinkedHashMap<String, Object>();//用这个map是为了保持装进去的顺序不变
for (int i = 1; i <= counts; i++) {
map.put(rsmd.getColumnName(i), rs.getObject(i));
}
list.add(map);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}