SOS ! 关于statement的问题?45分,我能给的最大分数??大虾救命

luoql 2002-01-01 12:35:02
stmt = con.createStatement();
stmt.execute("INSERT INTO myTable ( id,number,prod_id,quantity,price,unit,iDate) VALUES('"+theId+"','"+theNumber+"','"+theProd+"','"+theQuantity+"','"+thePrice+"','"+theUnit+"','"+sTheDate+"')");
pstmt=con.prepareStatement("select * from products where prod_id = ?");
rs = stmt.executeQuery("select * from products where prod_id = "+theProd);
pstmt.setString(1,theProd);
rs = pstmt.executeQuery();
rs.next() ;
float currentStock = rs.getFloat("stock");
currentStock = currentStock + theQuantity;
pstmt1= con.prepareStatement("UPDATE products set stock = "+currentStock+" where prod_id= ?");
pstmt1.setString(1,theProd);
pstmt1.executeUpdate();
con.commit();

我上面的代码出现连接占线错误,我调试了一下,是因为有两个prepareStatement 对同一个表操作的原因,但我必须先从products表中取出currentStock,更新后又写入到products表中,而且theProd又是字符串的参数,我怎么搞定它,大虾帮帮忙!!
...全文
107 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
luo810 2002-01-02
  • 打赏
  • 举报
回复
我的这个问题关键是连接占线,我上面的怎样改才不占线?
czb 2002-01-02
  • 打赏
  • 举报
回复
如果prod_id字段是字符型、theProd也是字符型的话,
stmt.executeQuery("select * from products where prod_id = '" + theProd + "'");
如果prod_id字段是整型、theProd是字符型的话,
stmt.executeQuery("select * from products where prod_id = " + Integer.parse(theProd));//不知道parse有没有拼错哦
czb 2002-01-01
  • 打赏
  • 举报
回复
把后面的更新语句换成:
pstmt=con.prepareStatement("UPDATE products set stock = "+currentStock+" where prod_id= ?");
pstmt.setString(1,theProd);
pstmt.executeUpdate();
luo810 2002-01-01
  • 打赏
  • 举报
回复
不用prepareStatement 那应当怎样传参数,
stmt.executeQuery("select * from products where prod_id = "+theProd);
这句语句有错误,说是不能将字符串转环为整数,用statement,我该怎样写???

xiangqian 2002-01-01
  • 打赏
  • 举报
回复
字符参数不一定要用prepareStatement,但需要自己判断一下null
我一般不用prepareStatement,它会预留分配内存,我不喜欢
luoql 2002-01-01
  • 打赏
  • 举报
回复
大虾关注一下,我还没有搞定亚
luoql 2002-01-01
  • 打赏
  • 举报
回复
是不是字符串参数一定要用prepareStatement传呀???
luoql 2002-01-01
  • 打赏
  • 举报
回复
to:czb(草中宝)是一样的,我开始是pstmt
czb 2002-01-01
  • 打赏
  • 举报
回复
如果你的数据库支持JDBC2.0的话,里边有个可更新的结果集,操作起来比较方便。

23,405

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