2017-07-03 56 views
1

我试图获取时间戳记,以便可以将其添加到Oracle数据库中。数据库有一个TIMESTAMP(6)值。我已经能够直接在值输入用的'12 -sep-12 10点31分19' 秒Java在dd-MMM-YY中需要时间戳记hh:mm:ss.SSSSSSSSS for Oracle时间戳记(6)

现在在Java中的测试日期的查询时,我有以下几点:

Timestamp time_submitted = new Timestamp(System.currentTimeMillis()); 
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-YY hh:mm:ss.SSSSSSSSS"); 

dateFormat.format(time_submitted); 
System.out.println("time_submitted= " + time_submitted); 

我不断收到一些格式如下:

time_submitted= 2017-07-02 22:59:54.733 

,当我尝试将其添加到用一备statment我的数据库查询,我得到异常:

java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("PROJECT1"."ERS_REIMBURSEMENTS"."REBS_SUBMITTED") 

有什么想法?

编辑:

谢谢大家的反馈意见。这里是我的代码的其余部分连同我试图完成:

FrontController.java file 
// rebs_id IS AUTO INCREMENTING 
int user_id = Integer.parseInt(request.getParameter("author_id")); // must cast string to int 
//int man_id 
int rebs_type = Integer.parseInt(request.getParameter("type")); 
int rebs_status = Integer.parseInt(request.getParameter("status")); 
double rebs_amount = Double.parseDouble(request.getParameter("amount")); 
String rebs_description = request.getParameter("description"); 
//Blob rebs_attachments = getBlob(request.getPart("attachments")); 
Timestamp time_submitted = new Timestamp(System.currentTimeMillis()); 
// Timestamp time_resolved 

DateFormat dateFormat = new SimpleDateFormat("dd-MMM-YY hh:mm:ss.SSSSSSSSS"); 
dateFormat.format(time_submitted); 

RebsObj newReb = new RebsObj(user_id, rebs_type, rebs_status, rebs_amount, rebs_description, time_submitted); 
RebsDAO dao4 = new RebsDAOImpl(); 

dao4.createReimbursement(newReb); // sending newEmp object to RebsDAOImpl for creating 

session = request.getSession(); // grabs the session from request 
session.setAttribute("rebs_id", newReb.getRebsId()); 
session.setAttribute("rebs_user_id", newReb.getUserId()); 
session.setAttribute("rebs_type", newReb.getRebsType()); 
session.setAttribute("rebs_status", newReb.getRebsStatus()); 
session.setAttribute("rebs_amount", newReb.getRebsAmount()); 
session.setAttribute("rebs_description", newReb.getRebsDescription()); 
session.setAttribute("time_submitted", newReb.getTimeSubmitted()); 

RebsDAOImpl.java:

public void createReimbursement(RebsObj reb) { 

    // creating PS which will run queries 
    PreparedStatement ps = null; 

    // looks in util/ConnectionUtil.java and saves the url, username and password to "conn" 
    try(Connection conn = ConnectionUtil.getConnection();){ 

     int rebs_id = 0; // REBS_ID IS AUTO INCREMENTING 
     int user_id = reb.getUserId(); 
     //int man_id = reb.getManagerId(); // not needed 
     int rebs_type = reb.getRebsType(); 
     int rebs_status = reb.getRebsStatus(); 
     double rebs_amount = reb.getRebsAmount(); 
     String rebs_description = reb.getRebsDescription(); 
     // Blob rebs_attachments 
     Timestamp time_submitted = reb.getTimeSubmitted();  
     // Timestamp time_resolved 

     System.out.println("TIME STAMP IN DAO: " + time_submitted); 

     // you can put this string 'sql' into multiple lines by adding +, and having everything within "" 
     // this sql line will be ran on SQL 
     String sql = "INSERT INTO ERS_REIMBURSEMENTS(rebs_id, user_id_author, user_id_resolver, " 
       + "rebs_type, rebs_status, rebs_amount, rebs_description, rebs_receipt, " 
       + "rebs_submitted, rebs_resolved) " 
       + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" 
       + "RETURNING rebs_id INTO ?"; 

     // creating prepared statement 
     ps = conn.prepareStatement(sql); // uses connection to send string as a prepared statement 
     ps.setString(1, null); // REBS_ID IS AUTO INCREMENTING 
     ps.setInt(2, user_id); 
     ps.setString(3, null); 
     ps.setInt(4, rebs_type); 
     ps.setInt(5, rebs_status); 
     ps.setDouble(6, rebs_amount); 
     ps.setString(7, rebs_description); 
     ps.setString(8, null); 
     ps.setTimestamp(9, time_submitted); 
     ps.setString(10, null); 
     ps.setInt(11, rebs_id); 

     reb.setRebsId(rebs_id); 
     System.out.println("in DAO, rebs_id: " + rebs_id); 
     // rows affected 
     int affected = ps.executeUpdate(); 
     System.out.println("Rows inserted: " + affected); 

而且奖金问题(这我已经试图解决),我试图“RETURN rebs_id”(因为它正在自动增加),但我似乎无法得到

+5

您不需要格式化java.sql.Timestamp对象以将其传递给Oracle。然而,你并没有展示你是如何尝试这样做的,所以这个问题是脱离主题的,直到你显示代码在哪里填充'PreparedStatement'变量。 –

+0

不能将小数点后9位放入限定为6的时间戳中。 –

+1

错误消息'不能将NULL插入到... ...很难来自不正确的格式。 –

回答

1

如果您不需要timestamp值的任何行填充一列中的一列,那么你不需要调用System.currentTimeMillis()。相反,您只需使用SYSTIMESTAMP伪列。但即使你确实需要这个值,你也可以使用DML命令的RETURNING子句,并且你可以找回Oracle使用的东西。

+0

这当然是一种选择。但是,如果数据库和Java客户端运行的计算机略微不同步,则不会得到相同的结果。 –

+0

@Ole但即使两台机器不在同一时区(只要时间戳有时区),您也可以轻松地从数据库的时间戳转换为本地时间戳。但任何架构师都会阻止将数据库服务器与应用服务器场分离。他们需要与其他地方的灾难恢复站点位于同一个数据中心,其中包括自己的数据库服务器和应用服务器场。尽管如此,我总是希望比其他任何人都更喜欢db服务器的时间戳。 – jeff6times7

+0

谢谢大家。我已经添加了我的代码,以防你可以检查出来 –