【发布时间】: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 谢谢,但没有运气。还是一样的错误。