值在mySql表的错误列中更新

时间:2014-08-08 06:01:21

标签: java mysql jsp

以下是我在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原始值,而不是修改它! 可能出现什么问题!

1 个答案:

答案 0 :(得分:3)

使用变量时出现错误:

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();

删除第二个陈述。

相关问题