rs=pstme.executeQuery()报null class found解决

本文介绍了使用JDBC连接Oracle数据库时遇到的问题及解决方案,重点讨论了如何避免空指针异常和资源泄露,提供了将Map转换为List的方法,并强调了正确管理数据库连接的重要性。

最近的一个项目使用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.youkuaiyun.com/yulungggg/article/details/63732432

http://blog.youkuaiyun.com/yulungggg/article/details/63727703


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值