有时我们的查询数据库数据时需要查询一个字段的数量,并且还要符合查询条件。
可以使用count来实现查询;
public int selectGPSDay(String company, String day){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
int count=0;
try {
conn=DbUtil.getCon();
String sql="SELECT COUNT(*) FROM t_gpsinsurance WHERE gpsi_company='"+company+"' AND gpsi_createTime LIKE '"+day+"%' AND gpsi_gpsID IS NOT NULL";
System.out.println(sql);
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
count=rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
DbUtil.close(ps, conn, rs);//关闭连接
} catch (Exception e) {
e.printStackTrace();
}
}
return count;
}