value object--javabean中的问题,急需解决!!
简单说明一下问题:
Org.java
public class Org{
private int orgId;
public void setOrgId(int orgId){
this.orgId=orgId;
}
public int getOrgId(){
return orgId;
}
...
}
OrgDaoOraImpl.java中的方法select代码如下:
public Org[] select(Connection con, Org o) throws SQLException {
Org[] orgs = null;
PreparedStatement ps = null;
ResultSet rs = null;
StringBuffer buff = new StringBuffer();
try {
buff.append("SELECT * FROM org WHERE 1 = 1 ");
if (o != null) {
if (o.getOrgId() >= 0)
buff.append(" AND orgid=?");
if (o.getOrgName() != null && !o.getOrgName().equals(""))
buff.append(" AND orgname LIKE ? ");
}
buff.append(" order by orgid asc ");
ps = con.prepareStatement(buff.toString());
if (o != null) {
int index = 1;
if (o.getOrgId() >=0)
ps.setInt(index++, o.getOrgId());
if (o.getOrgName() != null && !o.getOrgName().equals(""))
ps.setString(index++, "%" + o.getOrgName() + "%");
}
rs = ps.executeQuery();
orgs = (Org[]) populate(rs);
} catch (Exception e) {
throw new SQLException("Exception occurred when find! "
+ e.getMessage());
} finally {
DbUtil.close(rs, ps, null);
}
return orgs == null ? new Org[0] : orgs;
}
假设数据库中有五条记录,orgid分别为0,1,2,3,4,5.
Org o=new Org();
o.setOrgName("wei");
select(o);
这时问题就出现了。如果orgId为1,2,3,4,5,姓名为wei时,将查不到记录。
因为javabean中默认int型为0,此时上面相关语句将变成如下:
select * from org where 1=1 and orgid=0 and orgname='wei'
大家有没有什么办法急救呀。。
1.不让数据库中的机构id为0,可行,但在我这已经没有办法了,别人用了,不能改,和其它表关联很大。