org.sqlite.SQLiteException: [SQLITE_NOTADB] 打开的文件不是数据库文件(文件已加密或不是数据库)[NETBEANS]

org.sqlite.SQLiteException: [SQLITE_NOTADB] File opened that is not a database file (file is encrypted or is not a database) [NETBEANS]

所以我得到了我正在使用的这个表格,但是每当我提交时我都会收到那个错误,它说连接成功但是然后它 returns 错误并且永远不会插入也不会从数据库中检索任何东西。我检查了 sqlite 的版本和所有内容,无法弄清楚。

public class 数据库连接 {

public static Connection connection = null;

public static Connection getConnection() throws ClassNotFoundException {
    try {
        System.out.println("CONNECTING");
        Class.forName("org.sqlite.JDBC");
        connection = DriverManager.getConnection("jdbc:sqlite:SoftwareDB.db");
        System.out.println("CONNECTION SUCCESSFUL");
    } catch (SQLException e) {
        System.out.println("ERROR: Connection Failed!");
    }
    return connection;
}

public static void login(String username, String password, String login) throws ClassNotFoundException {
    try {
        System.out.println("INSERTING");
        try (Statement stmt = getConnection().createStatement()) {
            String sql = "INSERT INTO login (username, password) VALUES ('" + username + "', '" + password + "', '" + login + "');";
            stmt.execute(sql);
        }
        getConnection().close();
        System.out.println("INSERT SUCCESSFUL");
    } catch (SQLException ex) {
        Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static ResultSet getLoginDetails(String query) throws SQLException, ClassNotFoundException {
    ResultSet rs;
    try (PreparedStatement ps = getConnection().prepareStatement(query)) {
        rs = ps.executeQuery();
        ps.close();
        getConnection().close();
    }
    return rs;
}

public static ResultSet getExistentDetails(String query) throws SQLException, ClassNotFoundException {
    ResultSet rs;
    try (PreparedStatement ps = getConnection().prepareStatement(query)) {
        rs = ps.executeQuery();
        getConnection().close();
    }
    return rs;
}

}

private void loginBtnMouseClicked(java.awt.event.MouseEvent evt) {                                      

    if (username.getText().isEmpty() || password.getText().isEmpty()) {
        infoLabel.setVisible(true);
        username.setText("");
        password.setText("");
    } else {
        try {
            
            databaseConnection.getLoginDetails("SELECT * FROM register WHERE email = '?' AND password = '?'");
            String ts = new SimpleDateFormat("dd.MM.yyyy - HH.mm.ss").format(System.currentTimeMillis());
            databaseConnection.login(username.getText(), password.getText(), ts);
            JOptionPane.showMessageDialog(null, "Login succesful!");
            new login().setVisible(true);

            infoLabel.setVisible(true);
            username.setText("");
            password.setText("");
        } catch (HeadlessException ex) {
            JOptionPane.showMessageDialog(null, "Failed!");
        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}     

Output

我相信您忘记了一件重要的事情:正确准备 PreparedStatement 和 opening/closing 连接。

您会尝试以下重写的 getLoginDetails() 方法并从中获取灵感用于其他方法吗?

public static ResultSet getLoginDetails(String query, String email, String password) throws SQLException, ClassNotFoundException {
    ResultSet rs;
    try (Connection conn = getConnection()) {   
        try (PreparedStatement ps = conn.prepareStatement(query)) {
            ps.setString(1,email);
            ps.SetString(2,password);
            rs = ps.executeQuery();    
           // Do something with the ResultSet here or do not close the statement!   
        }
    }
    return rs; // should be something else! (as it could be already closed)
}

那么您肯定需要用ResultSet做点什么!例如:检查 email/password 组合是否存在以验证登录请求。

此外,一些重要的评论和提示:

  • 使用isValid(timeout)
  • 初始化后最好检查连接是否有效
  • 考虑一个连接池或至少一些重用连接的方法
  • 最终将现有工具(如 Apache 等库)用于 ORM(对象关系映射)和 DAO(数据库访问对象)层。实际上,强烈推荐
  • 关闭 PreparedStatement 将自动关闭关联的 ResultSet。您的代码没有考虑到这一点。比照。 https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html

随时关注我!