【问题标题】:SQLite is busy, Database is lockedSQLite 很忙,数据库被锁定
【发布时间】:2017-02-21 20:41:21
【问题描述】:

我需要帮助,我正在尝试向现有数据库中插入一些内容,但无法启动。我得到数据库文件被锁定(数据库被锁定)。我试过关闭preparedStatement,但没有运气。我用 executeUpdate() 代替了 executeQuery(),没有帮助。

public class tableController {
Connection connection;

@FXML
private TextField txtId;

@FXML
private TextField txtName;

@FXML
private TextField txtLastName;

@FXML
private TextField txtAge;

@FXML
private TextField txtUsername;

@FXML
private TextField txtPassword;

@FXML
private Button save;


public tableController() {
    connection = SQLiteConnection.connector();
    if(connection == null) {
        System.out.println("Test");
        System.exit(1);
    }
}

public boolean isDBConnected() {
    try {
        return !connection.isClosed();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
}

public void Insert(ActionEvent event) throws SQLException {

    PreparedStatement preparedStatement = null;
    ResultSet resultSet;
    String query = "insert into employee (id, name, lastName, age, username, password) values (?,?,?,?,?,?)";

    try {
        preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1, txtId.getText());
        preparedStatement.setString(2, txtName.getText());
        preparedStatement.setString(3, txtLastName.getText());
        preparedStatement.setString(4, txtAge.getText());
        preparedStatement.setString(5, txtUsername.getText());
        preparedStatement.setString(6, txtPassword.getText());

        Alert aleret = new Alert(AlertType.INFORMATION);
        aleret.setTitle("Information dialog");
        aleret.setHeaderText(null);
        aleret.setContentText("User has been created");
        aleret.showAndWait();


        preparedStatement.executeUpdate();


    } catch (Exception e) {
        // TODO: handle exception
        JOptionPane.showMessageDialog(null, e);
        e.printStackTrace();
    } finally {
        if(preparedStatement != null) {
            preparedStatement.close();
        }
    }
}

}

【问题讨论】:

  • 其他一些连接(在这个或另一个程序中)有一个活动事务。
  • @CL。所有其他项目都已关闭,后台没有运行任何内容。我该如何解决这个问题?
  • 尝试复制 .db 文件,然后将该副本用于您的程序,看看会发生什么
  • 那么它可能是你自己程序中的某个(其他)连接对象。
  • @MilanMarkovic 谢谢,但没有运气。还是一样的错误。

标签: java sqlite


【解决方案1】:

我找到了解决方案。 @CL。没错,后台运行着其他连接对象。 问题是我在登录应用程序时没有关闭preparedStatement 和resultSet。

    PreparedStatement preparedStatement;
    ResultSet resultSet;
    String query = "select * from employee where username = ? and password = ?";

    try {
        preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1, user);
        preparedStatement.setString(2, pass);

        resultSet = preparedStatement.executeQuery();


        if (resultSet.next()) {
            preparedStatement.close(); // after closing this, it works
            resultSet.close();         // closed this one also
            return true;
        } else {
            return false;
        }


    } catch (Exception e) {
        // TODO: handle exception
        return false;
    }

【讨论】:

    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 2015-05-11
    • 2011-02-14
    • 1970-01-01
    相关资源
    最近更新 更多