gpt4 book ai didi

java - prepareStatement.executeUpdate(sql) 给出错误

转载 作者:行者123 更新时间:2023-12-01 16:20:38 24 4
gpt4 key购买 nike

当我尝试使用准备好的语句更新 SQL 数据库时,我不知道为什么我的代码在 executeUpdate(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

关于java - prepareStatement.executeUpdate(sql) 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62294226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com