[英]JDBC - Query Execution
Can someone help me executing the following SQL in java using 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;
The SQL is doing the following steps: SQL正在执行以下步骤:
Retrieves all emp_ids having more than 1 '01-Jan-3000' end dates;检索所有具有超过 1 个 '01-Jan-3000' 结束日期的 emp_ids;
Uses oracle window function to retrieve start date for next row in partition for particular emp id subtracts 1 day from next start date to correct end date.使用 oracle 窗口函数为特定 emp id 检索分区中下一行的开始日期,从下一个开始日期减去 1 天到正确的结束日期。
Updates ent_emp_policy using Merge to correct ENT EMPPOL_END_DATE for that particular ENTEMPPOL_ID.使用 Merge 更新 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(); } }
Basic steps are described in the JDBC Getting started guide JDBC 入门指南中描述了基本步骤
You need to:你需要:
executeUpdate
method) executeUpdate
方法)Sample code (copied from the documentation and slightly modified):示例代码(从文档中复制并稍作修改):
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.