使用java更改mysql数据库中的tinyint(1)数据类型

时间:2016-07-17 10:30:12

标签: java mysql

当客户从数据库中删除时,他的状态必须设置为脱机(而不是实际从数据库中删除客户)。在我的数据库中,我使用tinyint(1)将状态设置为0(非活动)或1(活动)。现在我该怎么做呢?

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

}

我在localhost上通过USB网络服务器使用SQL数据库。我不确定这是否正常,因为我的其余代码不完整而且我无法测试,但我必须确定在继续之前。谢谢

1 个答案:

答案 0 :(得分:2)

改为使用setByte()

因为SQL is equal to byte in java中的TINYINT,它也有一些方法,例如:setByte()updateByte()以及getByte()