Get DATE column of the JTable to save it in SQL

0

I have:

  • SQL Server 2017
  • I have jcalendar-1.4 added to my project
  • I have import java.sql.Date; in my class.
  • I have the column fechaNacimiento type DATE in my database.

To obtain the information from the database and put together the model of JTable use:

String [] titulosColumnas = {"ID", "Nombre", "FechaNacimiento"};
            ArrayList<Object[]> registros = new ArrayList<Object[]>();

            while (rs.next()) {
                registros.add(new Object[] {
                    rs.getInt("id_director"),
                    rs.getString("nombre"),
                    // Aquí paso la información obtenida a String, porque JTable solo permite String's, cierto?.
                    rs.getDate("fechaNacimiento").toString()
                });
            }

            model = new DefaultTableModel(registros.toArray(new Object[0][]), titulosColumnas);

My resulting JTable:

At this moment, when I click on a row of the JTable, I receive the error:

  

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:   java.util.Date can not be cast to java.sql.Date at   views.Director_View $ 7.mouseClicked (Vista_director.java:272)

JTable code when you click:

table = new JTable();
        scrollPane.setViewportView(table);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                int fila = table.rowAtPoint(arg0.getPoint());
                datoDirector.setId(Integer.parseInt(table.getValueAt(fila, 0).toString()));
                datoDirector.setNombre(table.getValueAt(fila, 1).toString());
                // Obtener la columna Fecha Nacimiento
                Date date;
                try {
                    date = (Date) new SimpleDateFormat("yyyy-MM-dd").parse((String)table.getValueAt(fila, 2));
                    datoDirector.setFechaNacimiento(date);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

I want to get the date as type DATE to save it in a local variable type DATE as well (I'll place it in a JDateChooser with its function .set ()).

  • What is the correct way to get the String that has the JTable and pass it to DATE type?
  • The use of rs.getDate("fechaNacimiento").toString() is correct or can I eliminate the .toString() ?
  • asked by Robert Gomez 11.11.2017 в 05:28
    source

    1 answer

    0
    date = (Date) new SimpleDateFormat("yyyy-MM-dd").parse((String)table.getValueAt(fila, 2));
    

    the error must exist in this line, do that procedure in several lines and test.

    or instead of using the .parse (), use the .format () of the SimpleDateFormat class, and reference the cell in the table as you currently do.

    date = new SimpleDateFormat("yyyy-MM-dd").format(table.getValueAt(fila, 2));
    

    in this way, try it and write how it went.

        
    answered by 12.11.2017 в 02:59