虽然都是调用executeUpdate方法,但是Commons-dbutils封装的update仅用来做update、delete操作,不可以用来insert。以下是错误示范
public int insert(String userId, String address, Date added, String receiver, String receiverPhone) {
int rows = 0;
try {
rows = DBUtil.RUNNER.update(
"insert into tb_address(user_id,address,added,receiver,receiver_phone) values (?,?,?,?,?)",
new Object[]{userId, address, new java.sql.Timestamp(added.getTime()), receiver, receiverPhone});
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}
之后又调用了Commons-dbutils封装的insert方法,但是发现参数还需要传入一个Handler(可能是顺便在后台显示插入的数据,看了看底层,暂时看不懂)因此作罢。最后调用了Commons-dbutils封装的execute方法,问题解决。修改错误示范后如下:
public int insert(String userId, String address, Date added, String receiver, String receiverPhone) {
int rows = 0;
try {
rows = DBUtil.RUNNER.execute(
"insert into tb_address(user_id,address,added,receiver,receiver_phone) values (?,?,?,?,?)",
new Object[]{userId, address, new java.sql.Timestamp(added.getTime()), receiver, receiverPhone});
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}