2016-01-13 149 views
-1

有人可以幫助我在JDBC中使用JDBC執行以下SQL嗎?JDBC - 查詢執行

MERGE INTO ent_emp_policy e 
USING(select e.ENTEMPPOL_ID,e.emp_id,e.entemppol_start_date,e.ENTEMPPOL_END_DATE, nvl(lead(e.entemppol_start_date,1) over (partition by e.emp_id order by e.emp_id,e.entemppol_start_date)-1,e.ENTEMPPOL_END_DATE) as correct_end_dt from ent_emp_policy e ,(select e1.emp_id from ent_emp_policy e1 where e1.ENTEMPPOL_END_DATE='01-jan-3000' group by e1.emp_id having count(*)>1) P where e.ENTEMPPOL_END_DATE='01-jan-3000'and e.emp_id=p.emp_id)ef ON(e.ENTEMPPOL_ID=ef.ENTEMPPOL_ID)WHEN MATCHED THEN UPDATE SET e.ENTEMPPOL_END_DATE = ef.correct_end_dt; 

的SQL是執行以下步驟:

  1. 檢索具有多於1 '01 -Jan-3000' 結束日期所有emp_ids;

  2. 使用Oracle窗口函數檢索下一行開始日期分區爲特定的EMP ID減去從明年開始日1天到正確的結束日期。

  3. 更新ent_emp_policy用合併來糾正特定ENTEMPPOL_ID ENT EMPPOL_END_DATE。

    private void processDupEndDate(long empID, java.util.Date start, DBConnection conn) throws SQLException{   
    PreparedStatement ps = null; 
    ResultSet rs = null; 
    int result = 0; 
    
    try 
    { 
        StringBuffer sb = new StringBuffer(); 
        sb.append("MERGE INTO ent_emp_policy e USING ") 
        .append(("select e.ENTEMPPOL_ID,e.emp_id,e.entemppol_start_date,e.ENTEMPPOL_END_DATE, nvl(lead (e.entemppol_start_date,1) over (partition by e.emp_id order by e.emp_id,e.entemppol_start_date)-1,e.ENTEMPPOL_END_DATE) as correct_end_dt") 
        .append(" from ent_emp_policy e , ") 
        .append((" select e1.emp_id from ent_emp_policy e1 ") 
        .append(" where e1.ENTEMPPOL_END_DATE='01-JAN-3000' group by e1.emp_id having count(*)>1) P ") 
        .append(" where e.ENTEMPPOL_END_DATE='01-JAN-3000' ") 
        .append(" and e.emp_id=p.emp_id") 
        .append(") ef ") 
        .append(" ON(e.ENTEMPPOL_ID=ef.ENTEMPPOL_ID) ") 
        .append(" WHEN MATCHED THEN UPDATE SET ") 
        .append(" e.ENTEMPPOL_END_DATE = ef.correct_end_dt); "); 
    
        ps = conn.prepareStatement(sb.toString()); 
        ps.setTimestamp(1,new java.sql.Timestamp(DateHelper.addDays(start,-1).getTime())); 
        ps.setLong(2,empId); 
        ps.setTimestamp(3,new java.sql.Timestamp(start.getTime()));   
        ps.setTimestamp(4,new java.sql.Timestamp(start.getTime())); 
        ps.setLong(5,empId); 
        ps.setTimestamp(6,new java.sql.Timestamp(DateHelper.addDays(start,-1).getTime()));   
        rs = ps.executeQuery(); 
        conn.commit(); 
    } catch (SQLException e) { 
        conn.rollback(); 
    } 
    finally { 
        if (rs != null) rs.close(); 
        if (ps != null) ps.close(); 
    } 
    
    }  
    
+2

定義 「救救我」。你想寫一個簡單的JDBC程序來運行它嗎?你是否已經在運行它並且遇到了異常? –

+0

我試圖重新格式化您的SQL以便於閱讀/理解,但發現您的括號不匹配。修復語法錯誤。如果這是整個問題,那麼這個問題應該被視爲「由印刷錯誤引起的」。這是一個很好的例子,說明爲什麼你應該格式化SQL來表明分組的條款。 –

+0

謝謝,我正在檢查。如果我發現這是問題,我會關閉它。讓我測試它。我查過了,這不是問題。 – zebango

回答

1

基本步驟在JDBC Getting started guide

描述你需要:

示例代碼(從文檔複製並略有修改):

public static void main (String args []) throws SQLException 
{ 

    String sql = "MERGE INTO ent_emp_policy e " 
     + " USING(select e.ENTEMPPOL_ID,e.emp_id,e.entemppol_start_date,e.ENTEMPPOL_END_DATE," 
     + " nvl(lead(e.entemppol_start_date,1) over (partition by e.emp_id order by e.emp_id,e.entemppol_start_date)-1,e.ENTEMPPOL_END_DATE) as " 
     + " correct_end_dt from ent_emp_policy e ,(select e1.emp_id from ent_emp_policy e1 where e1.ENTEMPPOL_END_DATE='01-jan-3000' group by e1.emp_id having count(*)>1) P " 
     + " where e.ENTEMPPOL_END_DATE='01-jan-3000'and e.emp_id=p.emp_id)ef ON(e.ENTEMPPOL_ID=ef.ENTEMPPOL_ID)WHEN MATCHED THEN UPDATE SET e.ENTEMPPOL_END_DATE = ef.correct_end_dt"; 


    OracleDataSource ods = null; 
    Connection conn = null; 
    Statement stmt = null; 

    // Create DataSource and connect to the local database 
    ods = new OracleDataSource(); 
    ods.setURL("jdbc:oracle:thin:@//myhost:1521/orcl"); 
    ods.setUser("scott"); 
    ods.setPassword("tiger"); 
    conn = ods.getConnection(); 

    try { 
     // Execute the query 
     stmt = conn.createStatement(); 
     stmt.executeUpdate (sql); 

     // commit changes 
     conn.commit(); 
    } finally { 
     if(stmt!=null) stmt.close(); 
     if(conn!=null) conn.close(); 
    } 
}