java.sql.SQLException:[SQLITE_BUSY]数据库文件已锁定

时间:2015-03-13 01:58:11

标签: java sqlite database-connection

我在Eclipse中使用SQLite数据库,但我遇到了SQLITE_BUSY错误。我已经阅读了这个问题2天了,我尝试了不同的东西,比如每次关闭PreparedStatement-s和Resultset-s或关闭连接。即使我关闭连接并打开一个新连接,我也会收到此错误。下面是我得到错误的代码。在这个类中有几个查询。正在执行的第一个工作正常,但是当涉及到最后一个(我的意思是最后一个被执行)时,应用程序崩溃了。我不明白为什么会这样。我已经看到了这个问题的几乎所有帖子,但我仍然无法修复它。谁能帮帮我吗?我真的需要让它发挥作用。先感谢您!

我的代码:

 //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 <String, Integer>();

    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<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<Integer,Integer>();
        mapTableProductQuantity=new HashMap<Integer,Integer>();
        mapDBProductID = new HashMap<Integer,Integer>();
        mapDBProductQuantity = new HashMap<Integer,Integer>();

        setOrderID();
        int totalRows=table.getRowCount();
        int i=0;
        for(int row=0; row<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>=totalRows){
            System.out.println("Reshti 167");
            int index1=0;
            while(index1<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<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>=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<totalRows){
            int index1=0;
            while(index1<totalRows){
                int index2=0;
                boolean found=false;
                while((index2<i)&&(!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>=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);
    }
}

0 个答案:

没有答案