我正在尝试获取时间戳,以便我可以添加到我的Oracle数据库中。 DB具有TIMESTAMP(6)值。我已经能够直接输入一个测试日期为'12 -SEP-12 10:31:19'的查询值
现在在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
当我尝试使用准备好的语句将其添加到我的数据库查询时,我得到例外:
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”(因为它正在自动递增)但我似乎无法得到那个
答案 0 :(得分:1)
如果除了填充行中的列之外不需要时间戳值,则不需要调用System.currentTimeMillis()。相反,您只需要使用SYSTIMESTAMP伪列。但是,即使您确实需要该值,也可以使用DML命令的RETURNING子句,并且可以返回Oracle使用的内容。