sqlite没有使用java更新表/数据库忙[锁定]

时间:2015-07-25 10:36:13

标签: java sqlite

我正在使用java和sqlite作为应用程序。当我尝试更新表时工作正常,但是当我尝试在第一个表之后直接更新第二个表时,sqlite告诉我正忙[锁定] .I搜索解决方案,但无法找到。我正在关闭所有Resultsets,PreparedStatements和第一次更新的连接,然后启动第二次更新,但它不起作用。任何实际的解决方案或导致此错误的原因?

    String sql2 = "UPDATE wages SET EPIDOMAADEIAS = ?,FMYEPIDOMATOSADEIAS = ?,ERGODOTHSEPIDOMAADEIAS = ?,ERGAZOMENOSEPIDOMAADEIAS = ? WHERE SURNAME = ?";


                if(kathgoria.equals("ΥΠΑΛΛΗΛΟΣ")){
                    model.addRow(new String[]{surname,etairia,kathgoria,ep,erg,ergod,fmyS,syn});
                     ps = connection.prepareStatement(sql2);
                     try {
                        ps.setString(1,epS);
                        ps.setString(2, fmySS);
                        ps.setString(3, ergodS);
                        ps.setString(4, ergS);
                        ps.setString(5, surname);
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    int reS1 = 0;
                    try {
                        reS1 = ps.executeUpdate();
                        ps.close();
                        connection.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }   
                }

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题,阅读的任何解决方案(关闭连接、关闭结果集等)都对我不起作用。唯一真正对我有帮助的字体是遵循如下事务架构:

  1. 创建连接

    private Connection connect() {
        // SQLite connection string
        String url = "jdbc:sqlite:C://sqlite/db/test.db";
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return conn;
    }

  1. 声明:您需要的 n 个字符串句子、1 个结果集、1 个连接和 n 个准备好的语句,就像您在交易中想要做的那样。示例:
    // SQL for creating a new material
    String sqlMaterial = "INSERT INTO materials(description) VALUES(?)";
            
    // SQL for posting inventory
    String sqlInventory = "INSERT INTO inventory(warehouse_id,material_id,qty)"
                    + "VALUES(?,?,?)";

    ResultSet rs = null;
    Connection conn = null;
    PreparedStatement pstmt1 = null, pstmt2 = null;

(*) 如果您的交易有多个句子,您也可以使用列表 (pstmt[])。

  1. 打开一个 try/catch/finally 块并按如下方式实施您的交易:
        try {
            // connect to the database
            conn = this.connect();
            if(conn == null)
                return;
            
            // set auto-commit mode to false
            conn.setAutoCommit(false);
            
            // 1. insert a new material
            pstmt1 = conn.prepareStatement(sqlMaterial,
                    Statement.RETURN_GENERATED_KEYS);

            pstmt1.setString(1, material);
            int rowAffected = pstmt1.executeUpdate();

            // get the material id
            rs = pstmt1.getGeneratedKeys();
            int materialId = 0;
            if (rs.next()) {
                materialId = rs.getInt(1);
            }

            if (rowAffected != 1) {
                conn.rollback();
            }
            // 2. insert the inventory
            pstmt2 = conn.prepareStatement(sqlInventory);
            pstmt2.setInt(1, warehouseId);
            pstmt2.setInt(2, materialId);
            pstmt2.setDouble(3, qty);
            // 
            pstmt2.executeUpdate();
            // commit work
            conn.commit();

        } catch (SQLException e1) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException e2) {
                System.out.println(e2.getMessage());
            }
            System.out.println(e1.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pstmt1 != null) {
                    pstmt1.close();
                }
                if (pstmt2 != null) {
                    pstmt2.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e3) {
                System.out.println(e3.getMessage());
            }
        }

示例摘自 - https://www.sqlitetutorial.net/sqlite-java/transaction/ .

请注意,此示例还有助于了解如果任何事情都不起作用,并且不会在数据库中产生不一致,您如何能够回滚事务。