我从Config properti文件中读取了B详细信息。现在我试图通过命令行参数传递和传递日期值来执行查询。我得到的是异常。请让我知道如何通过命令行参数将日期值传递给我的查询。 注意:我创建了包含所有库文件的excutalbel Jar文件。
输出:
java -jar CreateExcelFile1.jar 2014/01/20 2014/01/24
Command line arguments length is 2
Array index 0 value is 2014/01/20
Array index 1 value is 2014/01/24
Connection Successful
java.sql.SQLException: ORA-01841: (full) year must be between -4713 and +9999, and not be 0
代码:
public class CreateExcelFile extends DbConnection{
public static void main(String[] args) {
try{
System.out.println("Command line arguments length is " + args.length);
System.out.println("Array index 0 value is " + args[0]);
System.out.println("Array index 1 value is " + args[1]);
Connection con = openConnection();
PreparedStatement st=con.prepareStatement("Select a.oi_intervention_id ,a.oi_region,a.oi_mrn,b.opi_title, a.oi_rx_nb ,a.oi_store_id,to_char(a.oi_doc_crtd_dt,'dd-MON-yy') as SheetName,to_char(a.oi_doc_crtd_dt,'dd-MON-yy hh.mi.ss,FF AM TZH:TZM') as oi_doc_crtd_dt,to_char(a.oi_doc_expry_dt,'dd-MON-yy hh.mi.ss,FF AM TZH:TZM') as oi_doc_expry_dt from rxesbopcs.OPCS_INTERVENTION a ,rxesbopcs.opcs_program_info b where trunc(a.oi_doc_crtd_dt) BETWEEN TO_DATE ('args[0]', 'yyyy/mm/dd') AND TO_DATE ('args[1]', 'yyyy/mm/dd') and b.opi_program_id = a.oi_program_id order by a.oi_doc_crtd_dt",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=st.executeQuery();
createXls(rs);
System.out.println("Your excel file has been generated!");
closeConnection(con);
} catch ( Exception ex ) {
System.out.println(ex);
}
}
答案 0 :(得分:1)
我认为您需要将占位符放在要插入变量值的查询字符串中,例如而不是
BETWEEN TO_DATE ('args[0]', 'yyyy/mm/dd')
你会把
BETWEEN TO_DATE (?, 'yyyy/mm/dd')
然后在执行查询之前调用st.setString(1, args[0]);
,对其他参数也类似。
您可能想要查看how to use prepared statements。
答案 1 :(得分:0)
这不是PreparedStatements给定参数的方式(sql分布在几行以提高可读性):
PreparedStatement st=con.prepareStatement("Select a.oi_intervention_id
,a.oi_region,a.oi_mrn,b.opi_title, a.oi_rx_nb
,a.oi_store_id,to_char(a.oi_doc_crtd_dt,'dd-MON-yy') as SheetName,
to_char(a.oi_doc_crtd_dt,'dd-MON-yy hh.mi.ss,FF AM TZH:TZM') as oi_doc_crtd_dt,
to_char(a.oi_doc_expry_dt,'dd-MON-yy hh.mi.ss,FF AM TZH:TZM') as oi_doc_expry_dt
from rxesbopcs.OPCS_INTERVENTION a ,rxesbopcs.opcs_program_info b
where trunc(a.oi_doc_crtd_dt) BETWEEN TO_DATE (?, 'yyyy/mm/dd') AND
TO_DATE (?, 'yyyy/mm/dd') and b.opi_program_id = a.oi_program_id
order by a.oi_doc_crtd_dt",
ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
st.setString(1, args[0]);
st.setString(2, args[1]);
ResultSet rs=st.executeQuery();