method that checks if there is any record in my database

1

Good morning everyone. I'm new to Java programming, and would like some help. Well, it's the following, I want to make a method and check if it has any record in my table, if it has, proceed with the action, otherwise it falls outside the if. I already have this select ready, now I just need to implement this in my method and do the verification.

Here is the class you are checking:

 public static void main(String args[]) {

    AcknowledgementsTemporaryFacade ack = new AcknowledgementsTemporaryFacade();
    try {

        if(ack.selectTemporary()){
            System.out.println("Tem registros");
        }
        else{
            System.out.println("Não tem registros");
        }

  public boolean selectTemporary() throws ServiceException {

    try {
       acknowledgementsDao = new AcknowledgementsTemporaryDao();
       if(acknowledgementsDao.selectAckTemporary2());
            return true;
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return false;
}

And here is my select, where it searches my records base.

  public AcknowledgementsTemporary selectAckTemporary2() throws ServiceException {

    AcknowledgementsTemporary ack = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Connection conn = null;
    String commandSQL = "SELECT \"ds_code\", \"it_date_time_ack\" "
    + "FROM \"tb_ack_temporary\" ";

    try {
    openConnection();
    conn = this.conn;
    ps = conn.prepareStatement(commandSQL);

    rs = ps.executeQuery();


    if(rs.next()){
        ack = new AcknowledgementsTemporary();
        ack.setCode(rs.getString("ds_code"));
        ack.setDateTimeAck(rs.getLong("it_date_time_ack"));
        return ack;
    };


    } catch (Exception e) {
      e.printStackTrace(System.err);
      LOGGER.error("command sql: " + commandSQL + "parameters: " + ack.getCode() +    ack.getDateTimeAck());
    } finally {
    ConnectionDatabaseFactory.closeConnection(conn, ps);
    }

    return null;
    }
    
asked by anonymous 14.04.2015 / 17:14

1 answer

0

Your acknowledgmentsDao.selectAckTemporary2 () method returns a AcknowledgmentsTemporary object when it finds a result and null when it does not find, so this method does not can be used alone inside the if, as it does not return a boolean.

To resolve the problem:

  • Replace the if (acknowledgmentsDao.selectAckTemporary2 ()) line with
  • if (acknowledgementsDao.selectAckTemporary2 ()! = null)

Also note that you are ending if when using (;) at the end of the line, ending this check before arriving at return . for this reason it is recommended to use the keys to open and close the if .

In short your code looks like the one below

public boolean selectTemporary() throws ServiceException {

    try {
        acknowledgementsDao = new AcknowledgementsTemporaryDao();

        if(acknowledgementsDao.selectAckTemporary2() != null){
            return true;
        } else {
            return false;
        }

    } catch(Exception e) {
        e.printStackTrace();
    }

}
    
14.04.2015 / 18:56