In Java, Deletion operation is correct, but catch

2019-08-02 07:50发布

问题:

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.

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);

}

回答1:

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.