0

When a customer gets removed from the database, his status has to be set to offline (rather than actually deleting the customer from the database). In my database I'm using tinyint(1) to set the status to 0 (inactive) or 1 (active). Now how do I go about coding this?

// Delete a customer on the database by setting his status to inactive
public void deleteCust(int id) throws DBException {

  // connect (and close connection)
  try (Connection conn = ConnectionManager.getConnection();) {
     // PreparedStatement
     try (PreparedStatement stmt = conn.prepareStatement(
        "update customer set active = ? where id = ?");) {
        stmt.settinyint(1, 0);
        stmt.setInt(2, id);

        stmt.execute();
     } catch (SQLException sqlEx) {
        throw new DBException("SQL-exception in deleteCust - statement"+ sqlEx);
     }
  } catch (SQLException sqlEx) {
     throw new DBException(
        "SQL-exception in deleteCust - connection"+ sqlEx);
  }

}

I'm using an SQL database via USB webserver on localhost. I'm unsure whether this works right now because the rest of my code is incomplete and I'm unable to test, but I have to be sure before continuing. Thank you

1 Answer 1

1

use setByte() instead.

because TINYINT in SQL is equal to byte in java, also it has some methods, like: setByte() and updateByte() and getByte()

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! :) I'll try this
your welcome, check these examples java2s.com and tutorialspoint.com too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.