Reputation: 63
I'm trying to update a table, trying to input a integer
(age).
First I had the same problem with insert
, them, after some answers here, I could manage to work properly, but, using the same solution on a update
command, doesn't work.
Here's the solution for insert
:
PreparedStatement pst = conecta.conn.prepareStatement("INSERT INTO cad_pessoa(cad_cpf,cad_nome, cad_idade, cad_apelido, cad_data) values (?,?,?,?,?)");
pst.setString(1, jFormattedTextFieldCPF.getText()); //pega o texto insirido e armazena no banco de dados
pst.setString(2, jTextFieldNOME.getText()); //pega o texto insirido e armazena no banco de dados
int n1 = Integer.parseInt(jTextFieldIDADE.getText());
pst.setInt(3, n1); //pega o texto insirido e armazena no banco de dados
pst.setString(4, jTextFieldAPELIDO.getText());//pega o texto insirido e armazena no banco de dados
SimpleDateFormat formatter = new SimpleDateFormat ("dd-MM-yyyy");
java.util.Date utilDate = null;
try {
utilDate = formatter.parse(jFormattedTextFieldDATA.getText());
} catch (ParseException ex) {
Logger.getLogger(Pessoa3.class.getName()).log(Level.SEVERE, null, ex);
}
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
pst.setDate(5, sqlDate);
pst.executeUpdate();
The problem with the update
is: Column is in type Integer but expression is of type character varying.
Here's the code for update
:
PreparedStatement pst = conecta.conn.prepareStatement("update cad_pessoa set cad_nome= ?, cad_idade=?, cad_apelido= ? where cad_cpf= ?");
pst.setString(1, jTextFieldNOME.getText()); //pega o texto insirido e armazena no banco de dados
pst.setString(2, jTextFieldIDADE.getText()); //pega o texto insirido e armazena no banco de dados
int n1 = Integer.parseInt(jTextFieldIDADE.getText()); //string to int
pst.setInt(3, n1); //pega o texto insirido e armazena no banco de dados
pst.setString(4, jFormattedTextFieldCPF.getText()); //pega o texto insirido e armazena no banco de dados
pst.executeUpdate(); //executa o SQL
pst.execute();
So, how can I make this work?
Upvotes: 0
Views: 405
Reputation: 308928
Here are a few suggestions:
It's bad when I see Swing and database code intermingled. Keep them strictly separate. Your app should be constructed in layers.
I wouldn't persist age, because it changes every day. I'd persist birthday and calculate age when I needed it.
You don't say which statement gives you the error. I assume it's here:
int n1 = Integer.parseInt(jTextFieldIDADE.getText()); //string to int
pst.setInt(3, n1); //pega o texto insirido e armazena no banco de dados
The variable cad_apeli
is the fourth argument in your INSERT, but it's the third in your UPDATE. I'm guessing that you've got some confusion about columns.
Upvotes: 1