How to save a record in Postgresql?

Asked

Viewed 556 times

-1

This error occurs when I try to save a record in the Postgresql database, I am 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);
            }

        }
    }  

Connected to the bank

public class Conexaobanco { public Statement Stm; //Change,Exluir,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);
    }
}             
  • https://answall.com/tour

  • Post the insert code next to the SQL used

  • What is the 306 line of the class CadastroLeitor?

  • id = Integer.parseint(Banco.Rs.getString("id_reader"));

  • It was working normally, it’s connected to the bank, it’s just not saving and deleting. Until I edit it I can

  • So the error is not in this code

  • if (Change = false){

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

  • In these two lines appear the error lamp

  • In the first: "Inactive Branch"

  • I found the error, ta in this code yes, in the part of changing

  • in the second:"Unused name"

  • What’s the mistake? I’m already lost

  • Èssa line id = Integer.parseInt(Banco.Rs.getString("id_leitor")); is the cause of the error, but its code is incomplete, and it is not possible to know about this Bank.RS.

Show 9 more comments

1 answer

0

This error ocher why your Resultset pointer is not getting the starting line of your query output from where it should start reading the data. The error must be in this code snippet:

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

Put your class code Bench to better analyze the error :)

  • public class Cadastre leitor extends javax.swing.Jframe { Connectionbank Bank = new Connectionbank(); Static Boolean Change = false; public Register() { initComponents(); Bank.connected(); Bank.executaSQL("SELECT * from Reader"); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.