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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7896554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:24:12  来源:igfitidea点击:

How to insert into TIMESTAMP Column Values in oracle using JDBC PreparedStatement

sqloracle

提问by arsenal

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);

//How can I use TIMESTAMP here in this prepareStatement to add timestamp values in database as in Insert statement we can use like this INSERT INTO mytimestamp (id, made_on) VALUES (1, TIMESTAMP '2005-05-13 07:15:31.123456789'); so how can I use this in prepareStatement.

//我如何在这个prepareStatement中使用TIMESTAMP在数据库中添加时间戳值,就像在Insert语句中一样,我们可以像这样使用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 ();

This way I am getting error. Any wrong with this code..

这样我就会出错。此代码有任何错误..

回答by ruakh

Instead of TIMESTAMP '?', write TO_TIMESTAMP(?, 'YYYY-MM-DD HH24:MI:SS.FF').

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