Problema com o botão "gravar"

8 respostas
guiJava

Olá pessoal,
resolvi meu pro com os frames falando com meu professor, mas absorvi muitas coisas com as dicas que recebi neste fórum. Por isso volto aqui pedido a ajuda de vocês agora com o botão grava do meu porjeto.

o código que uso para gravar o que eu quero é este:

if(JOptionPane.showConfirmDialog(this, "Deseja realmente salvar?")==JOptionPane.YES_NO_OPTION){ int cpf=Integer.parseInt(txtCpfF.getText()); Fisico f=dao.localizarCtF(cpf); if(f==null){ f=new Fisico(); f.setCpf(cpf); f.setNome(txtNomeF.getText()); f.setEmail(txtEmailF.getText()); f.setTelefone(txtTelefoneF.getText()); dao.gravarCtF(f); } else{ f.setCpf(Integer.parseInt(txtCpfF.getText())); f.setNome(txtNomeF.getText()); f.setEmail(txtEmailF.getText()); f.setTelefone(txtTelefoneF.getText()); dao.atualizarCtF(f); } limparTexto(); }
assim, no BD o CPF é int… e ele reclama que estou tentanto gravar uma String, o que eu estou fazendo de errado no meu código?

Desdejá meu muito obrigado! :slight_smile:

8 Respostas

aluisiodsv

Como assim reclama?
Dá erro? Ou nem compila?

guiJava

na hora de executar ele não grava, da erro… aparece isso aqui

[EL Info]: 2011-01-03 13:00:19.053ServerSession(3916375)EclipseLink, version: Eclipse Persistence Services - 2.0.0.v20091127-r5931

[EL Info]: 2011-01-03 13:00:20.368ServerSession(3916375)file:/C:/Users/pc/Documents/NetBeansProjects/ProgramaColorPrinter/src/_ProgramaColorPrinterPU login successful

Exception in thread AWT-EventQueue-0 java.lang.NumberFormatException: For input string: [telefone removido]

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java:461)

at java.lang.Integer.parseInt(Integer.java:499)

at bd.colorprinter.gui.FormFisico.btnSalvarFActionPerformed(FormFisico.java:200)

at bd.colorprinter.gui.FormFisico.access$000(FormFisico.java:22)

at bd.colorprinter.gui.FormFisico$1.actionPerformed(FormFisico.java:139)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6263)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

at java.awt.Component.processEvent(Component.java:6028)

at java.awt.Container.processEvent(Container.java:2041)

at java.awt.Component.dispatchEventImpl(Component.java:4630)

at java.awt.Container.dispatchEventImpl(Container.java:2099)

at java.awt.Component.dispatchEvent(Component.java:4460)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)

at java.awt.Container.dispatchEventImpl(Container.java:2085)

at java.awt.Window.dispatchEventImpl(Window.java:2478)

at java.awt.Component.dispatchEvent(Component.java:4460)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

P.S.: estou usando o IDE NetBeans 6.8

aluisiodsv

int: The int data type is a 32-bit signed two’s complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

O número do cpf não cabe em uma variável do tipo int. Por isso dá uma erro na conversão da string para o int.

Use o tipo long.

Master.Abs

aluisiodsv:
int: The int data type is a 32-bit signed two’s complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

O número do cpf não cabe em uma variável do tipo int. Por isso dá uma erro na conversão da string para o int.

Use o tipo long.

Está correto altera para o tipo long que aceita um tamanho muito maior.
Flw

guiJava

Bom pessoal meu problema agora é outro. Na hora que eu vou gravar um objeto isto é apresentado:

Internal Exception: java.sql.SQLIntegrityConstraintViolationException: A coluna ENDERECO não aceita valor nulo.

Error Code: -1

Call: INSERT INTO FISICO (CPF, EMAIL, TELEFONE, NOME, RG, ENDERECO) VALUES (?, ?, ?, ?, ?, ?)

Exception in thread AWT-EventQueue-0 javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException

Internal Exception: java.sql.SQLIntegrityConstraintViolationException: A coluna ENDERECO não aceita valor nulo.

Error Code: -1

Call: INSERT INTO FISICO (CPF, EMAIL, TELEFONE, NOME, RG, ENDERECO) VALUES (?, ?, ?, ?, ?, ?)

bind => [[telefone removido], teste@teset, 234434544, teste, 345567857, null]

Query: InsertObjectQuery(bd.colorprinter.model.programacp.Fisico[cpf=[telefone removido]])

bind => [[telefone removido], teste@teset, 234434544, teste, 345567857, null]

at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)

Query: InsertObjectQuery(bd.colorprinter.model.programacp.Fisico[cpf=[telefone removido]])

Por favor quem puder me ajudar fico muito grato…

o código que eu uso pra gravar é este aqui:

if(JOptionPane.showConfirmDialog(this, "Deseja realmente salvar?")==JOptionPane.YES_NO_OPTION){ Fisico f=dao.localizarCtF(txtCpfF.getText()); if(f==null){ f=new Fisico(); f.setCpf(txtCpfF.getText()); f.setNome(txtNomeF.getText()); f.setRg(txtRgF.getText()); f.setEmail(txtEmailF.getText()); f.setTelefone(txtTelefoneF.getText()); dao.gravarCtF(f); } else{ f.setCpf(txtCpfF.getText()); f.setNome(txtNomeF.getText()); f.setRg(txtRgF.getText()); f.setEmail(txtEmailF.getText()); f.setTelefone(txtTelefoneF.getText()); dao.atualizarCtF(f); } limparTexto();

gustavoliveira

amigo eu acho que vc esqueceu de setar o endereço por exemplo

f.setEndereco(seu endereço)

Por isso que está null!

guiJava

vixi foi mesmo oh! Vlw’s cara…

gustavoliveira

Que isso… :-o abraço!

Criado 3 de janeiro de 2011
Ultima resposta 4 de jan. de 2011
Respostas 8
Participantes 4