第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何顯示條目是否在數(shù)據(jù)庫(kù)中?

如何顯示條目是否在數(shù)據(jù)庫(kù)中?

慕哥6287543 2024-01-25 15:56:05
我目前正在制作一個(gè)程序,可以在其中刪除數(shù)據(jù)庫(kù)中的某些條目。我可以做到這一點(diǎn),但我想添加一個(gè) JOptionPane 屏幕,以便它何時(shí)可以找到它或無(wú)法找到它(因此,當(dāng)未找到名稱時(shí),不應(yīng)找到它并顯示一條消息說(shuō)找不到它)。我嘗試使用結(jié)果語(yǔ)句,但這似乎不起作用。如果有人知道如何做,請(qǐng)?jiān)u論如何做!private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                              ArrayList<Cookbook  > recipes = new ArrayList();    String name = txtName.getText();    try {        String url = "jdbc:derby://localhost:1527/Cookbook";        Connection conn = DriverManager.getConnection(url);        String query = "DELETE from RECIPES WHERE NAME = ?";        PreparedStatement pst = conn.prepareStatement(query);        pst.setString(1, name);        pst.executeUpdate();        JOptionPane.showMessageDialog(null, name + " was sucessfully deleted");    } catch (Exception e) {        JOptionPane.showMessageDialog(null, "was not found or could not be deleted");    }
查看完整描述

1 回答

?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

引用javadoc?executeUpdate()

返回:

(1)?SQL 數(shù)據(jù)操作語(yǔ)言 (DML) 語(yǔ)句的行計(jì)數(shù)或 (2) 0(不返回任何內(nèi)容的 SQL 語(yǔ)句)

所以:

String url = "jdbc:derby://localhost:1527/Cookbook";

try (Connection conn = DriverManager.getConnection(url)) {

? ? String query = "DELETE from RECIPES WHERE NAME = ?";

? ? try (PreparedStatement pst = conn.prepareStatement(query)) {

? ? ? ? pst.setString(1, name);

? ? ? ? int rowCount = pst.executeUpdate();

? ? ? ? if (rowCount == 0) {

? ? ? ? ? ? JOptionPane.showMessageDialog(null, "'" + name + "' was not found");

? ? ? ? } else if (rowCount == 1) {

? ? ? ? ? ? JOptionPane.showMessageDialog(null, "'" + name + "' was successfully deleted");

? ? ? ? } else { // cannot happen if `NAME` has unique index in the database

? ? ? ? ? ? JOptionPane.showMessageDialog(null, rowCount + " recipes named '" + name + "' were deleted");

? ? ? ? }

? ? }

} catch (Exception e) {

? ? JOptionPane.showMessageDialog(null, "something went wrong: " + e);

}


查看完整回答
反對(duì) 回復(fù) 2024-01-25
  • 1 回答
  • 0 關(guān)注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)