Java-使用“;查找“;用于显示访问文件中的特定记录或从中删除特定记录的命令

Java - Using the "find" command to display or delete a specific record from an access file

本文关键字:记录 命令 删除 文件 查找 使用 用于 显示 访问 Java-      更新时间:2023-09-26

因此,我一直难以使Java代码正常工作。我正在尝试使用"查找"功能来定位访问数据库文件(.accdb)中的特定信息集,并显示或删除它。

我运气不太好。这很令人费解,因为据我所知,向数据库中添加新条目也需要"查找"代码,而我可以毫无问题地做到这一点。坦率地说,我怀疑我在某个地方贴错了标签。

下面是我的代码片段,其中还显示了一个查找由用户写入文本框的"键"标识的特定文件的示例。如果您能帮忙解决这个烦恼,我们将不胜感激。

// Implement the four instance methods *************
    // addNew, delete, update - called from each specific PD class
    // find - used locally by addNew(), delete(), and update().
    /**
     * An instance method to find a record in the database.<br /><br />
     *
     * This is a private method and can only be used locally within objects instantiated from this class.<br />
     * Used by addNew(), delete(), and update().
     *
     * @param String objectType
     * @param String key
     * @return a Anime object
     * @throws NotFoundException
     * @throws SQLException
     */
    private Anime find (String objectType, String key) throws NotFoundException, SQLException {
        anAnime = null;
        if (objectType.equalsIgnoreCase ("PlushToys")) {
                // define the SQL query statement using the phone number key
            String sqlQuery = "SELECT Anime, Character, Ability, CanItSpeak, Material, Size " +
                                "FROM PlushToys" +
                                "Where Character = '" + key +"'";
                // execute the SQL query statement
            ResultSet rs = aStatement.executeQuery (sqlQuery);
                // next method sets cursor & returns true if there is data
            boolean gotIt = rs.next();
            if (gotIt) {
                // extract the data
            String Anime = rs.getString(1);
            String Character = rs.getString (2);
            String Ability = rs.getString (3);
            String CanItSpeak = rs.getString (4);
            String Material = rs.getString (5);
            String Size = rs.getString (6);
                // create PlushToy instance & add it to the ArrayList
            anAnime = new PlushToys (Anime, Character, Ability, CanItSpeak, Material, Size);
                rs.close();
            } else {
                    // nothing was retrieved
                rs.close();
                throw (new NotFoundException ("not found "));
            }

在SQL查询之后,您需要询问结果是否为空。因此,您必须计算结果中的行数。你可以这样做:

ResultSet rs;
  ...
  int count = 0;
  while(rs.next()) {
    count++;  
  }

当计数为零时,结果中没有条目,用户正在搜索的内容也不在数据库中。