当我从jdbc检索数据时,我无法获得任何结果。在数据库中,我正在存储服务器的时间戳。如何检索最近5分钟的数据以下是我的代码:
public static void main(String[] args)
{
String url = "jdbc:mysql://localhost:3306";
String user = "root";
String password = "demo@123";
String qry = "SELECT * FROM test.database WHERE EndTime >= date_sub(now(), interval 5 minute)";
Connection conn = null;
try
{
conn = DriverManager.getConnection(url, user, password);
ResultSet rs = conn.prepareStatement(qry).executeQuery();
while(rs.next())
{
System.out.println(rs.getString("EventName"));
System.out.println(rs.getTimestamp("EndTime"));
System.out.println("-------------------");
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:0)
SELECT *,from_unixtime(EndTime / 1000, '%Y-%m-%d %H:%i') FROM test.database WHERE EndTime >= unix_timestamp(CURRENT_TIMESTAMP - INTERVAL 5 MINUTE) * 1000
您需要更改查询。