如何使用JDBC PreparedStatement在oracle中插入TIMESTAMP列值

时间:2011-10-25 22:15:51

标签: sql oracle

String s1 = "create table crawler " +
            "(id number NOT NULL PRIMARY KEY, " +
            "url varchar(255) NOT NULL, " +
            "urlHash varchar(255) NOT NULL, " +
            "contentHash varchar(255), " +
            "modDate TIMESTAMP(4), " +
            "contentLocation varchar(100), " +
            "status integer, " +
            "lastCrawlDate TIMESTAMP(4)) ";




            String s2 = "create sequence test_seq start with 1 increment by 1 nomaxvalue";
            //String s3 = "create or replace trigger inserttrigger before insert on test for each row begin select test_seq.nextval into :new.id from dual; end;"; 

            stmt=conn.createStatement();
            stmt.executeUpdate(s1);
            stmt.executeUpdate(s2);
            //stmt.executeUpdate(s3);

//如何在此prepareStatement中使用TIMESTAMP在数据库中添加时间戳值,就像我们可以使用的插入语句一样,INSERT INTO mytimestamp(id,made_on)VALUES(1,TIMESTAMP'2005-05- 13 07:15:31.123456789');那么如何在prepareStatement中使用它呢。

            ps = conn.prepareStatement (
            "INSERT INTO crawler (id, url, urlHash, contentHash, modDate, contentLocation, status, lastCrawlDate) VALUES(test_seq.nextval,?,?,?,TIMESTAMP '?',?,?,TIMESTAMP '?')");

            ps.setString (1, "http://www.google.com");
            ps.setString (2, "swerrsdfsfdgfgrgthtyty");
            ps.setString (3, "1a10407d9a7997531aabe");
            ps.setString (4, "2005-05-13 07:15:31.123456789");
            ps.setString (5, "c://");
            ps.setLong (6, 302);
            ps.setString (7, "2005-05-13 07:15:31.123456789");

            int count = ps.executeUpdate ();

这样我就会收到错误。这段代码有什么问题..

1 个答案:

答案 0 :(得分:2)

而不是TIMESTAMP '?',请写TO_TIMESTAMP(?, 'YYYY-MM-DD HH24:MI:SS.FF')

相关问题