-2

hello i need to display a date column in a tableview; in the database I declare the field fecha_nacimiento as date , i used a datepicker to insert that data into the database, so far its Ok with that operation so the next step was formatting the tableview cell to display the date data correctly, with the help of this site i did that, but when i need to retrieve the date data from the database i am getting an error, something like this:

Caused by: java.lang.UnsupportedOperationException
    at java.sql.Date.toInstant(Unknown Source)

this is some of my controller code

public void initialize(URL arg0, ResourceBundle arg1) {
        clienteid.setCellValueFactory(new PropertyValueFactory <Persona, Integer>("id_cliente"));
        nombrescol.setCellValueFactory(new PropertyValueFactory <Persona, String>("nombres"));
         apellidoscol.setCellValueFactory(new PropertyValueFactory <Persona, String>("apellidos"));
        //fechacli.setCellValueFactory(new PropertyValueFactory <Persona, LocalDate>("fechacliente"));//

         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

         fechacli.setCellFactory(column -> {
             return new TableCell<Persona, LocalDate>() {
                 @Override
                 protected void updateItem(LocalDate item, boolean empty) {
                     super.updateItem(item, empty);

                     if (item == null || empty) {
                         setText(null);
                     } else {
                         setText(formatter.format(item));

                     }
                 }
             };
         });

         seleccionaregistros();
         seleccionanombre();
         seleccionapellido(); 
    }



public void seleccionaregistros() {
        ObservableList <Persona> data =FXCollections.observableArrayList();
          Connection conn=null;{
              try {

                 conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=prueba", "sa", "milkas87");
                  Statement mostrar=conn.createStatement();
                  ResultSet rs;
                  rs= mostrar.executeQuery("select * from cliente");


                  while ( rs.next() ) 
                  {
                     data.add(new Persona(

                             rs.getString("nombre"),
                             rs.getString("apellido"),
                             rs.getInt("id"),
                             rs.getDate(4).toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
                             );
                     tablacliente.setItems(data);
                  }

              } catch (SQLException e) {
                  e.printStackTrace();
              }

         }

    }

this is my persona class code

package application;


import java.time.LocalDate;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Persona {



    private StringProperty nombres;
    private StringProperty apellidos;
    private IntegerProperty id_cliente;
    private ObjectProperty <LocalDate>fechacliente;

    public Persona (String nombres, String apellidos, Integer id_cliente, LocalDate fechacliente) {
        this.nombres=  new SimpleStringProperty (nombres);
        this.apellidos= new SimpleStringProperty ( apellidos);
        this.id_cliente=new SimpleIntegerProperty (id_cliente);
        this.fechacliente= new SimpleObjectProperty<>(fechacliente);
    }


public LocalDate getFechaCliente() {
        return fechacliente.get();
}
public void setFechaCliente(LocalDate fechacliente) {
        this.fechacliente = new SimpleObjectProperty<>(fechacliente);
}
public ObjectProperty<LocalDate> fechaClienteProperty() {
        return fechacliente;
}

public String getNombres() {
    return nombres.get();
}

public  void  setNombres(String nombres) {
    this.nombres=new SimpleStringProperty (nombres);
}


public String getApellidos() {
    return apellidos.get();
}

public  void  setApellidos(String apellidos) {
    this.apellidos=new SimpleStringProperty ( apellidos);
}


public Integer getId_cliente() {
    return id_cliente.get();
}

public  void  setid_cliente(Integer id_cliente) {
    this.id_cliente=new SimpleIntegerProperty (id_cliente);
}

}

id have been reading, and i found that can't truncate or pass the data beetween Localdate and Date, so any help could be truly helpful. regards.

all the errors here:

javafx.fxml.LoadException: 
/C:/Users/ROA%20PC/eclipse-workspace/Conexion/bin/application/Vista.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.Main.start(Main.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsupportedOperationException
    at java.sql.Date.toInstant(Unknown Source)
    at application.ConexionController.seleccionaregistros(ConexionController.java:190)
    at application.ConexionController.initialize(ConexionController.java:92)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Root cannot be null
    at javafx.scene.Scene.<init>(Scene.java:336)
    at javafx.scene.Scene.<init>(Scene.java:235)
    at application.Main.start(Main.java:27)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application application.Main
Jhon Mark
  • 3
  • 2
  • 7
  • still not learned what a [mcve] is? As the stacktrace tells you, there's a type mismatch - so distill that into a couple of lines (simple main, no ui, no custom data class) to work out what exactly is wrong. – kleopatra Oct 22 '18 at 08:32

1 Answers1

0

The java.sql.Date is a java.util.Date subclass that holds no time information, so you can’t convert it to an Instant class.

Fortunately, there is a toLocalDate method that does just that, convert it directly to LocalDate.

It should be like this:

rs.getDate(4).toLocalDate();

For further reference take a look at the JavaDoc https://docs.oracle.com/javase/8/docs/api/java/sql/Date.html#toInstant--

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Tomaz Fernandes
  • 2,429
  • 2
  • 14
  • 18