java - prepareStatement.executeUpdate(sql) 给出错误
问题描述
executeUpdate(sql)
当我尝试使用准备好的语句更新 SQL 数据库时,我不知道为什么我的代码在错误。当我尝试在 Oracle SQL 终端中运行 SQL 查询时,它工作得很好。谁能帮帮我吗。我在下面添加了代码。
我试图调试,它显示错误
int iRs = cimStmt.executeUpdate(sql.toString());
public String updatePatternDiffRemarks(String sLot,
String sRemarks,
String sUserId,
int iLangMode)
{
StringBuffer sql = null;
Connection con = null;
PreparedStatement stmt = null;
StringBuffer rtnMsg = new StringBuffer("");
try {
con = CimDbConnect.getDbConnection();
if (con == null) {
throw new Exception("Failed DB connect.");
}
con.setAutoCommit(false);
sql = new StringBuffer();
sql.append(" UPDATE ");
sql.append(" CIM.PRODUCT_BPM_DIFF_COMMENT_TBL ");
sql.append(" SET REMARKS = ? ");
sql.append(" , LAST_MODIFIED_DATE = sysdate ");
sql.append(" , USER_ID = ? ");
sql.append(" WHERE ");
sql.append(" LOT_NO = ? ");
stmt = con.prepareStatement(sql.toString());
CimPreparedStatement cimStmt = new CimPreparedStatement(stmt);
int index = 1;
cimStmt.setString(index++, sRemarks);
cimStmt.setString(index++, sUserId);
cimStmt.setString(index++, sLot);
int iRs = cimStmt.executeUpdate(sql.toString());
con.commit();
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
rtnMsg = CimMessage.setMsgStyleCForHtml(rtnMsg, e.getMessage(), false);
CimMessage.putErrMsgEx(e, this.getClass().getName() + ": updatePatternDiffRemarks Exception!!");
if(con != null) {
try {
con.rollback();
} catch(Exception ex) {
ex.printStackTrace();
}
}
} finally {
try {
if(stmt != null) {
stmt.close();
}
if(con != null) {
con.close();
}
} catch(Exception e) {
CimMessage.putErrMsgEx(e, this.getClass().getName() + ": updatePatternDiffRemarks Exception!!");
}
}
return rtnMsg.toString();
}
解决方案
你可以尝试像这个例子一样使用分隔:
public void closeConn(PreparedStatement ps) throws SQLException {
if (ps != null) {
ps.close();
}
}
public void updateService(Servs srv){
String sql = "UPDATE services SET name=?, description =? , content = ? , img = ? WHERE id = ? ";
PreparedStatement ps = null;
try{
ps = super.prepareStatement(sql);
ps.setString(1, srv.getName());
ps.setString(2, srv.getDescription());
ps.setString(3, srv.getContent());
ps.setString(4, srv.getImg());
ps.setInt(5, srv.getId());
ps.execute();
}catch(SQLException|NullPointerException e){
logger.error("Error UPDATE >> ",e);
}
finally {
closeCn(ps);
}
}// CRUD UPDATE
推荐阅读
- node.js - .findOneAndUpdate() 删除数组中的所有元素
- mongodb - MongoDB如何计算子文档总数大于3且子文档值之和大于15000的地方
- python - 如何从python中的文本文件中仅获取日期
- python - numpy polyfit 没有根据卡方给出最佳线性拟合
- apache-kafka - 如何从 Kafka 全局状态存储中删除记录?
- scala - 如何在集成测试和单元测试之间共享代码
- xslt - 如何组相邻 - XSLT
- node.js - 为什么以下代码中没有执行路由?
- angular - 无法使用 Primeng 的 p-fileUpload 上传视频
- windows - Ansible - vmware_guest - 使用“自动登录”时回答或跳过横幅(Windows Server)