[RESOLVIDO]Erro na gravação em bd

15 respostas
S

Olá estou tentando gravar um registo no postge mas to recebendo o seguinte erro

run:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:504)
	at java.lang.Integer.valueOf(Integer.java:582)
	at empresarial.JFTransportadora.altera(JFTransportadora.java:694)
	at empresarial.JFTransportadora.jbSalvarActionPerformed(JFTransportadora.java:812)
	at empresarial.JFTransportadora.access$200(JFTransportadora.java:20)
	at empresarial.JFTransportadora$3.actionPerformed(JFTransportadora.java:123)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
	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:6505)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
	at java.awt.Component.processEvent(Component.java:6270)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4861)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
	at java.awt.Container.dispatchEventImpl(Container.java:2273)
	at java.awt.Window.dispatchEventImpl(Window.java:2719)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
	at java.awt.EventQueue.access$000(EventQueue.java:102)
	at java.awt.EventQueue$3.run(EventQueue.java:662)
	at java.awt.EventQueue$3.run(EventQueue.java:660)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.awt.EventQueue$4.run(EventQueue.java:676)
	at java.awt.EventQueue$4.run(EventQueue.java:674)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

segue aqui o codigo para gravação

private void grava() {
        String sql = "INSERT into transportadoras (trans_codigo, trans_nome, trans_fantasia, trans_cnpj, trans_ie, trans_cep, trans_endereco, trans_numero, trans_complemento, trans_bairro, trans_cidade, trans_estado, trans_ibge, trans_pais, trans_telefone, trans_fax, trans_celular, trans_contato, trans_email, trans_site, trans_obs, trans_placa, trans_cfop, trans_cfop_nome)" + "VALUES (default,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            Connection conn = Conexao.getConexao();
            PreparedStatement pst = conn.prepareStatement(sql);

            
            pst.setString(1, txRazao_Nome.getText());
            pst.setString(2, txFantasia.getText());
            pst.setString(3, txCNPJ.getText());
            pst.setString(4, txIE.getText());
            if (txCEP.getText().equals("")) {
                txCEP.setText(valnull);
                pst.setInt(5, Integer.valueOf(txCEP.getText()));
            } else {
                pst.setInt(5, Integer.valueOf(txCEP.getText()));
            }
            pst.setString(6, txEndereco.getText());
            pst.setString(7, txNumero.getText());
            pst.setString(8, txComplemento.getText());
            pst.setString(9, txBairro.getText());
            pst.setString(10, txCidade.getText());
            pst.setString(11, txEstado.getText());
            if (txIBGE.getText().equals("")) {
                txIBGE.setText(valnull);
                pst.setInt(12, Integer.valueOf(txIBGE.getText()));
            } else {
                pst.setInt(12, Integer.valueOf(txIBGE.getText()));
            }
            pst.setString(13, txPais.getText());
            pst.setString(14, txTelefone.getText());
            pst.setString(15, txFax.getText());
            pst.setString(16, txCelular.getText());
            pst.setString(17, txContato.getText());
            pst.setString(18, txEmail.getText());
            pst.setString(19, txSite.getText());
            pst.setString(20, txObs.getText());
            pst.setString(21, txPlaca.getText());
            if (txNatureza.getText().equals("")) {
                txNatureza.setText(valnull);
                pst.setInt(22, Integer.valueOf(txNatureza.getText()));
            } else {
                pst.setInt(22, Integer.valueOf(txNatureza.getText()));
            }

            pst.setString(23, txNaturezaNome.getText());
            desabilita();

            pst.executeUpdate();

            alt = false;
            jbIncluir.setEnabled(true);
            jbAlterar.setEnabled(true);
            jbSalvar.setEnabled(false);
            jbExcluir.setEnabled(true);
            jbImprimir.setEnabled(true);

            jbFechar.setEnabled(true);
            jbCancela.setEnabled(false);
            alt = false;
            JOptionPane.showMessageDialog(null, "Gravado com Sucesso!");
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao gravar registo\n" + ex.getMessage() + "\n" + ex.getCause());
        }
    }

ja ollhei tanto esse cédigo q ja sei ele de cabeça pra baixo ^^

a segue a estrutura dos campos da tabela transportadoras

