无论我加几种查询条件,返回给我的查询总量恒等于mysql数据的总量
经过逐行打印,发现没有进入if语句中
问题:帮忙找出为什么没有进入if语句中????
public static int tongJi(StudentDuoTiaoJian sdtj) {
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
conn = Util.getConnection();
StringBuilder sql = new StringBuilder();
sql.append(" select count(*)");
sql.append(" from stu_student");
sql.append(" where 1=1");
if (sdtj.getId() != 0) {
sql.append(" and stu_id=?");
System.out.println("==============================");
}
if (sdtj.getName() != null) {
sql.append(" and stu_name like ?");
System.out.println("==============================");
}
if (sdtj.getNianji() != 0) {
sql.append(" and stu_nianji=?");
System.out.println("==============================");
}
if (sdtj.getPsw() != null) {
sql.append(" and stu_psw=?");
System.out.println("==============================");
}
if (sdtj.getIdNo() != 0) {
sql.append(" and stu_id>=?");
System.out.println("==============================");
}
if (sdtj.getIdEnd() != 0) {
sql.append(" and stu_id<=?");
System.out.println("==============================");
}
int a = 0;
try {
ps = conn.prepareStatement(sql.toString());
if (sdtj.getId() != 0) {
ps.setInt(++a, sdtj.getId());
System.out.println(a);
}
if (sdtj.getName() != null) {
ps.setString(++a, sdtj.getName());
System.out.println(a);
}
if (sdtj.getNianji() != 0) {
ps.setInt(++a, sdtj.getNianji());
System.out.println(a);
}
if (sdtj.getPsw() != null) {
ps.setString(++a, sdtj.getPsw());
System.out.println(a);
}
if (sdtj.getIdNo() != 0) {
ps.setInt(++a, sdtj.getIdNo());
System.out.println(a);
}
if (sdtj.getIdEnd() != 0) {
ps.setInt(++a, sdtj.getIdEnd());
System.out.println(a);
}
rs = ps.executeQuery();
if (rs.next()) {
int num = rs.getInt(1);
System.out.println(num);
return num;
}else{
return 0;
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return 0;
}finally {
Util.close(conn, ps, rs);
}
}
你先写个语句,把你的sql变量打印出来,你就知道问题了。