user3227070 user3227070 - 1 year ago 48
SQL Question

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

} 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

Answer Source

use setByte() instead.

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