简体   繁体   English

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

[英]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). 在我的数据库中,我使用tinyint(1)将状态设置为0(不活动)或1(活动)。 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. 我正在通过本地主机上的USB Web服务器使用SQL数据库。 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. 使用setByte()代替。

because TINYINT in SQL is equal to byte in java , also it has some methods, like: setByte() and updateByte() and getByte() 因为SQL中的 TINYINT 等于java中的byte ,所以它还有一些方法,例如: setByte()updateByte()以及getByte()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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