ResultSet方法previous()不起作用

时间:2016-01-01 12:27:24

标签: java jdbc

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

我不知道原因,但是previous()方法无效,每当用户按下next按钮时,输出保持不变,并且不会前进。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:8)

每次调用actionPerformed方法时(我假设在按下某个按钮时调用它),您再次执行查询。因此,next()将始终为您提供第一个元素,previous将返回false。

您应该将所有行读入某些数据结构,例如ArrayList(作为初始化步骤,而不是单击按钮时),并维护List的当前索引,这将是允许您在单击按钮时向前或向后移动。

P.S。如果不检查返回值,则不应使用rs.next()rs.previous()。如果这些方法返回false,则不应调用rs.getInt("id")之类的方法,因为它们会抛出异常。

P.P.S某些JDBC驱动程序不支持previous()first& last方法,这意味着他们可能会抛出SQLFeatureNotSupportedException。但是,如果您按照我的建议读取初始化步骤中的所有数据,则只需要next()