在Java中,删除操作是正确的,但是catch也会执行 [英] In Java, Deletion operation is correct, but catch gets executed too

查看:191
本文介绍了在Java中,删除操作是正确的,但是catch也会执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的概念是我必须在第一个组合框中选择一个项目.根据选择,它将在第二个组合框中添加一些项目.一旦选择了第二个组合框,则将检索一些访问表数据.

My concept is that I have to select an item in first combobox. It will add up some items to the second combo box, based on the selection. Once, the second combobox is selected, then some access table data is retrieved.

最后,当我单击Delete时,必须根据两个组合框中显示的项目删除记录.

Finally, when I click Delete, the record has to get deleted based on the item present in the two combo box.

我得到正确删除的记录.但是,我的问题是,每次使用

I get the record correctly deleted. But, my problem is, I'm getting an error dialog(which I used in catch) everytime with a

未生成结果集

No resultset produced

尽管正确执行了删除操作,但命令提示符中出现

错误.我想知道,里面发生了什么.请帮助我诊断.

error in the command prompt, though the deletion happens correctly. I want to know, what's going on inside. Kindly help me diagnose.

try
{

  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection conn=DriverManager.getConnection("jdbc:odbc:vasantham","","");
  Statement st=conn.createStatement();
  ResultSet rs=st.executeQuery("delete from try where tname='"+scb+"' and   Purpose='"+scp+"'");

  JOptionPane.showMessageDialog(null,"Your entry has been DELETED successfully!!!");

}
catch(Exception e)
{
  JOptionPane.showMessageDialog(null,"Error!Try again!");
  System.out.println(e);

}

推荐答案

您正在使用

You are using st.executeQuery which has to be used with selects, as only selects create a ResultSet. You are using an UPDATE query, which will not produce a ResultSet hence the error.

在您的情况下,您需要使用

In your case you need to use executeUpdate

int result = st.executeUpdate(...)

结果将包含已删除(更新)的行数.

The result will contain the number of your deleted (updated) rows.

这篇关于在Java中,删除操作是正确的,但是catch也会执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