不支持的操作异常Java.Sql.Date

时间:2018-10-22 03:48:04

标签: javafx

您好,我需要在表格视图中显示日期列;在数据库中,我将字段fecha_nacimiento声明为date,我使用了datepicker将该数据插入数据库,到目前为止该操作还可以,因此下一步是格式化表格视图单元格可以正确显示日期数据,在此站点的帮助下,我做到了,但是当我需要从数据库中检索日期数据时,我得到了一个错误,像这样:

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

这是我的controller代码

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();
              }

         }

    }

这是我的persona class代码

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一直在读取,我发现无法截断或传递Localdate和Date之间的数据,因此任何帮助都将真正有用。问候。

这里的所有错误:

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

1 个答案:

答案 0 :(得分:0)

java.sql.Date是一个不包含时间信息的java.util.Date子类,因此您无法将其转换为Instant类。

幸运的是,有一个toLocalDate方法可以做到这一点,将其直接转换为LocalDate。

应该是这样的:

rs.getDate(4).toLocalDate();

有关更多信息,请查看JavaDoc https://docs.oracle.com/javase/8/docs/api/java/sql/Date.html#toInstant--