【问题标题】:Java derby embedded DB error: The syntax of the string representation of a date/time value is incorrectJava derby Embedded DB 错误:日期/时间值的字符串表示的语法不正确
【发布时间】:2018-12-10 22:19:15
【问题描述】:

在我的应用程序中使用 derby 嵌入式数据库时遇到以下问题:

我更改了一个与 Mysql 数据库一起使用的程序以使用 Derby 嵌入式数据库,因为由于 Internet 问题,客户想要这样,一切正常,但是当我想使用一系列日期进行搜索时控制台显示下一个错误:

2018-12-10 17:26:18.920  INFO 107044 --- [nio-7009-exec-3] 

C.wDatos_ControlTransferencias_Servicios : /transferencias/consultar
2018-12-10 17:26:18.970  WARN 107044 --- [nio-7009-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 30000, SQLState: 22007
2018-12-10 17:26:18.970 ERROR 107044 --- [nio-7009-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : The syntax of the string representation of a date/time value is incorrect.
2018-12-10 17:26:19.009 ERROR 107044 --- [nio-7009-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute query] with root cause

org.apache.derby.iapi.error.StandardException: The syntax of the string representation of a date/time value is incorrect.

就像我在使用 MySql 时所说的那样,它可以完美运行。

我使用的查询是这样的:

@SuppressWarnings("unchecked")
    @RequestMapping(value = "/transferencias/consultatodos", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @CrossOrigin
    @RequestScope
    Collection<Transferencias> transferenciasConsultastodos(@RequestBody Consulta conf) throws JsonProcessingException {
        List<Transferencias> transl = new ArrayList<>();
        log.info("/transferencias/consultar");
        String Query = "Select trans from Transferencias trans";
        if (conf.getFecha1() != null && conf.getFecha2() != null) {
            Query = Query + " WHERE trans.fecha >= '" + conf.getFecha1() + "' AND trans.fecha<= '"+conf.getFecha2()+"'";
             if (conf.getBanco() != null) {
                Query = Query + " AND trans.banco = '" + conf.getBanco() +"'";
            }
             if (conf.getBeneficiario() != null) {
                Query = Query + " AND trans.beneficiario = '" + conf.getBeneficiario() +"'";
            }
             if (conf.getTipo() != null) {
                Query = Query + " AND trans.tipo = '" + conf.getTipo() +"'";
            }
        }
        else {
            if (conf.getBanco() != null) {
                Query = Query + " WHERE trans.banco = '" + conf.getBanco() +"'";
                if (conf.getBeneficiario() != null) {
                    Query = Query + " AND trans.beneficiario = '" + conf.getBeneficiario() +"'";
                }
                if (conf.getTipo() != null) {
                    Query = Query + " AND trans.tipo = '" + conf.getTipo() +"'";
                }
            }
            else {
                if (conf.getBeneficiario() != null) {
                    Query = Query + " WHERE trans.beneficiario = '" + conf.getBeneficiario() +"'";
                    if (conf.getTipo() != null) {
                        Query = Query + " AND trans.tipo = '" + conf.getTipo() +"'";
                    }
                }
                else {
                    if (conf.getTipo() != null) {
                        Query = Query + " WHERE trans.tipo = '" + conf.getTipo() +"'";
                    }
                }
            }
        }
        transl = em.createQuery(Query).getResultList();
        return transl;
    }

当我使用其他参数进行搜索时,它工作得很好,问题出在日期上。

实体:

package ControlTransferencias.wDatos;



import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
 *
 * @author juan.finol
 */
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Transferencias {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    private Long referencia;
    private String beneficiario;
    private String banco;
    private Date fecha;
    private String notaAdicional;
    private String descripcion;
    private Long monto;
    private String tipo;

    public String getTipo() {
        return tipo;
    }
    public void setTipo(String tipo) {
        this.tipo = tipo;
    }
    public Long getReferencia() {
        return referencia;
    }
    public void setReferencia(Long referencia) {
        this.referencia = referencia;
    }
    public String getBeneficiario() {
        return beneficiario;
    }
    public void setBeneficiario(String beneficiario) {
        this.beneficiario = beneficiario;
    }
    public String getBanco() {
        return banco;
    }
    public void setBanco(String banco) {
        this.banco = banco;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getDescripcion() {
        return descripcion;
    }
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }
    public Date getFecha() {
        return fecha;
    }
    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }
    public Long getMonto() {
        return monto;
    }
    public void setMonto(Long monto) {
        this.monto = monto;
    }
    public String getNotaAdicional() {
        return notaAdicional;
    }
    public void setNotaAdicional(String notaAdicional) {
        this.notaAdicional = notaAdicional;
    }

}

我注意到的另一件事是在前端,当我进行搜索时,它会显示如下日期:1544414400000,而当我使用 MySql 时,它会显示 dd-mm-yyyy 格式。

注意:后端是用spring boot制作的。

【问题讨论】:

    标签: java mysql spring-boot derby


    【解决方案1】:

    确实,跨数据库系统的日期和时间数据类型的默认字符串格式没有标准化,因此使用文本字符串格式的日期发出 SQL 查询是非常不可移植的。

    一种更便携的方法是使用 JDBC PreparedStatement 对象及其 setDate() 方法,将日期值放入查询中。

    使用PreparedStatement 及其参数替换功能还具有极大的好处,可以使您的程序更不容易受到 SQL 注入漏洞的影响。

    在这里查看优秀的文档:https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

    【讨论】:

    • 在将我的代码更改为使用 PreparedStatement 并使用 Timestamp 而不是 Date 后,问题得到了解决,但我在前端仍然遇到问题,当它以不同的格式显示日期时,例如页面显示“1548820800000”而不是“30-01-2019”
    【解决方案2】:

    @phantomsnake 这里是一些格式化日期的代码

       DateTimeFormatter mon = DateTimeFormatter.ofPattern("MMMM");
    

    DateTimeFormatter day = DateTimeFormatter.ofPattern("d"); DateTimeFormatter 年份 = DateTimeFormatter.ofPattern("yyyy"); DateTimeFormatter dayOFwk = DateTimeFormatter.ofPattern("EEEE"); DateTimeFormatter mdwd = DateTimeFormatter.ofPattern("MMMM d EEEE"); 现在 LocalDateTime = LocalDateTime.now();

    System.out.println(mon.format(now));
    
    System.out.println(day.format(now));
    
    System.out.println(year.format(now));
    
    System.out.println(dayOFwk.format(now));
    
    System.out.println(mdwd.format(now));
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 2023-04-02
      • 2019-09-19
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      相关资源
      最近更新 更多