Java-使用“;查找“;用于显示访问文件中的特定记录或从中删除特定记录的命令
Java - Using the "find" command to display or delete a specific record from an access file
因此,我一直难以使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++;
}
当计数为零时,结果中没有条目,用户正在搜索的内容也不在数据库中。
相关文章:
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 当我点击jsf中的primefaces命令按钮时,如何获得点击事件
- 如何使用Socket.io将命令从客户端发送到服务器
- 通过命令行/批处理文件打开页面时,将javascript代码注入Google Chrome
- AngularJs对所有页面中的所有记录进行排序
- 有没有一种方法可以在控制台关闭的情况下让console.log()在IE中记录消息
- window.location替换并传递URL历史记录条目中的变量
- Javascript历史记录转换为php行
- 命令行中的Uglify js;不要弄乱变量名
- JavaScript,PHP-用JavaScript将多个数据库记录发送到变量或表
- 猫鼬在特定记录中查找嵌套记录
- 浏览器何时记录历史记录
- 将一个表的id与另一个表中的user_id进行匹配,以获取mysql中的记录
- 更新成员数据模型中的记录列表
- http.listen()在运行时接受终端命令
- 如何使用提交按钮搜索表中的记录
- 正在OrientDB中嵌入函数中的记录
- 如何在没有单词的情况下从命令行运行Node.js程序'节点'
- Java-使用“;查找“;用于显示访问文件中的特定记录或从中删除特定记录的命令
- 有没有办法记录npm install命令的输出?