This discussion is archived
5 Replies Latest reply: Nov 20, 2012 8:27 AM by 805574 RSS

this is code for delete button,is there any errors??,plz help guys

974995 Newbie
Currently Being Moderated
private void deleteActionPerformed(java.awt.event.ActionEvent evt) {                                      
// TODO add your handling code here:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cc = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/registration", "root", "");
Statement s = (Statement) cc.createStatement();
ResultSet r = s.executeQuery("delete from students where nic=" + nic.getText() + "");
while (r.next()) {

name.setText(r.getString("Name"));
phone.setText(r.getString("Phone"));
email.setText(r.getString("Email"));
course.setSelectedItem(r.getString("Course"));



}
} catch (Exception e) {
e.printStackTrace();

}


}
  • 1. Re: this is code for delete button,is there any errors??,plz help guys
    Tolls Journeyer
    Currently Being Moderated
    Does it work?

    I would take the whole database access stuff out and do that in its own thread, via some DAO object.
    I would also rework it so it closed resources, and used a PreparedStatement.

    But essentially it comes down to my first sentence above...
  • 2. Re: this is code for delete button,is there any errors??,plz help guys
    974995 Newbie
    Currently Being Moderated
    talls, actually it seems dosen't have any syntax errors ,bt in running cause problems, it code can use for update button as well ,only substitute part is database query
  • 3. Re: this is code for delete button,is there any errors??,plz help guys
    Tolls Journeyer
    Currently Being Moderated
    Well, if you have problems then it might be an idea to actually post them?
    Or are we supposed to guess?
  • 4. Re: this is code for delete button,is there any errors??,plz help guys
    974995 Newbie
    Currently Being Moderated
    your right, i must post them, those are the errors ,

    ava.sql.SQLException: After end of result set
         at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
         at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:726)
         at com.mysql.jdbc.ResultSet.getStringInternal(ResultSet.java:5624)
         at com.mysql.jdbc.ResultSet.getString(ResultSet.java:5544)
         at com.mysql.jdbc.ResultSet.getString(ResultSet.java:5584)
         at reg.student.listStudents(student.java:54)
         at reg.student.<init>(student.java:33)
         at reg.student$7.run(student.java:383)
         at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
         at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
         at java.awt.EventQueue.access$200(EventQueue.java:103)
         at java.awt.EventQueue$3.run(EventQueue.java:682)
         at java.awt.EventQueue$3.run(EventQueue.java:680)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
         at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
         at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
         at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
         at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
    BUILD SUCCESSFUL (total time: 8 seconds)
  • 5. Re: this is code for delete button,is there any errors??,plz help guys
    805574 Newbie
    Currently Being Moderated
    You are executing a delete statement, what exactly are you expecting in the result set?

    Take a look at the java.sql.Statement javadoc, there are better choices than executeQuery for what you are attempting.

    I agree with Tolls though, a PreparedStatement would be better. The code you have written has potential for SQL injection written all over it. What if nic.getText( ) returned "abc' or nic != 'a"?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points