trans_codigo serial NOT NULL,
  trans_nome character varying(280),
  trans_fantasia character varying(150),
  trans_cnpj character varying(20),
  trans_ie character varying(20),
  trans_endereco character varying(150),
  trans_numero character varying(20),
  trans_complemento character varying(80),
  trans_cidade character varying(150),
  trans_bairro character varying(150),
  trans_cep integer,
  trans_estado character varying(4),
  trans_pais character varying(80),
  trans_ibge integer,
  trans_telefone character varying(40),
  trans_fax character varying(40),
  trans_celular character varying(40),
  trans_contato character varying(150),
  trans_email character varying(280),
  trans_site character varying(280),
  trans_obs character varying(255),
  trans_placa character varying(20),
  trans_cfop integer,
  trans_cfop_nome character varying(280),

15 Respostas

E

Troque

if (txIBGE.getText().equals("")) {  
                txIBGE.setText(valnull);  
                pst.setInt(12, Integer.valueOf(txIBGE.getText()));  
            } else {  
                pst.setInt(12, Integer.valueOf(txIBGE.getText()));  
            }

por

if (txIBGE.getText().equals("")) {  
                txIBGE.setText(valnull);  
                pst.setNull (12, Types.INTEGER);
            } else {  
                pst.setInt(12, Integer.valueOf(txIBGE.getText()));  
            }
klebson_lou

tenta troca o valueof por parseint

if (txIBGE.getText().equals("")) {    
                txIBGE.setText("0");    
                pst.setInt(12, Integer.parseInt(txIBGE.getText()));    
            } else {    
                pst.setInt(12, Integer.parseInt(txIBGE.getText()));    
            }
E

klebson lou:
tenta troca o valueof por parseint

if (txIBGE.getText().equals("")) {    
                txIBGE.setText(valnull);    
                pst.setInt(12, Integer.parseInt(txIBGE.getText()));    
            } else {    
                pst.setInt(12, Integer.parseInt(txIBGE.getText()));    
            }

valueOf chama parseInt e vai dar o mesmo erro :slight_smile: - neste caso o erro é ululantemente óbvio, porque você está passando em txIBGE um valor vazio, e está tentando fazer um valueOf ou parseInt de um valor vazio. Não adianta forçar a barra :frowning:

klebson_lou

entanglement:
klebson lou:
tenta troca o valueof por parseint

if (txIBGE.getText().equals("")) {    
                txIBGE.setText(valnull);    
                pst.setInt(12, Integer.parseInt(txIBGE.getText()));    
            } else {    
                pst.setInt(12, Integer.parseInt(txIBGE.getText()));    
            }

valueOf chama parseInt e vai dar o mesmo erro :slight_smile: - neste caso o erro é ululantemente óbvio, porque você está passando em txIBGE um valor vazio, e está tentando fazer um valueOf ou parseInt de um valor vazio. Não adianta forçar a barra :frowning:

entao em vez de passar null passa 0;

if (txIBGE.getText().equals("")) {      
                txIBGE.setText("0");      
                pst.setInt(12, Integer.parseInt(txIBGE.getText()));      
            } else {      
                pst.setInt(12, Integer.parseInt(txIBGE.getText()));      
            }
S

entanglement

Obrigado pela ajuda

fiz o que voc falou mas continua o erro

run:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:504)
	at java.lang.Integer.valueOf(Integer.java:582)
	at empresarial.JFTransportadora.altera(JFTransportadora.java:700)
	at empresarial.JFTransportadora.jbSalvarActionPerformed(JFTransportadora.java:818)
	at empresarial.JFTransportadora.access$200(JFTransportadora.java:20)
	at empresarial.JFTransportadora$3.actionPerformed(JFTransportadora.java:123)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
	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:6505)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
	at java.awt.Component.processEvent(Component.java:6270)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4861)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
	at java.awt.Container.dispatchEventImpl(Container.java:2273)
	at java.awt.Window.dispatchEventImpl(Window.java:2719)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
	at java.awt.EventQueue.access$000(EventQueue.java:102)
	at java.awt.EventQueue$3.run(EventQueue.java:662)
	at java.awt.EventQueue$3.run(EventQueue.java:660)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.awt.EventQueue$4.run(EventQueue.java:676)
	at java.awt.EventQueue$4.run(EventQueue.java:674)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
