gpt4 book ai didi

java - 在 mySql 表的错误列中更新值

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:44 25 4
gpt4 key购买 nike

以下是我在jsp页面中的代码:AdminModifyGrocery3.jsp:

int g_id = session.getAttribute("g_id") != null ? ((Integer) session.getAttribute("g_id")).intValue() : 0 ;
String g_name=(String)request.getParameter("g_name");
String g_price_string=(String)request.getParameter("g_price");
int g_price = Integer.parseInt(g_price_string);

PreparedStatement stmtUpdate = null;
String strUpdate = "update ShopSystem.Grocery set g_name= ? where g_id = ?";
stmtUpdate = con.prepareStatement(strUpdate);
stmtUpdate.setString(1,g_name);
stmtUpdate.setInt(2,g_id);
int c2 = stmtUpdate.executeUpdate();

PreparedStatement stmtUpdate1 = null;
String strUpdate1 = "update ShopSystem.Grocery set g_price=? where g_id=?";
stmtUpdate1 = con.prepareStatement(strUpdate);
stmtUpdate1.setInt(1,g_price);
stmtUpdate1.setInt(2,g_id);
int c1 = stmtUpdate1.executeUpdate();

if(c2>0 && c1>0)
{
%>
<br> Data is modified successfully.<br>
<%
}
else
{
%>
<br> Sorry the action cannot be completed.<br>
<%
}

显示“数据修改成功”信息。但是在实际的数据库中是将g_price的值设置为g_name,并且是保留g_price的原始值而不是修改它!可能出了什么问题!

最佳答案

此处使用变量时出现错误:

String strUpdate1 = "update ShopSystem.Grocery set g_price=? where g_id=?";
stmtUpdate1 = con.prepareStatement(strUpdate);
^^^^^^
stmtUpdate1.setInt(1,g_price);
stmtUpdate1.setInt(2,g_id);

你必须在标记的位置使用strUpdate1

或者你可以使用 on 语句:

String strUpdate = "update ShopSystem.Grocery set g_name= ?, g_price=?   where g_id = ?";
stmtUpdate = con.prepareStatement(strUpdate);
stmtUpdate.setString(1,g_name);
stmtUpdate.setString(2,g_price);
stmtUpdate.setInt(3,g_id);
int c2 = stmtUpdate.executeUpdate();

并删除第二条语句。

关于java - 在 mySql 表的错误列中更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25196926/

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