private int isStocksEnough(int goodsNum,int goodsID) {
while(true) {
sql = "select * from tb_goodsmanage where goodsID = ?";
conn = DBUtil.getconn();
// getconn()属于静态方法,只加载一次,即只执行一次
try {
ps = conn.prepareStatement(sql);
System.out.println(ps);
ps.setInt(1, goodsID);
rs = ps.executeQuery();
System.out.println(rs);
if(rs.next()) {
int a = rs.getInt("goodsNum");
if(goodsNum>a ) {
System.out.println("商品库存不足,仅剩:"+rs.getInt("goodsNum"));
System.out.println("请重新选择商品数量");
goodsNum = input.nextInt();
isStocksEnough(goodsNum,goodsID);
break;
}
//不加else时,当库存满足时,会跳出if(goodsNum>a)代码块不执行,会重复上边的预编译SQL和SQL相关操作,但由于这些操作控制台没有显示输出,所以不易发现该死循环!1
// 但是静态方法getcoonn();只加载执行一次。当第二次是回去方法区查看有就直接调用。
// else{
// return goodsNum;
// }
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error:系统错误");
}
}
return goodsNum;
}