如何比较java和mysql的数据

如何比较java和mysql的数据,java,mysql,jakarta-ee,jpa,ejb,Java,Mysql,Jakarta Ee,Jpa,Ejb,嗨,我想知道我们如何比较java中的当前日期和mysql中存储的日期 MySQL中存储的日期是2013-01-04 13:15:00 当我在java中通过获取当前日期来比较此日期时 Date date = new Date(); 然后编写一个查询,如果MySQL中存储的日期小于当前日期,则显示结果。但查询返回的结果为0 select model from abc model where model.abcStartDate >= date and model.abcEndDate &am

嗨,我想知道我们如何比较java中的当前日期和mysql中存储的日期

MySQL中存储的日期是
2013-01-04 13:15:00

当我在java中通过获取当前日期来比较此日期时

Date date = new Date();
然后编写一个查询,如果MySQL中存储的日期小于当前日期,则显示结果。但查询返回的结果为0

select model from abc model where model.abcStartDate >= date and model.abcEndDate >= date
在MySQL中,开始日期和结束日期是时间戳数据类型

下面是EJB实体类

@Entity
@Table(name = "abc_table", uniqueConstraints = {})
public class ABCClass implements java.io.Serializable {

private static final long serialVersionUID = -1924961655993587546L;

// Fields
private Date abcStartDate;
private Date abcEndDate;

// Constructors

/** default constructor */
public ABCClass() {
}


@OrderBy(value="3")
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "abc_start_date", unique = false, nullable = false, insertable = true, updatable = true, length = 19)
public Date getabcStartDate() {
    return this.abcStartDate;
}

public void setabcStartDate(Date abcStartDate) {
    this.abcStartDate = abcStartDate;
}

@OrderBy(value="4")
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "abc_end_date", unique = false, nullable = false, insertable = true, updatable = true, length = 19)
public Date getabcEndDate() {
    return this.abcEndDate;
}

public void setabcEndDate(Date abcEndDate) {
    this.abcEndDate = abcEndDate;
}

}

正确的方法之一——在java中使用参数化查询 将准备好的语句与查询一起使用

select model from abc model where model.abcStartDate <= ? and model.abcEndDate >= ?
从abc模型中选择模型,其中model.abcstardate=?

然后在java代码中将第一个和第二个参数设置为new Date()。在这种情况下,您不仅可以比较当前日期,还可以比较任何java日期

您必须将日期与时间进行比较:
对util.Date使用java.sql.Timestamp。它会起作用的。
例如:

java.sql.Timestamp t=新的时间戳(Date.getTime())

如果要使用PreparedStatement比较SQL查询中的日期:

String query="select model from abc model where model.abcStartDate >= ? and model.abcEndDate <=?;";
PreparedStatement stmnt = conn.preparedStatement(query);
stmnt.setTimestamp(1, new java.sql.Timestamp(Calender.getInstance().getTime()));
stmnt.setTimestamp(2, new java.sql.Timestamp(Calender.getInstance().getTime()));
stmnt.executeQuery();

String query=“从abc模型中选择模型,其中model.abcStartDate>=?和model.abcEndDate hi表示类[class java.lang.String]的对象[65464533565]无法转换为[class java.lang.Integer]。可能存在重复的