How to save a record in PostgreSQL?

-1

This error occurs when I try to save a record in the PostgreSQL database, I'm using java

mai 15, 2018 11:09:18 AM bibliotecapesca.CadastroLeitor BtnSalvarActionPerformed
GRAVE: null
org.postgresql.util.PSQLException: ResultSet não está posicionado corretamente, talvez você precise chamar next.
    at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2770)
    at org.postgresql.jdbc.PgResultSet.getString(PgResultSet.java:1893)
    at org.postgresql.jdbc.PgResultSet.getString(PgResultSet.java:2478)
    at bibliotecapesca.CadastroLeitor.BtnSalvarActionPerformed(CadastroLeitor.java:306)
    at bibliotecapesca.CadastroLeitor.access$100(CadastroLeitor.java:18)
    at bibliotecapesca.CadastroLeitor$2.actionPerformed(CadastroLeitor.java:108)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Insertion code:

private void BtnSalvarActionPerformed(java.awt.event.ActionEvent evt) {                                          

        if (Alterar = false){       
        try {

        PreparedStatement inclusao = Banco.Conn.prepareStatement("INSERT INTO leitor (nome_leitor, endereco_leitor, cpf, telefone_leitor, celular_leitor) VALUES (?,?,?,?,?)");


        inclusao.setString(1, TxtNome.getText());
        inclusao.setString(2, TxtEndereco.getText());
        inclusao.setString(3, TxtCpf.getText());
        inclusao.setString(4, TxtTelefone.getText());
        inclusao.setString(5, TxtCelular.getText());

        inclusao.executeUpdate();

        JOptionPane.showMessageDialog(null, "Cadastro Realizado com sucesso!!");

        //Limpa Campos
        TxtNome.setText(null);
        TxtEndereco.setText(null);
        TxtCpf.setText(null);
        TxtTelefone.setText(null);
        TxtCelular.setText(null);

        //Atualiza o banco de dados
        Banco.executaSQL("SELECT * From leitor");



         //Desabilita botões e campos

        BtnSalvar.setEnabled(false);
        BtnNovo.setEnabled(true);
        BtnAnterior.setEnabled(true);
        BtnPosterior.setEnabled(true);

        TxtNome.setEnabled(false);
        TxtEndereco.setEnabled(false);
        TxtCpf.setEnabled(false);
        TxtTelefone.setEnabled(false);
        TxtCelular.setEnabled(false);

        } catch (SQLException ex) {
            Logger.getLogger(CadastroLeitor.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Ocorreu um erro na execução, contate o administrador!!");

        }//catch

        }//if
         //Salvar alteração
        else if (Alterar = true){
            try {
                PreparedStatement alteracao;
                alteracao = Banco.Conn.prepareStatement("UPDATE leitor SET nome_leitor=?, endereco_leitor=?, cpf=?, telefone_leitor=?, celular_leitor=? WHERE id_leitor=?");


                int id;
                id = Integer.parseInt(Banco.Rs.getString("id_leitor"));

                alteracao.setString(1, TxtNome.getText());
                alteracao.setString(2, TxtEndereco.getText());
                alteracao.setString(3, TxtCpf.getText());
                alteracao.setString(4, TxtTelefone.getText());
                alteracao.setString(5, TxtCelular.getText());
                alteracao.setInt(6, id);

                alteracao.executeUpdate();

                JOptionPane.showMessageDialog(null, "Dados alterados com sucesso!!");


                //Atualiza consulta
                Banco.executaSQL("SELECT * FROM leitor");    

                //Desabilita / Ativa botões e campos

                BtnSalvar.setEnabled(false);
                BtnNovo.setEnabled(true);
                BtnAnterior.setEnabled(true);
                BtnPosterior.setEnabled(true);

                TxtNome.setEnabled(false);
                TxtEndereco.setEnabled(false);
                TxtCpf.setEnabled(false);
                TxtTelefone.setEnabled(false);
                TxtCelular.setEnabled(false);

                TxtNome.setText(null);
                TxtEndereco.setText(null);
                TxtCpf.setText(null);
                TxtTelefone.setText(null);
                TxtCelular.setText(null);

                Alterar=false;


            } catch (SQLException ex) {
                Logger.getLogger(CadastroLeitor.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }  

Bank Connection

public class ConnectionBank {     public Statement Stm; // Change, Exclude, Add     public ResultSet Rs; // Search

private String Driver = "org.postgresql.Driver";
private String Caminho = "jdbc:postgresql://localhost:5432/biblioteca";
private String Usuario = "postgres";
private String Senha = "0000";

public Connection Conn;



public void conexao()
{

    try {
        System.setProperty("jdbc.Drivers", Driver);
        Conn = DriverManager.getConnection(Caminho, Usuario, Senha);
        JOptionPane.showMessageDialog(null, "Conectado!!");
    } catch (SQLException ex) {
        Logger.getLogger(ConexaoBanco.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public void executaSQL (String sql){
    try {
        Stm = Conn.createStatement(Rs.TYPE_SCROLL_INSENSITIVE, Rs.CONCUR_READ_ONLY);

        Rs = Stm.executeQuery(sql);

    } catch (SQLException ex) {
        Logger.getLogger(ConexaoBanco.class.getName()).log(Level.SEVERE, null, ex);
    }

}


public void desconecta()
{
    try{
        Conn.close();

    }catch (SQLException ex){
        Logger.getLogger(ConexaoBanco.class.getName()).log(Level.SEVERE, null, ex);
    }
}             
    
asked by anonymous 15.05.2018 / 16:13

1 answer

0

This error ocrre because the pointer of your ResultSet is not receiving the initial line of the result of your query from where it should begin to read the data. The error should be in this code snippet:

id = Integer.parseInt(Banco.Rs.getString("id_leitor"));

Put the code of your class Bank to better analyze the error:)

    
15.05.2018 / 17:06