2014-12-07 75 views
-1

我使用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表,但我不前循環,知道why..plz提出了一些解決辦法..

+0

你有這樣的錯字:從...選擇qunatity你也拼寫'成分',但你一直這樣做,所以也許這不是問題。 – 2014-12-07 05:40:49

+0

其實列名是qunatity所以它不是一個錯字..也沒有語法錯誤它是某種連接問題.. – 2014-12-07 05:47:07

回答

0

每個連接和語句之前您需要關閉任何隱私連接和聲明 ,因爲sqlite不支持m礦石比一個活躍的交易,所以在你的代碼中的第con1.prepareStatement(在while循環),關閉其他以前的連接和語句之前還,我的意思是PSTCON

+0

「sqlite不支持多個連接」這個事實雖然伎倆,雖然我沒有在每個新語句前關閉其他連接。 – 2014-12-07 09:37:02

+0

SQLite實際上確實支持多個連接;當您嘗試寫入時,它不允許多個活動事務。 – 2014-12-07 10:03:49