JDBC - 查询执行

时间:2016-01-13 21:06:23

标签: java sql oracle jdbc

有人可以帮助我使用JDBC在java中执行以下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; 

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

1 个答案:

答案 0 :(得分: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();
    }
}