作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试创建一个 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/
我是一名优秀的程序员,十分优秀!