2017-07-03 22 views
1

私は自分のOracle DBに追加できるようにタイムスタンプを取得しようとしています。 DBにはTIMESTAMP(6)値があります。Javaタイムスタンプdd-MMM-YY hh:mm:ss.SSSSSSSSS for Oracleタイムスタンプ(6)

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

I:私はJavaで今すぐ'12 -sep-12 10時31分19' 秒

の試験日とクエリで値を直接入力することができた、私は以下の持っていますこの形式で何かを得続ける:

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

そして私は、プリペアドステートメントと私のDBクエリに追加しようとしたとき、私は例外を取得:

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

限界値6で定義されたタイムスタンプに小数点以下9桁を入れることはできません。 –

+1

エラーメッセージ 'NULLを挿入することはできません。... ...間違った形式からの出力はほとんどありません。 –

答えて

1

行に列を入力する以外にタイムスタンプ値を必要としない場合は、 System.currentTimeMillis()を呼び出します。代わりに、必要なのはSYSTIMESTAMP疑似列を使用することだけです。しかし、値が必要な場合でも、DMLコマンドのRETURNING句を使用すると、Oracleが使用したものを返すことができます。

+0

それは確かにオプションです。データベースとJavaクライアントが実行するコンピュータの同期がわずかにずれている場合でも、同じ結果は得られません。 –

+0

@Oleしかし、2つのマシンが同じタイムゾーンにない場合でも、dbのタイムスタンプからローカルタイムスタンプに簡単に変換できます(タイムスタンプにタイムゾーンがある限り長くなります)。しかし、どのアーキテクトでも、dbサーバーとアプリケーションサーバーファームを分離するのはお勧めできません。自社のdbサーバーとアプリケーションサーバーファームを含む他の場所で災害復旧サイトと同じデータセンターにいる必要があります。それが可能な限り、私はいつもdbサーバーのタイムスタンプを他のものよりも優先したいと思っています。 – jeff6times7

+0

ありがとう皆さん。あなたがそれを確認できる場合に備えて私のコードを追加しました –

関連する問題