当客户从数据库中删除时,必须将其状态设置为离线(而不是实际从数据库中删除客户)。在我的数据库中,我使用 tinyint(1) 将状态设置为 0(非 Activity )或 1( Activity )。现在我该如何编码呢?
// 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);
}
}
我通过本地主机上的 USB 网络服务器使用 SQL 数据库。我不确定这现在是否有效,因为我的其余代码不完整并且我无法测试,但在继续之前我必须确定。谢谢
最佳答案
改用setByte()
。
因为 SQL is equal to byte in java 中的 TINYINT ,它还有一些方法,例如:setByte()
和updateByte()
和getByte()
关于java - 使用java更改mysql数据库中的tinyint(1)数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420270/