将datetime从sqlite解析为java.sql.timestamp时出错

oaxa6hgo  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(988)

我正在使用Java11和SQLite3.30.1制作一个桌面WindowsPC应用程序。在我的sql表“alarms”中有一个date(datetime)列,我尝试使用jdbc resultset.gettimestamp()检索它。但是我得到了以下错误:“java.sql.sqlexception:error parsing time stamp”。因为这是一个报警管理器,我想存储日期+时间。我也希望能够减去2个日期,一个从另一个。这是我的闹钟:

CREATE TABLE "alarms" (
"id"    INTEGER NOT NULL,
"date"  DATETIME NOT NULL,
"name"  TEXT,
"text"  TEXT NOT NULL,
PRIMARY KEY("id")
);

这是我的java类:

import java.io.Serializable;
import java.sql.Timestamp;

public class Alarms implements Serializable {

private int id;

private Timestamp date;

private String name;

private String text;

public Alarms() {
}

public Alarms(int id, Timestamp date, String name, String text) {
    this.id = id;
    this.date = date;
    this.name = name;
    this.text = text;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Timestamp getDate() {
    return date;
}

public void setDate(Timestamp date) {
    this.date = date;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public String toString() {
    return name;
}
}

这是我的java jdbc方法:

public ObservableList<Alarms> getAlarms() {

    alarmsList.clear();
    try {
            s = "SELECT * FROM alarms";

        statement = connection.createStatement();
        resultSet = statement.executeQuery(s);
        while (resultSet.next()) {

            Alarms al = new Alarms();
            al.setId(resultSet.getInt(1));
            al.setDate(resultSet.getTimestamp(2)); 
            al.setName(resultSet.getString(3));
            al.setText(resultSet.getString(4));

            alarmsList.add(al);

        }
        return alarmsList;

    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
    return null;
}

所以问题是,如何使用jdbc在sqlite中存储、插入和检索日期+时间?我做错什么了?

sulc1iza

sulc1iza1#

根本没有 DATETIME 根据规范键入sqlite。
它将被转换为 NUMERIC . 您可以在请求中使用各种函数来获得所需的内容:日期和时间函数

67up9zun

67up9zun2#

我找了一整天才找到解决办法。在sqlite表中,可以使用datetime来存储date+time。sqlite将它存储为字符串。我的table如下:

CREATE TABLE "alarms" (
"id"    INTEGER NOT NULL,
"date"  DATETIME NOT NULL,
"name"  TEXT,
"text"  TEXT NOT NULL,
PRIMARY KEY("id")
);

由于sqlite返回一个字符串,因此必须在java中的结果集上使用“getstring()”。这个字符串是一个很长的数字,比如15883704060005。因此,我不得不在java类中添加一些额外的getter和setter。这是我的报警java类:

import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

public class Alarms implements Serializable {

private int id;

private LocalDateTime date;

private String name;

private String text;

public Alarms() {
}

public Alarms(int id, LocalDateTime date, String name, String text) {
    this.id = id;
    this.date = date;
    this.name = name;
    this.text = text;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public LocalDateTime getDate() {
    return date;
}

public String getDateStringFormat(){

    return Long.toString(date.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

}

public void setDate(LocalDateTime date) {
    this.date = date;
}

public void setDate(String stringDate){

    date= Instant.ofEpochMilli(Long.parseLong(stringDate)).atZone(ZoneId.systemDefault()).toLocalDateTime();

}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public String toString() {
    return name;
}
}

我的jdbc方法是:

public ObservableList<Alarms> getAlarms() {

    alarmsList.clear();
    try {
            s = "SELECT * FROM alarms";

        statement = connection.createStatement();
        resultSet = statement.executeQuery(s);
        while (resultSet.next()) {

            Alarms al = new Alarms();
            al.setId(resultSet.getInt(1));
            al.setDate(resultSet.getString(2));
            al.setName(resultSet.getString(3));
            al.setText(resultSet.getString(4));

            alarmsList.add(al);

        }
        return alarmsList;

    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
    return null;
}

希望这能帮助别人。

相关问题