0

TIMESTAMP フィールドに正しく挿入できません。

メソッドは次のようになります。

private static void addPickup(Connection conn, boolean debug, String 
        logfileName, String serverName, String mapName, long start, 
        long end) throws SQLException {

    try (CallableStatement statement = conn.prepareCall("{CALL AddPickup "
                 + "(?, ?, ?, ?, ?) }")) {
        statement.setString(1, logfileName);
        statement.setString(2, serverName);
        statement.setString(3, mapName);
        statement.setTimestamp(4, new Timestamp(start));
        statement.setTimestamp(5, new Timestamp(end));
        statement.execute();
    }
}

start の値は 1373573918000 です

end の値は 1373574819000 です

手順:

CREATE PROCEDURE AddPickup(
    IN logfilename VARCHAR(45), 
    IN servername VARCHAR(10), 
    IN mapname VARCHAR(20),
    IN start TIMESTAMP,
    IN end TIMESTAMP
)
AddPickup:BEGIN
IF EXISTS(SELECT p.id FROM pickup p WHERE p.logfile_name = logfilename) THEN
    LEAVE AddPickup;
END IF;

INSERT IGNORE INTO map (name) VALUES (mapname);
INSERT IGNORE INTO server (name) VALUES (servername);
INSERT INTO pickup (logfile_name, server_id, map_id, started, ended, datetime) 
    VALUES (
        logfilename, 
        (SELECT s.id FROM server s WHERE s.name = servername),
        (SELECT m.id FROM map m WHERE m.name = mapname),
        start,
        end,
        NOW()
    );
END //

テーブル:

CREATE TABLE map (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(20) UNIQUE NOT NULL
) ENGINE = 'InnoDB';

CREATE TABLE server (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(10) UNIQUE NOT NULL
) ENGINE = 'InnoDB';

CREATE TABLE pickup (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    logfile_name VARCHAR(45) UNIQUE NOT NULL,
    server_id INT UNSIGNED NOT NULL,
    map_id INT UNSIGNED NOT NULL,
    started TIMESTAMP NOT NULL,
    ended TIMESTAMP NOT NULL,
    datetime DATETIME NOT NULL,
    FOREIGN KEY (map_id) REFERENCES map(id) ON UPDATE CASCADE ON DELETE CASCADE,
    FOREIGN KEY (server_id) REFERENCES server(id) ON UPDATE CASCADE ON DELETE CASCADE
 ) ENGINE = 'InnoDB';

TIMESTAMPフィールドのpickup.startedpickup.endedの両方に常に「0000-00-00 00:00:00」が表示され、その理由がわかりません。

4

2 に答える 2

0

問題はsetTimestampに渡す引数にあると思います

setTimestampを使用するとき、使用する特定の日付がない場合は、常にnew Date().getTime()を引数として渡します。

private static void addPickup(Connection conn, boolean debug, String 
    logfileName, String serverName, String mapName, long start, 
    long end) throws SQLException {

try (CallableStatement statement = conn.prepareCall("{CALL AddPickup "
             + "(?, ?, ?, ?, ?) }")) {
    statement.setString(1, logfileName);
    statement.setString(2, serverName);
    statement.setString(3, mapName);
    statement.setTimestamp(4, new Date().getTime());
    statement.setTimestamp(5, new Date().getTime());
    statement.execute();
}

}

于 2013-07-23T12:47:32.663 に答える