gpt4 book ai didi

java - java中的IF ELSE语句删除sql joption消息

转载 作者:行者123 更新时间:2023-12-02 05:16:16 25 4
gpt4 key购买 nike

我尝试创建一个 if 语句,但不起作用,如果在 jtable 中找到 isbn book,则显示消息“book”删除”,否则找不到“book”。这可能吗?

 private void elimina_libroActionPerformed(java.awt.event.ActionEvent evt) {      
Connection conn = Connessione.ConnecrDb();
Statement stmt = null;
ResultSet emps = null;
boolean found = false;
try{
if(emps.next())
found= true;
String sql= "DELETE FROM progetto.libro WHERE isbn =?";
pst=(OraclePreparedStatement) conn.prepareStatement(sql);
pst.setString (1, txt_isbn.getText());
pst.execute();

if (!found)
JOptionPane.showMessageDialog(null, "LIBRO ELIMINATO");
else{
JOptionPane.showMessageDialog(null, "BOOK NOT FOUND","ERRORE",jOptionPane.WARNING_MESSAGE);

}

}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,e);
}

但是不行,你能帮我吗?谢谢

我用这个更改代码

Connection conn = Connessione.ConnecrDb();
Statement stmt = null;
ResultSet emps = null;

try{

String sql= "DELETE FROM progetto.libro WHERE isbn =?";

pst=(OraclePreparedStatement) (PreparedStatement) conn.prepareStatement(sql);
pst.setString (1, txt_isbn.getText());
int deleted = pst.executeUpdate();

if (deleted == 0) {

JOptionPane.showMessageDialog(null, "LIBRO ELIMINATO");
Update_table();


}else
JOptionPane.showMessageDialog(null, "LIBRO NON TROVATO");

}

catch (Exception e)
{
JOptionPane.showMessageDialog(null,e);

}

但是,如果我插入不正确的 isbn,则消息为:“图书删除”,如果我插入正确的 isbn,则消息为“未找到图书”,如果我再次插入正确的 isbn,则消息为“图书删除”,并且图书从数据库中删除。

最佳答案

    pst=(PreparedStatement) conn.prepareStatement(sql);            
pst.setString (1, txt_isbn.getText());
int deleted = pst.executeUpdate();

if (deleted == 0) {
...

int executeUpdate() throws SQLException

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Returns: either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing

关于java - java中的IF ELSE语句删除sql joption消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26932038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com