method that checks if there is any record in my database

Asked

Viewed 406 times

1

Good morning guys. I’m new to Java programming, and I’d like a little help. Well, the thing is, I want to do a method and check if there is any record in my table, if you have, proceed with the action, otherwise it gets out of the if. I already have this select ready, now I just need to implement it in my method and do the check.

Here’s the class you check into:

 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");
        }

Right below is my stabbing, where I’m in trouble, I don’t know what kind of feedback that has to be.. I put Boolean, but it’s not working:

  public boolean selectTemporary() throws ServiceException {

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

And this is my select, where you search my base for the records.

  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;
    }

1 answer

0


Your method acknowledgementsDao.selectAckTemporary2() returns an object Acknowledgementstemporary when you find a result and null when not found, so this method can not be used alone inside the if, because it does not return a Boolean.

To solve the problem:

  • Replace the line if(acknowledgementsDao.selectAckTemporary2()); for
  • if(acknowledgementsDao.selectAckTemporary2() != null)

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

Summarizing your code is as below

public boolean selectTemporary() throws ServiceException {

    try {
        acknowledgementsDao = new AcknowledgementsTemporaryDao();

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

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

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.