Erro no Insert

3 respostas
M

Olá, pessoal!

Estou fazendo um cadastro simples de cidade, onde aparece para cadastro o nome da cidade (JTextField) e o estado (JComboBox).
Quando tento fazer a inserção dos dados dá o seguinte erro:

Exception in thread AWT-EventQueue-0 java.lang.ClassCastException: java.lang.String at br.com.sstintas.Cidade$1.actionPerformed(Cidade.java:191)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

a linha 191 é a seguinte:

String sel = "Insert into cidade (id_cidade, estado_id_estado, nome_cidade) values('"+null+"','"+((PegarDados)cbEstado.getItemAt(cbEstado.getSelectedIndex())).getEstado_id()+"', '"+tfNomeCidade.getText()+"')";

Esse insert está dentro de um JButton.

Gostaria de alguma dica para resolver esse problema.

Obrigada!

3 Respostas

Pedrosa

Use PreparedStatement:

String sql = "Insert into cidade (id_cidade, estado_id_estado, nome_cidade) values(?,?,?)";
PreparedStatement stmt = this.connection.prepareStatement(sql);
stmt.setInt(1, "valor1");
stmt.setInt(2, "valor2");
stmt.setString(3, "valor3");
stmt.execute();
M
Pedrosa:
Use PreparedStatement:
String sql = "Insert into cidade (id_cidade, estado_id_estado, nome_cidade) values(?,?,?)";
PreparedStatement stmt = this.connection.prepareStatement(sql);
stmt.setInt(1, "valor1");
stmt.setInt(2, "valor2");
stmt.setString(3, "valor3");
stmt.execute();

O estado é listado num JComboBox o nome do estado, mas quero armazenar o código desse estado na tabela cidade. Eu posso tratá-lo como um int?

Pedrosa

Vc deve colocar o tipo igual ao seu bd:

stmt.setInt(2, 9999);
ou
stmt.setString(2, "valor2");
Criado 2 de maio de 2006
Ultima resposta 2 de mai. de 2006
Respostas 3
Participantes 2