How to pass data from a Jtextfield to a Postgresql Date field

Asked

Viewed 81 times

0

I urge you to help me solve a problem of entering data type Date (SQL) to register new students in the Crud project developed by me in the code below. The method containing the INSERT INTO command does not insert new registration in the database because the text typed in the dd/MM/yyyy format obtained in Jtextfield is not accepted as Date format data in SQL.

package grafico;

import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type.String;
import java.sql.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import sistema.Conectora;

public class Tela extends javax.swing.JFrame {

    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    public Tela() {
        initComponents();
        setLocationRelativeTo(null);
        c = Conectora.conectora();

    }

    public void cadastrar() throws ParseException{
        String consulta = "INSERT INTO alunos (aluno, , nascimento, mae, pai, sexo, serie) VALUES (?,?,?,?,?,?);";




        try {
            ps = c.prepareStatement(consulta);
            ps.setString(1,campoAluno.getText());
            ps.setDate(2, new Date(campoNascimento.getText()));
            ps.setString(3,campoMae.getText());
            ps.setString(4,campoPai.getText());
            ps.setString(5,campoSexo.getText());
            ps.setInt(6,(Integer.parseInt(campoSerie.getText())));
            ps.execute();
            JOptionPane.showMessageDialog(null,"Cadastrado com sucesso!");
        }catch(SQLException e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
  • 1

    You haven’t filed a code that is [mcve] And not even the problem faced was explained properly, so you can’t draw too many specific conclusions. But striking my eye at your code, I realized there’s a mistake right here in the dates, which has already been answered here.

  • I’m sorry, I’ve already worked on the question. But the problem solved in the similar question to which you associated mine as duplicate is a little different, because in the case of the question of the other user the obtained date is that of the system, already in my situation, it is a text obtained from a Jtextfield.

  • It has also been answered on the site, see the other question added the duplicates

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.