In Java, Deletion operation is correct, but catch gets executed too

孤街浪徒 提交于 2019-12-11 09:39:09

问题


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.



来源:https://stackoverflow.com/questions/11579389/in-java-deletion-operation-is-correct-but-catch-gets-executed-too

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!