SQLite很忙,数据库已锁定

时间:2017-02-20 15:49:26

标签: java sqlite

我需要帮助,我正在尝试在现有数据库中插入一些内容,但我无法理解。我得到数据库文件被锁定(数据库被锁定)。我试过关闭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();
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 @CL。是的,在后台运行了其他连接对象。 问题是我在登录应用程序时没有关闭prepareStatement和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;
    }