最近的一个项目使用jdbc连接oracle数据库操作,出现高中各样的问题,又是结果集不足,又是关闭的连接next;
while (rs.next()) {也会报空指针异常,这是数据库操作太频繁,然后result set和prepare statement没有关闭,导致
解决办法是每次数据库操作后关闭数据库,同时不要讲result set作为数据库结果返回出去,可以返回list也可以返回map进行操作,
返回map
public Map<String, String> GetPoliceMsg(String policecatename,Connection conn ) throws SQLException, InstantiationException, IllegalAccessException{//得到需要发送警察所有信息
// Connection conn=OracleUtil.getinstance().getOracleConnection();
Map<String, String> map=new HashMap<String, String>();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String str=sdf.format(new java.util.Date());
String sql=" ";
try {
if (conn==null||conn.isClosed()) {
// conn=OracleUtil.getinstance().getOracleConnection();
conn=new ConnectionPool().Oracletwo();
pstme = conn.prepareStatement(sql);
}
else {
pstme = conn.prepareStatement(sql);
}
pstme.setString(1, policecatename);
pstme.setString(2, str);
rs=pstme.executeQuery();
int i=1;
while (rs.next()) {
map.put(rs.getString(1), rs.getString(2));
// System.out.println("++++++"+(i++)+rs.getString(1)+rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}
return map;
}
map转换成list
Map< String, String > policeMap=new HashMap<String, String>();
policeMap=new OraclemateSql().GetPoliceMsg(LOCALPOLICE,connoracle);//
List<String> PoliceidList = new ArrayList<String>();
List<String> PolicePhoneList=new ArrayList<String>();
// int i=1;
for (Map.Entry<String , String > e : policeMap.entrySet()) {
PoliceidList.add(e.getKey());
PolicePhoneList.add(e.getValue());
// System.out.println("police_------"+(i++)+e.getKey()+e.getValue());
}
数据库的connecttion操作也是关键,在我的另一个博客上有:
http://blog.csdn.net/yulungggg/article/details/63732432
http://blog.csdn.net/yulungggg/article/details/63727703