我正在使用sqlite数据库在java中创建餐馆管理应用程序。在按下订单确认按钮的应用程序中,应用程序应插入订单表,插入订单菜单/项目并减少在订单中添加的菜肴中使用的每种成分的数量。 当我尝试使用代码时,它会显示“SQLExcption:数据库被锁定”
for (int i = 0; i < orderTable.getRowCount(); i++) {
pst = con.prepareStatement("select dishId from dishes where dishName = '" + orderTable.getValueAt(i, 1) + "'");
rs = pst.executeQuery();
int dishId = rs.getInt("dishId");
pst = con.prepareStatement("insert into orderDishes values(?,?,?)");
pst.setInt(1, currentOrderNumber);
pst.setInt(2, dishId);
pst.setInt(3, (int) orderTable.getValueAt(i, 0));
pst.executeUpdate();
pst = con.prepareStatement("select ingId , quantity from dishIng where dishId = '" + dishId + "'");
rs = pst.executeQuery();
while (rs.next()) {
ingReducedQuantity = rs.getInt("quantity");
ingId = rs.getInt("ingId");
pst1 = con1.prepareStatement("select qunatity from ingriedients where ingId = '" + ingId + "'");
rs1 = pst1.executeQuery();
previousQuantity = rs1.getInt("qunatity");
pst1.close();
rs1.close();
con1.commit();
newQuantity = previousQuantity - ingReducedQuantity;
// Here it gives exception
pst1 = con1.prepareStatement("update ingriedients set qunatity = '" + newQuantity + "' where ingId = '" + ingId + "'");
pst1.executeUpdate();
con1.commit();
}
}
我认为异常是在更新ingriedients表之前进行循环但是我不知道为什么..plz建议一些解决方案..
答案 0 :(得分:0)
在每个连接和语句之前,您需要关闭任何私有连接和语句 因为 sqlite 不支持多个活动事务,所以在代码中的第一个 con1.prepareStatement (在while循环中)之前,还要关闭其他先前的连接和语句,我的意思是 pst , con 等