0

在 mysql 中,我有一个 INVENTORY 表,在该表中,我num_of_items在 gui 文本字段中添加了用户给出的,之前num_of_items存在于 mysql 中。我在我的java代码中写了一个查询如下..但它没有更新我的INVENTORY表..

try {
    st2 = conn.createStatement();
    rs=st2.executeQuery("select order_num,prod_name_in_order,num_of_items_in_order,cost_order from ORDER_DUP where order_num='"+ord_num+"'");
    int column=rs.getMetaData().getColumnCount();

    int j=0;
    while(rs.next())
    {
        for(int i=1;i<=column;i++)
        {
            System.out.print(rs.getObject(i)+"\t");
            product_name1 = (String) rs.getObject(2);
            x1 = rs.getInt(3);
            //int x=Integer.parseInt(num_of_items.trim());

        }
        product_name[j]=product_name1;
        x[j]=x1;
              j++;
        System.out.println();
    }
    for(int k=0;k<j;k++)
        st2.executeUpdate("update INVENTORY set num_of_items=num_of_items+'"+x[k]+"' where product_name='"+product_name[k]+"'"); //st2.close();
    //conn.close();
    System.out.printf("invy");

}
catch (SQLException e) {
    System.out.printf("error in updating inventory table(YES)");
    e.printStackTrace();
}

请帮帮我..我没有弄错!谢谢。

4

2 回答 2

0

首先,缩小问题的根本原因,您是否尝试打印出最终语句字符串并在数据库中运行命令?

System.out.println("update INVENTORY set num_of_items=num_of_items+'"+x[k]+"' where product_name='"+product_name[k]+"'");
于 2012-12-21T20:53:15.337 回答
0

不要用单引号将您插入的变量值括起来x[k]

你的查询应该如下:

"update INVENTORY set num_of_items=num_of_items+"+x[k]+" where product_name='"+product_name[k]+"'"
于 2012-12-21T20:34:49.377 回答