[英]Search method in java “ID not found”
I have some troubles with search method, I tried the code below and he is working, but the problem when an Owner_ID doesn't exist in the database doesn't give me the error msg "Owner not found"? 我在搜索方法上遇到了一些麻烦,我尝试了下面的代码,他正在工作,但是当数据库中不存在Owner_ID时的问题不会给我错误消息msg“找不到所有者”吗?
what am I missing here ? 我在这里想念什么?
private void search() {
try {
int OWNER_ID = Integer.parseInt(this.owneridtxt.getText().trim());
OwnerRecordDao owndao = new OwnerRecordDao();
OwnerRecord own = owndao.search(OWNER_ID);
if (own != null) {
companytxt.setText(own.getCOMPANY().toUpperCase());
addresstxt.setText(own.getADDRESS().toUpperCase());
citytxt.setText(own.getCITY().toUpperCase());
statetxt.setText(own.getSTATE().toUpperCase());
zipcodetxt.setText(Integer.toString(own.getZCODE()));
phonetxt.setText(own.getPHONE_N());
regdatetxt.setText(own.getREG_DATE());
}
else{
DM.Message("Owner not found");
}
} catch (Exception e) {
System.out.println(e.toString());
}
owneridtxt.requestFocus(true);
}
public OwnerRecord search(int OwnerCode) throws Exception {
OwnerRecord own = new OwnerRecord();
String sql = "SELECT COMPANY, ADDRESS, CITY, STATE, ZCODE, PHONE_N, REG_DATE FROM OWNER WHERE OWNER_ID = ?";
DBConnection con = new DBConnection();
Connection connect = con.getConnection();
PreparedStatement ps = connect.prepareStatement(sql);
try {
ps.setInt(1, OwnerCode);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
own.setCOMPANY(rs.getString("COMPANY"));
own.setADDRESS(rs.getString("ADDRESS"));
own.setCITY(rs.getString("CITY"));
own.setSTATE(rs.getString("STATE"));
own.setZCODE(rs.getInt("ZCODE"));
own.setPHONE_N(rs.getString("PHONE_N"));
own.setREG_DATE(rs.getString("REG_DATE"));
}
} catch (SQLException e) {
System.out.println(e.toString());
} finally {
try {
connect.close();
ps.close();
} catch (SQLException e) {
System.out.println(e.toString());
}
}
return own;
}
own
is not null because it contains the hash code to whatever owndao.serach(OWNER_ID)
is returning. own
不为null,因为它包含到owndao.serach(OWNER_ID)
返回的哈希码。
Try returning null
in the search method if the OWNER_ID
isn't found. 如果未找到
OWNER_ID
请尝试在搜索方法中返回null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.