gpt4 book ai didi

java - java中的搜索方法 "ID not found"

转载 作者:太空宇宙 更新时间:2023-11-04 13:31:59 24 4
gpt4 key购买 nike

我在搜索方法方面遇到了一些麻烦,我尝试了下面的代码,他正在工作,但是当数据库中不存在 Owner_ID 时的问题不会给我错误消息“未找到所有者”?

我在这里缺少什么?

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 不为 null,因为它包含 owndao.serach(OWNER_ID) 返回的任何内容的哈希码。

如果未找到 OWNER_ID,请尝试在搜索方法中返回 null

关于java - java中的搜索方法 "ID not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32080884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com