2017-02-20 20 views
0

私は助けが必要です、私は既存のデータベースに何かを挿入しようとしていますが、それを得ることができません。取得するデータベースファイルがロックされています(データベースがロックされています)。私はpreparedStatementを閉じようとしましたが、運はありません。私はexecuteUpdate()の代わりにexecuteQuery()を使用しました。SQLiteがビジー状態です、データベースがロックされています

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

@ CL。他のプロジェクトはすべて閉じられ、バックグラウンドでは何も実行されていません。どうすれば解決できますか? – szentmihaly

+0

.dbファイルをコピーして、そのコピーをプログラムに使用して、何が起こるかを確認してください。 –

答えて

0

私は解決策を発見しました。 @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; 
    } 
関連する問題