簡體   English   中英

SQLite忙,數據庫已鎖定

[英]SQLite is busy, Database is locked

我需要幫助,我正在嘗試向現有數據庫中插入某些內容,但無法繼續進行。 我得到數據庫文件被鎖定(數據庫被鎖定)。 我試圖關閉prepareStatement,但是沒有運氣。 我用了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。 是的,后台還有其他連接對象在運行。 問題是登錄到應用程序時我沒有關閉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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM