I have some doubts about this one, I already saw the others topics but I still can't make my code work properly.
I want to send dates into my DB using JDBC, I'm using a jTextField (or a JformattedField, trying both ways) and all I get is an error by the database, Because I'm sending text instead date, so, how can I send dates ?
EDIT1: I'm trying to send date of birth.
Already made some tries, that's why I have: SimpleDateFormat formatter = new SimpleDateFormat ("dd/MM/yyyy"); java.sql.Date data = new java.sql.Date(formatter.parse().getTime());
try {
conecta.conn.setAutoCommit(false);
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
pst.setString(3, jTextFieldIDADE.getText()); //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.sql.Date data = new java.sql.Date(formatter.parse().getTime());
pst.setDate(5, formatter.parse(jFormattedTextFieldDATA.getText())); //pega o texto insirido e armazena no banco de dados
pst.executeUpdate(); //executa o SQL
conecta.conn.commit();
jFormattedTextFieldCPF.setText(""); //deixa o campo vazio
jTextFieldNOME.setText(""); //deixa o campo vazio
jTextFieldIDADE.setText(""); //deixa o campo vazio
jFormattedTextFieldDATA.setText("");
jTextFieldApelido.setText(""); //deixa o campo vazio
jFormattedTextFieldCPF.setEnabled(false);
jTextFieldNOME.setEnabled(false);
jTextFieldIDADE.setEnabled(false);
jFormattedTextFieldDATA.setEnabled(false);
jTextFieldApelido.setEnabled(false);
JOptionPane.showMessageDialog(null, "Salvo!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(rootPane, "Erro!\n" ex);
}
CodePudding user response:
The setInt()
method that you use is intended for integer values, and it accepts an integer. To set the date, use the PreparedStatement.setDate()
method.
The java.util.Date
you get from the parse()
method should also be converted to java.sql.Date
.
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date utilDate = formatter.parse(jFormattedTextFieldDATA.getText());
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
pst.setDate(5, sqlDate);
See the documentation on the PreparedStatement
class: https://docs.oracle.com/javase/9/docs/api/java/sql/PreparedStatement.html
CodePudding user response:
pst.setInt(5, formatter.parse(jFormattedTextFieldDATA.getText()));
This is the culprit line. See you are using setInt() method in order to set a date. That is absolutely incorrect. Use setDate() method instead.
eg: pst.setDate(5, formatter.parse(jFormattedTextFieldDATA.getText()));
CodePudding user response:
For the future people who will have the same doubt, here's how it's my code ended after fixing
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
pst.setString(3, jTextFieldIDADE.getText()); //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(Pessoa.class.getName()).log(Level.SEVERE, null, ex);
}
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
pst.setDate(5, sqlDate);
pst.executeUpdate(); //executa o SQL
conecta.conn.commit();
I appreciate everyone who tried to help a newbie like me.
CodePudding user response:
Avoid legacy date-time classes
Never use java.until.Date
, java.sql.Date
, SimpleDateFormat
and such. These legacy date-time classes are terrible, tragically flawed in design.
The legacy classes were supplanted years ago by the modern java.time classes defined in JSR 310.
java.time.LocalDate
Parse your input string as a LocalDate
.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.parse ( input , f ) ;
Pass to your database via your prepared statement.
myPreparedStatement.setObject( … , ld ) ;
To retrieve from the database:
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
In Postgres, you would define your column to be of type DATE
.
Use a JDBC driver compliant with version 4.2 or later of the JDBC specification.
All of this has been covered many many times on Stack Overflow. Search to learn more.