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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我應(yīng)該如何確定數(shù)據(jù)是否已存在于數(shù)據(jù)庫中

我應(yīng)該如何確定數(shù)據(jù)是否已存在于數(shù)據(jù)庫中

哈士奇WWW 2023-09-13 17:33:37
我的代碼出現(xiàn)一些錯誤,當(dāng)我編寫數(shù)據(jù)庫中不存在的卷號并按下刪除按鈕時,還會彈出一條消息,提示“記錄已成功刪除”。實際上我想創(chuàng)建一個學(xué)生報告項目并通過連接java和MySQL。因此,我為刪除按鈕編寫了代碼,其中如果寫入了學(xué)生的學(xué)號并按下刪除,它將刪除該特定學(xué)生的記錄。所以希望你理解我的問題并期待得到準(zhǔn)確的答案。Class.forName("java.sql.DriverManager");Connection con=DriverManager.getConnection("jdbc:mysql://localhost/stud","root","");Statement stmt=con.createStatement();String query="delete from info where rollno="+rn.getText();int  d = stmt.executeUpdate(query);JOptionPane.showMessageDialog(null,"record deleted successfully!!!");rn.setText("");
查看完整描述

4 回答

?
鴻蒙傳說

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

首先,使用PreparedStatement填寫參數(shù)而不是編寫 SQL 字符串。

可以避免非常討厭的錯誤(《Bobby Tables》XKCD 漫畫中的 SQL 注入是如何工作的?)。所以


PreparedStatement stmt = con.prepareStatement("DELETE FROM info WHERE rollno=?");

stmt.setLong(1, Long.parseLong(rn.getText()));

int d = stmt.executeUpdate();

就您的問題而言:

該方法executeUpdate返回受影響的行數(shù)。

如果等于 0,則沒有刪除任何行。


if (d == 0)

{

  JOptionPane.showMessageDialog(null,"This record does not exist");

  // Return or thrown an exception or whatever to interrupt the operation

}

else

  JOptionPane.showMessageDialog(null,"record deleted successfully!!!");


查看完整回答
反對 回復(fù) 2023-09-13
?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗 獲得超6個贊

showMessageDialog僅當(dāng)變量值為正時才應(yīng)執(zhí)行,d即某些記錄已從數(shù)據(jù)庫中刪除。例如


Class.forName("java.sql.DriverManager");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost/stud","root","");

Statement stmt=con.createStatement();

String query="delete from info where rollno="+rn.getText();

int  d = stmt.executeUpdate(query);

if(d>0){

    JOptionPane.showMessageDialog(null,"record deleted successfully!!!");

}

rn.setText("");


查看完整回答
反對 回復(fù) 2023-09-13
?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗 獲得超4個贊

輸入 rn:1 or 1=1并享受。使用PreparedStatements 將防止這種邪惡的SQL 注入。它還處理 SQL 字符串周圍的撇號以及轉(zhuǎn)義撇號和其他字符。


Connection con=DriverManager.getConnection("jdbc:mysql://localhost/stud","root","");

String query="delete from info where rollno=?";

try (PreparedStatement stmt = con.prepareStatement(query)) {

    stmt.setLong(1, Integer.parseLong(rn.getText()));

    int d = stmt.executeUpdate();

    if (d != 0) {

        JOptionPane.showMessageDialog(null, "Record deleted successfully.",

            JOptionPane.INFORMATION_MESSAGE);

    }

}

此try-with-resources將確保stmt始終關(guān)閉


查看完整回答
反對 回復(fù) 2023-09-13
?
慕婉清6462132

TA貢獻(xiàn)1804條經(jīng)驗 獲得超2個贊

檢查executeUpdate 的結(jié)果是否> 0。如果是,則您的條目已被刪除。



查看完整回答
反對 回復(fù) 2023-09-13
  • 4 回答
  • 0 關(guān)注
  • 167 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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