public String selectData()throws Exception{
shujuJDBC sj = new shujuJDBC();
List<Object[]> list = (List<Object[]>) sj.query("SELECT * FROM dbo.T_cs_cdxxb");
if(list==null){
return "0";
}else{
return "1";
}
}
现在已经确定 sj.query("...."); 能查出数据
也确定list 写错了! 现在头都大了!~ 那个大哥指教一下!
------解决方案--------------------------------------------------------
- Java code
List<Object[]> list = (List<Object[]>) sj.query("SELECT * FROM dbo.T_cs_cdxxb");
------解决方案--------------------------------------------------------
首先你要定义一个实体来接受你查询到的数据,然后把实体给list就可以了
------解决方案--------------------------------------------------------
List<Object[]> list = (List<Object[]>) sj.query("SELECT * FROM dbo.T_cs_cdxxb");
这样查询得到的list不会为空,如果没有值,只是他得size()为0
if(list.size()>0){
//有数据的情况
}else{
//没有数据的情况
}
------解决方案--------------------------------------------------------
还有你Object[]应该改成你取出来数据的实体类的类型
------解决方案--------------------------------------------------------
- Java code
if(null != list && list.size() > 0){Ojbect[] obj = null;for(int i=0; i < list.size(); i++){YourDto dto = new YourDto();obj = list.get(i);dto.setXXX(obj [0]);//注意空指针和类型转换,默认取出的是Object类型dto.setXXX(obj [1]);。。。}}
------解决方案--------------------------------------------------------
把你sj的查询方法改下吧如下:
- Java code
public List<Map<String, Object>> execQuery(String sql) { Connection conn=null; ResultSet rs=null; PreparedStatement ps=null; rs = null; List<Map<String, Object>> list=new ArrayList<Map<String, Object>>(); try { Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); conn=DriverManager.getConnection("proxool.SMSWSPool"); ps = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs = ps.executeQuery(); if(rs==null){ return null; } ResultSetMetaData md=rs.getMetaData(); int columnCount=md.getColumnCount(); Map<String, Object> map; while(rs.next()){ map=new HashMap<String, Object>(columnCount); for(int i=1;i<=columnCount;i++){ map.put(md.getColumnName(i), rs.getObject(i)); } list.add(map); } } catch (SQLException e) { log.error("SQLException2:"+e.getMessage()); } catch (ClassNotFoundException e) { log.error("ClassNotFoundException2:"+e.getMessage()); }finally{ try { if (conn != null){rs.close();ps.close();conn.close();} } catch (SQLException e) { log.error("关闭连接异常2:"+e.getMessage()); } } return list; }