简体   繁体   中英

ResultSet method previous() is not working

   public void actionPerformed(ActionEvent E)
   {
       int id;
       String name,address,phone;
       Connection conn = null;
       PreparedStatement stmt = null;
       try {
           //STEP 2: Register JDBC driver
           Class.forName("com.mysql.jdbc.Driver");

           //STEP 3: Open a connection
           System.out.println("Connecting to database...");
           conn = DriverManager.getConnection(DB_URL,USER,PASS);

           //STEP 4: Execute a query
           String sql;
           sql = "SELECT * FROM person";
           System.out.println("Creating statement...");
           stmt = conn.prepareStatement (sql,ResultSet.TYPE_SCROLL_INSENSITIVE , 
                                     ResultSet.CONCUR_UPDATABLE );


            ResultSet rs = stmt.executeQuery(sql);

            if(E.getSource()== bNext) {
                rs.next();
                id  = rs.getInt("id");
                name = rs.getString("name");
                address = rs.getString("address");
                phone = rs.getString("phone");

                //Display values
                System.out.print("ID: " + id);
                System.out.print(", Name: " + name);
                System.out.print(", Address: " + address);
                System.out.println(", Phone: " + phone);
            }
            if(E.getSource()== bPrevious) {
                rs.previous();
                id  = rs.getInt("id");
                name = rs.getString("name");
                address = rs.getString("address");
                phone = rs.getString("phone");

                //Display values
                System.out.print("ID: " + id);
                System.out.print(", Name: " + name);
                System.out.print(", Address: " + address);
                System.out.println(", Phone: " + phone);
            }
            if(E.getSource()==bLast) {
                rs.last();
                id  = rs.getInt("id");
                name = rs.getString("name");
                address = rs.getString("address");
                phone = rs.getString("phone");

               //Display values
               System.out.print("ID: " + id);
               System.out.print(", Name: " + name);
               System.out.print(", Address: " + address);
               System.out.println(", Phone: " + phone);
           }
           if(E.getSource()==bFirst) {
               rs.first();
               id  = rs.getInt("id");
               name = rs.getString("name");
               address = rs.getString("address");
               phone = rs.getString("phone");

               //Display values
               System.out.print("ID: " + id);
               System.out.print(", Name: " + name);
               System.out.print(", Address: " + address);
               System.out.println(", Phone: " + phone);
          }
          //STEP 6: Clean-up environment
          rs.close();
          stmt.close();
          conn.close();
    }

I don't know why, but the previous() method is not working and whenever the next button is pressed by the user, the output remains the same, and doesn't move forwards. can anyone help please?

Each time your actionPerformed method is called (I'm assuming it's called when some button is pressed), you execute the query again. Therefore next() will always give you the first element and previous will return false.

You should probably read all the rows into some data structure such as ArrayList (as an initialization step, not when the button is clicked), and maintain the current index of that List , which will allow you to move forward or backward when a button is clicked.

PS you shouldn't use rs.next() or rs.previous() without checking the returned value. If those methods return false , you should not call methods such as rs.getInt("id") , since they will throw an exception.

PPS some JDBC drivers don't support the previous() , first & last methods, which means they might throw a SQLFeatureNotSupportedException . However, you'll only need next() if you read all the data in an initialization step, as I suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM