title: java.sql.SQLException: [SQLITE_BUSY] The database file is locked
body: <p>I am working in Eclipse with SQLite database, but I am facing the SQLITE_BUSY error. I've been reading for this problem for 2 days, and I have tried different things, such as closing PreparedStatement-s and Resultset-s every time or closing the connection. Even when i close a connection and open a new one, I get this error. Below is the code where i get the error. In this class there are several queries. The first ones being executed work fine, but when it comes to the last ones ( i mean the last being executed), the application crashes. I don't get it why this happens. I have seen almost all the posts for this problem, but still i couldn't fix it. Can anyone please help me? I really need to make it work. Thank you in advance! </p>
  
  <p>My code:</p>
  
  <pre><code> //WHEN THIS METHOD IS CALLED I GET THE EXCEPTION
  private void update(int a){
  
      int index2=a;
  try{String queryUpdate="Update Open_Orders set Quantity='"+mapTableProductQuantity.get(index2)+"' where Order_ID='"+orderID +"' and Table_ID='"+id_tavoline+"' and Product_ID='"+mapTableProductID.get(index2)+"'" ;
  PreparedStatement pstUpdate = connection.prepareStatement(queryUpdate);
  JOptionPane.showMessageDialog(null, "pst u be");
  pstUpdate.execute();
  JOptionPane.showMessageDialog(null, "u ekzekutua");
  pstUpdate.close();
  }catch(Exception e){
      JOptionPane.showMessageDialog(null, e);
  }
  }
  private void updateDatabase()
  
  {   mapName_ID= new HashMap &lt;String, Integer&gt;();
  
      try{String query="Select Product_Name, Product_ID from Products";
      PreparedStatement pst= connection.prepareStatement(query);
      ResultSet rsp= pst.executeQuery();
      //int no=0;
      while(rsp.next()){
           mapName_ID.put(rsp.getString("Product_Name"), rsp.getInt("Product_ID"));
  
      }
      pst.close();
      rsp.close();
      }catch(Exception e){
  
      }
      if(busy==0)
      {
          try{String query="insert into Order_Table (Table_ID) values (?)";
           //System.out.println("busy eshte "+busy);
          PreparedStatement pstInsert = connection.prepareStatement(query);
           pstInsert.setInt(1, id_tavoline);
           pstInsert.execute();
           pstInsert.close();}
          catch(Exception e1){
              JOptionPane.showMessageDialog(null, "e1 = " +e1);
          }
           setOrderID();
              int totalRows=table.getRowCount();
              for(int row=0; row&lt;totalRows;row++)
              {
                  try{
                      String prName=table.getModel().getValueAt(row, 0).toString();
                      int quantity = Integer.parseInt(table.getModel().getValueAt(row, 1).toString());
                  String queryInsert="Insert into Open_Orders (Order_ID, Table_ID, Product_ID, Quantity) values (?,?,?,?)";
                  PreparedStatement pstInsert1 = connection.prepareStatement(queryInsert);
                  pstInsert1.setInt(1, orderID);
                  pstInsert1.setInt(2, id_tavoline);
                  pstInsert1.setInt(3, mapName_ID.get(prName));
                  pstInsert1.setInt(3, quantity);
                  pstInsert1.execute();
                   pstInsert1.close();
                  //,id_tavoline, mapName_ID.get(""));
                  }catch(Exception e ){
                      JOptionPane.showMessageDialog(null, e);
                  }
  
              }
  
      }
      else {
          mapTableProductID= new HashMap&lt;Integer,Integer&gt;();
          mapTableProductQuantity=new HashMap&lt;Integer,Integer&gt;();
          mapDBProductID = new HashMap&lt;Integer,Integer&gt;();
          mapDBProductQuantity = new HashMap&lt;Integer,Integer&gt;();
  
          setOrderID();
          int totalRows=table.getRowCount();
          int i=0;
          for(int row=0; row&lt;totalRows;row++)
          {
              String name=table.getModel().getValueAt(row, 0).toString();
              System.out.println("name= " +name);
              mapTableProductID.put(row, mapName_ID.get(name));
              System.out.println(" mapName_ID.get(name)= " + mapName_ID.get(name));
              System.out.println("mapTableProductID.get(row) = " +mapTableProductID.get(row));
              mapTableProductQuantity.put(row, Integer.parseInt(table.getModel().getValueAt(row, 1).toString()));
  
          }
          try{connection= sqliteConnection.dbConnector();
              String querySelect= "select Product_ID, Quantity from Open_Orders where Order_ID='"+orderID+"' and Table_ID='"+id_tavoline+"'";
              PreparedStatement pstSelect = connection.prepareStatement(querySelect);
              ResultSet rs=pstSelect.executeQuery();
  
              while(rs.next()){
                  mapDBProductID.put(i, rs.getInt("Product_ID"));
  
                  mapDBProductQuantity.put(i, rs.getInt("Quantity"));
                  i++;
              }
              pstSelect.close();
              rs.close();
              connection.close();
          }catch(Exception e2){
              JOptionPane.showMessageDialog(null, e2);
          }
          System.out.println("ne db ka i = " +i);
          System.out.println("ne tabele ka  = " +totalRows);
          if(i&gt;=totalRows){
              System.out.println("Reshti 167");
              int index1=0;
              while(index1&lt;i){
                  int index2=0;
                  //boolean found=false;
                  System.out.println("mapDBProductID.get(index1) = " +mapDBProductID.get(index1));
                  System.out.println("mapTableProductID.get(index2) = " +mapTableProductID.get(index2));
                  while((index2&lt;totalRows)){
                      if(mapDBProductID.get(index1)!=mapTableProductID.get(index2)){
                          index2++;
                      }
                      else{//found=true;
                      System.out.println("Reshti 177");
                          if(mapDBProductQuantity.get(index1)!=mapTableProductQuantity.get(index2)){
  
                                  System.out.println("Reshti 180");
                                  System.out.println(orderID);
                                  connection=sqliteConnection.dbConnector();
                                  update(index2);
  
  
                          }else{
  
                          }
                          index1++;
  
                          break;
                      }
  
  
  
  
                  }
  
                  if(index2&gt;=totalRows){
                      try{
  
                          String queryDelete="Delete from Open_Orders  where Order_ID='"+orderID +"' and Table_ID='"+id_tavoline+"' and Product_ID='"+mapDBProductID.get(index1)+"'" ;
                          PreparedStatement pstDelete = connection.prepareStatement(queryDelete);
                          pstDelete.execute();
                          pstDelete.close();
  
                          }catch(Exception e4){
  
                              JOptionPane.showMessageDialog(null, "e4 = " +e4);
                          }
                      index1++;
                  }
  
              }
          }
  
          if(i&lt;totalRows){
              int index1=0;
              while(index1&lt;totalRows){
                  int index2=0;
                  boolean found=false;
                  while((index2&lt;i)&amp;&amp;(!found)){
                      if(mapTableProductID.get(index1)!=mapDBProductID.get(index2)){
                          index2++;
                      }else{found=true;
                          if(mapTableProductQuantity.get(index1)!=mapDBProductQuantity.get(index2)){
                              try{
                                  String queryUpdate= "Update Open_Orders set Quantity='"+mapTableProductQuantity.get(index1)+"' where Order_ID='"+orderID +"' and Table_ID='"+id_tavoline+"' and Product_ID='"+mapTableProductID.get(index1)+"'" ;
                                  PreparedStatement pstUpdate = connection.prepareStatement(queryUpdate);
                                  pstUpdate.execute();
                                  pstUpdate.close();
  
                              }catch(Exception e5){
                                  JOptionPane.showMessageDialog(null, "e5 = " +e5);
                              }
                          }
                          break;
                      }
                      index2++;
                  }
                  if(index2&gt;=i){
                      try {
  
                          String queryInsert="Insert into Open_Orders (Order_ID, Table_ID, Product_ID, Quantity) values (?,?,?,?)" ;
  
                          PreparedStatement pstInsert= connection.prepareStatement(queryInsert);
                          pstInsert.setInt(1, orderID);
                          pstInsert.setInt(2, id_tavoline);
                          pstInsert.setInt(3, mapTableProductID.get(index1));
                          pstInsert.setInt(1,mapTableProductQuantity.get(index1));
                          pstInsert.execute();
                          pstInsert.close();
  
                          }catch(Exception e10){
  
                              JOptionPane.showMessageDialog(null, "e10 = " +e10);
                          }
                  }
                  index1++;
              }
          }
  
  
      }
  
  
  }
  private void setOrderID(){
      try{
          String query="select Order_ID from Order_Table where Table_ID='" + id_tavoline+"'";
          PreparedStatement pst = connection.prepareStatement(query);
          ResultSet rs=pst.executeQuery();
          rs.next();
          orderID=rs.getInt("Order_ID");
          pst.close();
          rs.close();
          connection.close();
      }
      catch(Exception e)
      {
          JOptionPane.showMessageDialog(null, "setOrderID = "+e);
      }
  }
  </code></pre>