E

Eu não postei todas as modificações necessárias porque gostaria de ver se você entendeu o que tem de ser feito. No seu caso, eu mudei a forma de tratar txIBGE mas tem uma coisa parecida aqui em baixo que você tem de mudar:

// Seu código...
...
if (txCEP.getText().equals("")) {  
                txCEP.setText(valnull);  
                pst.setInt(5, Integer.valueOf(txCEP.getText()));  
            } else {  
                pst.setInt(5, Integer.valueOf(txCEP.getText()));  
            } 
...
if (txNatureza.getText().equals("")) {  
                txNatureza.setText(valnull);  
                pst.setInt(22, Integer.valueOf(txNatureza.getText()));  
            } else {  
                pst.setInt(22, Integer.valueOf(txNatureza.getText()));  
            }

Fica por sua conta você fazer a modificação que tem de ser feita.

S

entanglement e klebson lou obrigado pela ajuda

entanglement

sim eu troquei de todos

klebson lou

obrigado pela ajuda

fiz todas as ideias que voces me passaram continua o erro =T

run: Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:504) at java.lang.Integer.valueOf(Integer.java:582) at empresarial.JFTransportadora.altera(JFTransportadora.java:699) at empresarial.JFTransportadora.jbSalvarActionPerformed(JFTransportadora.java:817) at empresarial.JFTransportadora.access$200(JFTransportadora.java:20) at empresarial.JFTransportadora$3.actionPerformed(JFTransportadora.java:123) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 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:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703) at java.awt.EventQueue.access$000(EventQueue.java:102) at java.awt.EventQueue$3.run(EventQueue.java:662) at java.awt.EventQueue$3.run(EventQueue.java:660) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:676) at java.awt.EventQueue$4.run(EventQueue.java:674) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:673) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

S

o mais estranhu q se eu por o numero 0 em todos os campos da o mesmo erro

F

Como tá o método altera?

E

Amigo, qual é a linha 699 do arquivo JFTransportadora.java ?

S

pst.setInt(1, Integer.valueOf(txCodigo.getText()));

é da linha de alterar

achu q o erro ta no meu if de gravar

esse é meu if pra chamar o grava ou alterera

if (txRazao_Nome.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Você precisa digitar uma RAZÃO SOCIAL.");
        } else {
            if (txNatureza.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Você precisa preencher a NATUREZA DA OPERAÇÃO.");
            } else {
                if (txCNPJ.getText().equals("")) {
                    if (alt = false) {
                        grava();
                    } else {
                        altera();
                    }
                } else {
                }
                if (alt = false) {
                    grava();
                } else {
                    altera();
                }
            }
        }
F

Seu campo txtCodigo está com o valor = “”. . . . . .

S

felipeaps

pois é depois de notar isso eu vi q relamente o problema é no if q chama afunção gravar ou alterar

tava chamando sempre a função alterar dae como nao tinha código dava o erro.

E

Cuidado que o compilador Java não lhe avisa que você fez uma bobagem. Nesse caso, você teria de usar “==”, não “=”.

if (alt == false) {  
                 grava();  
             } else {  
                 altera();  
             }  
         } else {  
         }  
         if (alt == false) {  
             grava();  
         } else {  
             altera();  
         }
  1. Hum… depois que eu vi. Seu código parece que tem aquela doença do esquecimento - você já checou se alt é false, por que checar de novo?

  2. Para checar se alguma coisa é true ou false, para evitar dores de cabeça, use:

// para checar se é true
if (alt)

e

// para checar se é false
if ( ! alt)

Além disso, é mais bonitinho que comparar explicitamente com “true” ou “false”.

S

entanglement

vlw pela dica essa eu nao sabia

mas ta testando 2 x pq sao pates diferentes do if,

um vai verificar se campo CNPJ diger sido digitado vai pra um teste de validação passando o tesnte grava ou altea

se nao ele so grava ou altera.

A assim o usuario pode gravar mesmo com o campo em branco.

Criado 29 de junho de 2012
Ultima resposta 3 de jul. de 2012
Respostas 15
Participantes 4