【问题标题】:Unsupported Operation Exception Java.Sql.Date [duplicate]不支持的操作异常 Java.Sql.Date [重复]
【发布时间】:2019-03-26 02:29:53
【问题描述】:

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

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

【问题讨论】:

  • 还没有了解minimal reproducible example 是什么?正如堆栈跟踪告诉您的那样,存在类型不匹配 - 所以将其提炼成几行(简单的 main、没有 ui、没有自定义数据类)以找出究竟是什么错误。

标签: javafx


【解决方案1】:

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--

【讨论】:

  • 有什么例子吗?我还不是这样的专家。问候。 @Tomaz Fernandes
  • 用一个例子更新了答案,如果你还需要什么,请告诉我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 2021-11-21
  • 1970-01-01
  • 2021-11-05
相关资源
最近更新 更多