Frage

I have a two database table, on a single button click in my program , I want to delete all the two table's data. But there's always one datum left on the table.. Here is my code:

 public void mouseClicked(MouseEvent arg0) {
                    int confirm = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete the log? ", "Log", JOptionPane.YES_NO_OPTION );
                    if (confirm == JOptionPane.YES_OPTION){ 
                        try{

                            Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver");
                            con = DriverManager.getConnection("jdbc:odbc:RIM");
                            Statement st1 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                            Statement st2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                            String sql1 = "select * from userLogIn";
                            ResultSet rs1 = st1.executeQuery(sql1);
                            String sql2 = "select * from userViewed";
                            ResultSet rs2 = st2.executeQuery(sql2);

                            while(rs1.next()){          
                                rs1.deleteRow();
                                rs1.first();
                            }

                            while(rs2.next()){                                  
                                rs2.deleteRow();
                                rs2.first();
                            }

                            editorPane.setText("");
                        }catch(Exception e){e.printStackTrace();}
War es hilfreich?

Lösung

When there is only one element left, your call to first() sets the cursor to that element, but then the next() call in the while condition will return false, causing the exit from the loop and thus the deleteRow won't be executed.

Use rs.beforeFirst()`.

Note that your code has lots of issues, so get this answers as "for educational purposes" only.

Andere Tipps

Why are you iterating over a select record set at all? It's incredibly inefficient.

If you want to delete all data from those tables, you can simply execute the statements:

delete from userLogin;
delete from userViewed;

(or use truncate).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top