我在应用程序中使用derby embedded db时遇到以下问题:
我将一个与mysql数据库一起工作的程序改为使用derby嵌入式数据库,因为由于互联网问题,客户端希望这样做,一切正常,但当我想使用一系列日期进行搜索时,控制台显示下一个错误:
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。
2条答案
按热度按时间2ledvvac1#
@幻影蛇这里有一些代码来格式化日期
datetimeformatter day=模式的datetimeformatter.of(“d”);datetimeformatter year=模式的datetimeformatter.of(“yyyy”);datetimeformatter dayofwk=模式的datetimeformatter.of(“eeee”);datetimeformatter mdwd=datetimeformatter.of模式(“mmmm d eeee”);localdatetime now=localdatetime.now();
3z6pesqy2#
事实上,对于跨数据库系统的日期和时间数据类型,没有默认字符串格式的标准化,因此用文本字符串格式的日期发出sql查询是非常不可移植的。
一种更便于移植的方法是使用jdbc
PreparedStatement
对象及其setDate()
方法将日期值放入查询中。使用
PreparedStatement
它的参数替换特性还有一个很好的好处,那就是使您的程序不易受到sql注入漏洞的影响。请查看以下优秀文档:https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html