简体   繁体   中英

Changing tinyint(1) datatype in a mysql database using java

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

use setByte() instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM