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

我在这里想念什么?

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)返回的哈希码。

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

10-08 19:24