Navegabilidade dos dados

Bom dia a todos.

Estou utilizando em meu programa, os métodos, rs.first,rs.last,rs.previous e rs.next; os doi primeiros funnconam perfeitamente e consigo posicionar no primeiro e no último campo da tabela sem problemas, porém quando eu utilizo o previous e o next, não consigo o resultado desejado, ou seja não adianto ou retrocedo o registro corrente. Qndo chego ao primeiro ou ao último registro e aplico estes métodos, ocorre um erro. Se for possível gostaria que alguém pudesse me dar um exemplo de como informar ao programa que o registro já está posicionado no primeiro ou no último, e não acitar os comandos de previous ou next, e de como fazer estes dois funcionarem como deveriam. Cnsultei as características do driver e ele suporta estes métodos.

Muito obrigado por sua atenção.

Oi,
seria melhor se postasse o que você fez no botão avançar e retroceder, e qual o erro… tipo de evento… se essa barra fica é externa, controlando todas as outras… se tem paginação ou seja, se marca a quantida de paginas…

só assim poderemos ajuda-lo…

Tchauzin!

[color=blue]ok, então como solicitado:
Esta é a conexão com o banco:[/color]

/*

  • DBCONNECTION.java
  • Created on 13 de Janeiro de 2007, 17:29
  • To change this template, choose Tools | Template Manager
  • and open the template in the editor.
    */

package dr_soft_java;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.RowSet;
import javax.sql.rowset.CachedRowSet;
import com.sun.rowset.CachedRowSetImpl;
import javax.swing.JOptionPane;
import javax.swing.*;
/**
*

  • @author Leandro
    */
    public class DBCONNECTION
    {
    private Connection conn = null;
    public Connection getConnection() throws ClassNotFoundException
    {
    try
    {
    if (conn == null)
    {
    Class.forName(“org.firebirdsql.jdbc.FBDriver”);
    conn = DriverManager.getConnection(“jdbc:firebirdsql:localhost/3050:C:/SISTEMAS/NETBEANS/BANCO/DR.gdb”,“SYSDBA”,“masterkey”);

          }
          return conn;
         
          //JOptionPane.showMessageDialog(this,"DATABASE CONNECTED WITH SUCESS. HAVE A NICE WORK.");
       }
       catch (SQLException e)
       {
          throw new  IllegalStateException("DATABASE NOT FOUND. CONTACT THE SYSTEM MASTER AND REPORT THIS ERROR: ", e);
       }        
    

    }
    public RowSet excuteQuery(String sql) throws ClassNotFoundException
    {
    Statement stmt = null;
    ResultSet rs = null;
    try
    {
    try
    {
    stmt = getConnection().createStatement();
    rs = stmt.executeQuery(sql);
    CachedRowSet result = new CachedRowSetImpl();
    result.populate(rs);
    return result;
    }
    finally
    {
    if (rs != null)
    rs.close();
    if (stmt != null)
    stmt.close();
    }
    }
    catch (SQLException e)
    {
    throw new IllegalStateException("ERROR TO EXEC THE SQL CODE. CONTACT THE SYSTEM MASTER AND REPORT THIS ERROR: ",e);
    }
    }
    private void PrepareStatement(ResultSet rs, int TYPE_SCROLL_SENSITIVE, int CONCUR_UPDATABLE) {
    throw new UnsupportedOperationException(“Not yet implemented”);
    }
    }

[color=red]e este é o frame que utiliza esta conexão e implementa os métodos:[/color]

/*

  • DR_SOFT_JAVA_CONSULTORIOS.java
  • Created on 7 de Janeiro de 2007, 12:36
    */

package dr_soft_java;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.RowSet;
import javax.swing.JOptionPane;
import java.sql.*;

