简体   繁体   English

SWT小部件日期时间插入到mariadb中

[英]SWT widgets datetime inserting to mariadb

I need help. 我需要帮助。 I want to store a datetime value on my database but it says that the method timestamp is not applicable to the datetime argument , possibly referring to the preparedstatement code ".setTimestamp". 我想在数据库中存储一个datetime值,但是它说方法timestamp不适用于datetime参数 ,可能引用了preparestatement代码“ .setTimestamp”。 I use the SWT Datetime widget to get the start date but I can't seem to put it into the prepared statement. 我使用SWT Datetime小部件获取开始日期,但似乎无法将其放入准备好的语句中。 The datetime values from the SWT widget is passed on the parameter given. 来自SWT小部件的datetime值传递给给定的参数。

Here's the code below: 这是下面的代码:

public void addLeave(String leaveType, DateTime start, DateTime end, int halfDay){
            try{
                Connection con = getConnection();

                String sql = "INSERT into leaves(user_id, leaves_type, leaves_halfDay,leaves_startDate, leaves_endDate) VALUES(?,?,?,?,?)";
                PreparedStatement addLeave = con.prepareStatement(sql);

                addLeave.setString(1, Pkey);
                addLeave.setString(2, leaveType);
                addLeave.setInt(3, halfDay);
                addLeave.setTimestamp(4, start);
                addLeave.setTimestamp(5, end);
                addLeave.execute();

            }catch(Exception e){
                e.printStackTrace();
            }
        }

You'll have to convert the DateTime to Timestamp yourself: 您必须自己将DateTime转换为Timestamp

private Timestamp getTimestamp(DateTime dt)
{
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR,         dt.getYear());
    cal.set(Calendar.MONTH,        dt.getMonth());
    cal.set(Calendar.DAY_OF_MONTH, dt.getDay());
    cal.set(Calendar.HOUR_OF_DAY,  dt.getHours());
    cal.set(Calendar.MINUTE,       dt.getMinutes());
    cal.set(Calendar.SECOND,       dt.getSeconds());

    return new Timestamp(cal.getTimeInMillis());
}

Then call it like this: 然后这样称呼它:

addLeave.setTimestamp(4, getTimestamp(start));

Let MySQL do the conversion as you INSERT : 让MySQL在您INSERT进行转换:

CREATE TABLE `ts_dt_n` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `t2` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  `t3` datetime(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1

INSERT into ts_dt_n (t2, t3) values (FROM_UNIXTIME(1477155329.123),
                                     FROM_UNIXTIME(1477155329.123));

SELECT t2, t3 FROM  ts_dt_n WHERE id = 5\G
*************************** 1. row ***************************
t2: 2016-10-22 09:55:29.123000
t3: 2016-10-22 09:55:29.1

If you are coming from Java's millisecond timer, divide by 1000 as you convert and insert: 如果您来自Java的毫秒计时器,请在转换并插入时除以1000:

INSERT INTO ts_dt_n (t2, t3) values (FROM_UNIXTIME(1477155829739/1000),
                                     FROM_UNIXTIME(1477155829739/1000));

SELECT t2, t3 FROM  ts_dt_n WHERE id = 6\G
*************************** 1. row ***************************
t2: 2016-10-22 10:03:49.739000
t3: 2016-10-22 10:03:49.7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM