【问题标题】:Time retrieved from database is 00:00:00从数据库检索的时间是 00:00:00
【发布时间】:2019-07-26 16:21:39
【问题描述】:

我的 MySQL 数据库中有一个数据类型为 datetime 的列,但是当我从该列检索数据时,它显示的时间是 00:00:00,而不是我存储的时间。

public class MainWork extends TimerTask {

private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 公共无效运行(){

    try{

        Class.forName("com.mysql.cj.jdbc.Driver");  
        Connection con =DriverManager.getConnection(  
        "jdbc:mysql://localhost:3306/reminder","sarthak","sar31thak");  
        Statement stmt=con.createStatement();

        ResultSet rs= stmt.executeQuery("select * from ToDo");
        while(rs.next()){
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date dt = new Date();
            dt=rs.getDate(2);
            String activity=rs.getString(1);
            String str= sdf.format(dt); 
            Date localDate = new Date();
            String today;
            today=sdf.format(localDate);
            if(str.equals(today)){

                JOptionPane optionPane = new JOptionPane(activity,JOptionPane.WARNING_MESSAGE);
                JDialog dialog = optionPane.createDialog("REMINDER");
                //Toolkit.getDefaultToolkit().beep();
                dialog.setAlwaysOnTop(true); // to show top of all other application
                dialog.setVisible(true); // to visible the dialog

            }
            else
            {
             continue;

            }
        }
    }
    catch (Exception e)
    {System.out.println("Got an ERROR");}
}

public static void main(String args[]){
    Timer t1 = new Timer();
    t1.schedule(new MainWork(), 0,60000);


}

}

【问题讨论】:

  • select * 在这种情况下是相当邪恶的,因为这意味着我们不知道您选择的实际列或顺序。请在您的选择子句中明确写出所有列。这是解决问题的第一步。

标签: java mysql web-applications


【解决方案1】:

无法正常工作,因为您只能获得 Date 并转换为 "yyyy/MM/dd HH:mm:ss"。所以这就是你得到00:00:00的原因。

使用getDate() 只会返回Date

你应该试试,

rs.getTimestamp();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    相关资源
    最近更新 更多