/**
*

  • @author Leandro
    */
    public class DR_SOFT_JAVA_CONSULTORIOS extends javax.swing.JFrame {

    /** Creates new form DR_SOFT_JAVA_CONSULTORIOS */
    public DR_SOFT_JAVA_CONSULTORIOS() {
    initComponents();
    setSize(953,665);
    setLocation(35,54);
    setDefaultCloseOperation(DR_SOFT_JAVA_CONSULTORIOS.HIDE_ON_CLOSE);
    }

    /** This method is called from within the constructor to

    • initialize the form.

    • WARNING: Do NOT modify this code. The content of this method is

    • always regenerated by the Form Editor.
      */
      // //GEN-BEGIN:initComponents
      private void initComponents() {
      jTextField1 = new javax.swing.JTextField();
      jButton1 = new javax.swing.JButton();
      jButton2 = new javax.swing.JButton();
      jTextField2 = new javax.swing.JTextField();
      jButton3 = new javax.swing.JButton();
      jButton4 = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jButton1.setFont(new java.awt.Font(“Arial”, 1, 12));
      jButton1.setText("<<");
      jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      jButton1ActionPerformed(evt);
      }
      });

      jButton2.setFont(new java.awt.Font(“Arial”, 1, 12));
      jButton2.setText("<");
      jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      jButton2ActionPerformed(evt);
      }
      });

      jButton3.setFont(new java.awt.Font(“Arial”, 1, 12));
      jButton3.setText(">");
      jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      jButton3ActionPerformed(evt);
      }
      });

      jButton4.setFont(new java.awt.Font(“Arial”, 1, 12));
      jButton4.setText(">>");
      jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      jButton4ActionPerformed(evt);
      }
      });

      org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
      .add(67, 67, 67)
      .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 202, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
      .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 208, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
      .add(layout.createSequentialGroup()
      .add(138, 138, 138)
      .add(jButton1)
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
      .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
      .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
      .add(jButton4)))
      .addContainerGap(123, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
      .addContainerGap()
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
      .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
      .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
      .add(223, 223, 223)
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
      .add(jButton1)
      .add(jButton2)
      .add(jButton3)
      .add(jButton4))
      .addContainerGap(24, Short.MAX_VALUE))
      );
      pack();
      }// //GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    // TODO add your handling code here:
    DBCONNECTION conn = new DBCONNECTION();
    RowSet rs = null;
    try {
    rs = conn.excuteQuery(“select * from CONSULTORIOS”);
    } catch (ClassNotFoundException ex) {
    ex.printStackTrace();
    }
    try {
    rs.last();
    } catch (SQLException ex) {
    ex.printStackTrace();
    }
    String razao = null;
    String fantasia = null;
    try {
    fantasia = rs.getString(“nome_fantasia”);
    jTextField2.setText(fantasia);
    razao = rs.getString(“razao_social”);
    jTextField1.setText(razao);
    } catch (SQLException ex) {
    ex.printStackTrace();
    }

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    // TODO add your handling code here:
    DBCONNECTION conn = new DBCONNECTION();
    RowSet rs = null;
    try {
    rs = conn.excuteQuery(“select * from CONSULTORIOS”);
    } catch (ClassNotFoundException ex) {
    ex.printStackTrace();
    }
    try {
    if (rs.isLast()){
    return;

         }
         rs.next(); 
     } catch (SQLException ex) {
          JOptionPane.showMessageDialog(this,"THERE ARE NOT A NEXT 1 "+ex.getMessage());
     }
     String razao = null;
     String fantasia = null;
     try {
         fantasia = rs.getString("nome_fantasia");
         jTextField2.setText(fantasia);
         razao = rs.getString("razao_social");
         jTextField1.setText(razao);
     } catch (SQLException ex) {
         JOptionPane.showMessageDialog(this,"THERE ARE NOT A NEXT 1 "+ex.getMessage());
     }
    

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    // TODO add your handling code here:
    DBCONNECTION conn = new DBCONNECTION();
    RowSet rs = null;
    try {
    rs = conn.excuteQuery(“select * from CONSULTORIOS”);
    } catch (ClassNotFoundException ex) {
    ex.printStackTrace();
    }

     try {
         if(rs.isFirst()){
             return;
         }
         rs.previous(); 
     } catch (SQLException ex) {
        JOptionPane.showMessageDialog(this,"THERE ARE NOT A PREVIOUS 1 "+ex.getMessage());
     }
     String razao = null;
     String fantasia = null;
     try {
         fantasia = rs.getString("nome_fantasia");
         jTextField2.setText(fantasia);
         razao = rs.getString("razao_social");
         jTextField1.setText(razao);
     } catch (SQLException ex) {
         // throw new  IllegalStateException("THERE ARE NOT A PREVIOUS", ex);
          JOptionPane.showMessageDialog(this,"THERE ARE NOT A PREVIOUS"+ex.getMessage());
     }
    

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    DBCONNECTION conn = new DBCONNECTION();
    RowSet rs = null;
    try {
    rs = conn.excuteQuery(“select * from CONSULTORIOS”);
    } catch (ClassNotFoundException ex) {
    ex.printStackTrace();
    }
    try {
    rs.first();
    } catch (SQLException ex) {
    JOptionPane.showMessageDialog(this,"THERE STILL IN THE FIRST "+ex.getMessage());
    }
    String razao = null;
    String fantasia = null;
    try {
    fantasia = rs.getString(“nome_fantasia”);
    jTextField2.setText(fantasia);
    razao = rs.getString(“razao_social”);
    jTextField1.setText(razao);
    } catch (SQLException ex) {
    ex.printStackTrace();
    }

    }//GEN-LAST:event_jButton1ActionPerformed
    private Connection con = null;
    /**

    • @param args the command line arguments
      */

    public static void main(String args[]) throws ClassNotFoundException, SQLException {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new DR_SOFT_JAVA_CONSULTORIOS().setVisible(true);
    }
    });
    DBCONNECTION conn = new DBCONNECTION();
    RowSet rs = conn.excuteQuery(“select * from CONSULTORIOS”);
    rs.first();
    }

    private void PrepareStatement(ResultSet rs, int TYPE_SCROLL_SENSITIVE, int CONCUR_UPDATABLE) {
    throw new UnsupportedOperationException(“Not yet implemented”);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}

[color=green]Bom ainda estou em meu primeiro mês de java e sei que vou ter de estudar muito, como aliás já tenho feito, mas gostaria de contar com experiência de vocês, para continuar evoluindo com qualidade. Obrigado a todos.[/color]

  1. Sem querer ser chato, mas já sendo…
    http://www.guj.com.br/posts/list/50115.java

  2. Uma das formas seria vc colocar esses métodos de navegação dentro de um if.


if(rs.next())
    acao....

Isso porque estes métodos retornam boolean, e se não conseguirem realizar a operação retornam false.

Até mais,