create a java process to generate a idempiere report - java

I have Postgres that works when on it own, but doesn't work when integrated into java and called on idempiere. I'm looking for suggestion.
I get the ff error:
caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "00"
Position: 1055; State=42601; ErrorCode=0
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:143)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:120)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:384)
at jdk.internal.reflect.GeneratedMethodAccessor48.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.compiere.db.StatementProxy.invoke(StatementProxy.java:130)
at com.sun.proxy.$Proxy11.executeUpdate(Unknown Source)
at org.compiere.util.DB.executeUpdate(DB.java:1039)
at org.compiere.util.DB.executeUpdate(DB.java:898)
at org.compiere.util.DB.executeUpdate(DB.java:885)
at ice.enterprise.base.report.StockAgingReport.createDetailLines(StockAgingReport.java:145)
at ice.enterprise.base.report.StockAgingReport.doIt(StockAgingReport.java:72)
at org.compiere.process.SvrProcess.process(SvrProcess.java:201)
at org.compiere.process.SvrProcess.startProcess(SvrProcess.java:147)
at org.adempiere.util.ProcessUtil.startJavaProcess(ProcessUtil.java:173)
at org.compiere.apps.AbstractProcessCtl.startProcess(AbstractProcessCtl.java:467)
at org.compiere.apps.AbstractProcessCtl.run(AbstractProcessCtl.java:235)
15:21:49.994===========> DataEngine.loadPrintData: null - ERROR: column "levelno" does not exist
Position: 924
SQL=SELECT T_ReportStockAgeing.CurrentCost,T_ReportStockAgeing.Date1,T_ReportStockAgeing_ICE.Description,T_ReportStockAgeing.OnHand,(SELECT NVL(AD_PInstance.Name,'-1') ||'_'|| NVL(CAST (AD_PInstance.AD_PInstance_ID AS Text),'-1') ||'_'|| NVL((SELECT NVL(AD_Process.Name,'-1') ||'_'|| NVL(AD_Process.Value,'-1') FROM AD_Process WHERE AD_PInstance.AD_Process_ID=AD_Process.AD_Process_ID),'-1') FROM AD_PInstance WHERE T_ReportStockAgeing.AD_PInstance_ID=AD_PInstance.AD_PInstance_ID) AS AAD_PInstance_ID,T_ReportStockAgeing.AD_PInstance_ID AS AD_PInstance_ID,T_ReportStockAgeing.ProductCode,T_ReportStockAgeing.Qty1,T_ReportStockAgeing.Qty2,T_ReportStockAgeing.Qty3,T_ReportStockAgeing.Qty4,T_ReportStockAgeing.Value1,T_ReportStockAgeing.Valu2,T_ReportStockAgeing.Value3,T_ReportStockAgeing.Value4,T_ReportStockAgeing_ICE.T_ReportStockAgeing_ICE_UU,LevelNo FROM T_ReportStockAgeing_ICE WHERE T_ReportStockAgeing_ICE.AD_PInstance_ID=2109500 [136]
15:21:50.030===========> AbstractProcessDialog.doRun: org.postgresql.util.PSQLException: ERROR: column "levelno" does not exist
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.logging.Level;
import org.compiere.print.MPrintFormat;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Ini;
public class StockAgingReport extends SvrProcess {
private int p_AD_Org_ID = 0;
private int p_AD_Client_ID = 0;
private int p_C_AcctSchema_ID = 0;
private int p_M_Product_Category_ID = 0;
private Timestamp p_Date = null;
private long m_start = System.currentTimeMillis();
#Override
protected void prepare() {
StringBuffer sb = new StringBuffer ("Record_ID=")
.append(getRecord_ID());
// Parameter
ProcessInfoParameter[] para = getParameter();
for (int i = 0; i < para.length; i++)
{
String name = para[i].getParameterName();
if (para[i].getParameter() == null && para[i].getParameter_To() == null)
;
else if (name.equals("Date"))
{
p_Date = (Timestamp)para[i].getParameter();
}
else if (name.equals("AD_Org_ID"))
p_AD_Org_ID = ((BigDecimal)para[i].getParameter()).intValue();
else if (name.equals("AD_Client_ID"))
p_AD_Client_ID = para[i].getParameterAsInt();
else if (name.equals("C_AcctSchema_ID"))
p_C_AcctSchema_ID = para[i].getParameterAsInt();
else if (name.equals("M_Product_Category_ID"))
p_M_Product_Category_ID = para[i].getParameterAsInt();
else
log.log(Level.SEVERE, "Unknown Parameter: " + name);
}
log.fine(sb.toString());
}
#Override
protected String doIt() throws Exception {
createDetailLines();
int AD_PrintFormat_ID = DB.getSQLValue(get_TrxName(), "Select AD_PrintFormat_ID from AD_PrintFormat Where name = 'Stock_Ageing'");
if (AD_PrintFormat_ID > 0) {
if (Ini.isClient())
getProcessInfo().setTransientObject (MPrintFormat.get (getCtx(), AD_PrintFormat_ID, false));
else
getProcessInfo().setSerializableObject(MPrintFormat.get (getCtx(), AD_PrintFormat_ID, false));
}
if (log.isLoggable(Level.FINE)) log.fine((System.currentTimeMillis() - m_start) + " ms");
return "";
}
private void createDetailLines() {
StringBuffer sb = new StringBuffer ("INSERT INTO T_ReportStockAgeing "
+ "(AD_PInstance_ID, AD_Client_ID, AD_Org_ID, ProductCode, Description, CurrentCost,"
+ " Qty1, Qty2, Qty3, Qty4, OnHand,"
+ " Value1, Valu2, Value3 ,Value4) ");
sb.append("SELECT ").append(getAD_PInstance_ID()).append(", ").append(Env.getAD_Client_ID(getCtx())).append(", ").append(Env.getAD_Org_ID(getCtx())).append(", ")
.append( "ProductCode, Description, Case OnHand WHEN 0 THEN 0 ELSE CurrentCost*OnHand END AS CurrentCost,"
+ "Qty1, Qty2, Qty3, Qty4, OnHand,"
+ "CASE OnHand WHEN 0 THEN 0 ELSE round((CurrentCost) * Qty1,2) END as Value1, "
+ "CASE OnHand WHEN 0 THEN 0 ELSE round((CurrentCost) * Qty2,2) END as Valu2, "
+ "CASE OnHand WHEN 0 THEN 0 ELSE round((CurrentCost) * Qty3,2) END as Value3, "
+ "CASE OnHand WHEN 0 THEN 0 ELSE round((CurrentCost) * Qty4,2) END as Value4 "
+ "FROM ( "
+ "SELECT "
+ "prod.value as ProductCode, "
+ "prod.description as Description, "
+ "M_Product_Category_ID, "
+ "(SELECT COALESCE (max(c.CurrentCostPrice),0) FROM M_Cost c WHERE c.AD_Org_ID =" +p_AD_Org_ID +" AND c.C_AcctSchema_ID= "+p_C_AcctSchema_ID+" AND prod.M_Product_ID = c.M_Product_ID AND prod.ProductType !='A' ) as CurrentCost, "
+ "(SELECT COALESCE (SUM (st.qtyonhand),0)FROM M_Storage st "
+ "WHERE prod.M_Product_ID = st.M_Product_ID "
+ "AND st.DateMaterialPolicy >= ( ("+p_Date+"::date) - interval '3 month' ) "
+ "AND st.DateMaterialPolicy <= ("+p_Date+"::date) "
+ "AND st.AD_Org_ID = "+p_AD_Org_ID+" AND prod.ProductType !='A' "
+ ") as Qty1, "
+ "(SELECT COALESCE (SUM (st.qtyonhand),0)FROM M_Storage st "
+ "WHERE prod.M_Product_ID = st.M_Product_ID "
+ "AND st.DateMaterialPolicy >= ("+p_Date+"::date- interval '6 month' ) "
+ "AND st.DateMaterialPolicy <= ("+p_Date+"::date - interval '3 month') "
+ "AND st.AD_Org_ID = "+p_AD_Org_ID+" AND prod.ProductType !='A') "
+ "as Qty2, "
+ "(SELECT COALESCE (SUM (st.qtyonhand),0)FROM M_Storage st "
+ "WHERE prod.M_Product_ID = st.M_Product_ID "
+ "AND st.DateMaterialPolicy >= ("+p_Date+"::date - interval '12 month' ) "
+ "AND st.DateMaterialPolicy <= ("+p_Date+"::date- interval '6 month') "
+ "AND st.AD_Org_ID = "+p_AD_Org_ID+" AND prod.ProductType !='A' "
+ ") as Qty3, "
+ "(SELECT COALESCE (SUM (st.qtyonhand),0)FROM M_Storage st "
+ "WHERE prod.M_Product_ID = st.M_Product_ID "
+ "AND st.DateMaterialPolicy < ("+p_Date+"::date- interval '12 month') "
+ "AND st.AD_Org_ID = "+p_AD_Org_ID+" AND prod.ProductType !='A' "
+ ") as Qty4, "
+ "(SELECT COALESCE (SUM (st.qtyonhand),0)FROM M_Storage st "
+ "WHERE prod.M_Product_ID = st.M_Product_ID "
+ "AND st.AD_Org_ID = "+p_AD_Org_ID+" AND prod.ProductType !='A' "
+ ") as OnHand "
+ "FROM M_Product prod "
+ "LEFT JOIN AD_Org org ON org.AD_Org_ID = "+p_AD_Org_ID+" "
+ "LEFT JOIN M_Product_Category prodcat ON prodcat.M_Product_Category_ID = "+p_M_Product_Category_ID+" AND prodcat.AD_Client_ID = "+p_AD_Client_ID+" "
+ "WHERE prod.M_Product_Category_ID = "+ p_M_Product_Category_ID+" AND prod.ProductType !='A' "
+ ")temp ");
sb.append(" ) as temp ");
int no = DB.executeUpdate(sb.toString(), get_TrxName());
log.fine("#" + no);
log.finest(sb.toString());
}
}

I tried to integrate your class example for testing in iDempiere, but as GhostCat pointed, it's better if you create a minimal reproducible example, and also very important, please post also the error that is being thrown by the system, in UI and/or in console log.
The class doesn't have some imports, all the variables are not used, and the private method createDetailLines is never called, I assume the error you're mentioning is in that method, but there is no way to know if is not called.
Now, reviewing the SQL, there are two things to notice:
1 - it's not formatted for java, you use "AND c.C_AcctSchema_ID= $P{C_AcctSchema_ID}" and that's not the way as java manage variables, that sounds like jasper report syntax instead of java. For JDBC you must use ? as a replacement for binding variables
2 - iDempiere is multi-database, the way how the system is designed is to write oracle compatible SQL syntax, and there is a translation layer that converts the oracle statement ot postgresql syntax. So, it's better to avoid using postgresql specific syntax like "::date" or interval '3 month' - it could work, but it can also have problems with the convert layer. If you want to use specific postgresql syntax and avoid the convert layer being confused you can surround the postgresql specific syntax with NATIVE_PostgreSQL_KEYWORK - please don't blame me about the error in this constant :-)

Related

convert split to_char statement from Oracle to SQL Server in Java

I have some Oracle SQL statements in my java code which is often split in singular parts which makes in difficult for me to find an equivalent statement in SQL Server 2017. Here is an example:
if (typeId >= 1 && typeId <= 5)
{
sql = "SELECT t.run_id, t.tran_id, t.tran_id sort_id, t.tran_type, t.prod_id, t.type_id, t.value, " +
getExtractStatement() + " (year from t.tran_datetime) y, " +
getExtractStatement() + " (month from t.tran_datetime) mo, " +
getExtractStatement() + " (day from t.tran_datetime) d, " +
" to_number(to_char (t.tran_datetime, 'HH24')) h, " +
" to_number(to_char (t.tran_datetime, 'MI')) mi, " +
" to_number(to_char (t.tran_datetime, 'SS')) s, " +
" to_number(to_char (t.tran_datetime, 'FF')) ms " +
" FROM tran_calc_group t, mai_group_log m " +
" WHERE t.run_id = m.run_id and m.group_id = 1 " +
" AND m.mai_class_id = %d " +
" AND t.result_group_id = m.level_1 " +
" AND t.result_group_id_2 = m.level_2 " +
" AND t.result_group_id_3 = m.level_3 " +
" AND t.result_group_id_4 = m.level_4 " +
" AND t.prod_id = " + query.getProdId() +
" AND t.run_id IN (" + runLogIds + ")" +
" AND t.type_id = " + typeId;
}
If it were only a usual to_char statement with a YYYY-MM-DD argument for example I could just use GETDATE(), 20 in SQL Server but I don't know how to do this for such a split statement
" to_number(to_char (t.tran_datetime, 'HH24')) h, " +
" to_number(to_char (t.tran_datetime, 'MI')) mi, " +
" to_number(to_char (t.tran_datetime, 'SS')) s, " +
" to_number(to_char (t.tran_datetime, 'FF')) ms " +
I have tried to convert it with a tool (SQLines) but it did not work.
You can check the DATEPART function in SQL Server. It allows you to extract particular parts of given date and time value. For example:
SELECT GETUTCDATE()
,DATEPART(HOUR, GETUTCDATE())
,DATEPART(MINUTE, GETUTCDATE())
,DATEPART(SECOND, GETUTCDATE())
,DATEPART(MILLISECOND, GETUTCDATE());
Also, as stated in the documentation, it returns integer, so there is no need for additional converting.

Subquery on java error

hi guys I tried to make query on java for access data from database but when I compile the error is syntax error at or near "v", when I try to compile my query on postgresql it work and the data can show, but when I compile on eclipse it can't
this the query
#Override
protected StringBuffer buildQuery(String sql, Object... objects) {
StringBuffer sbSQL = new StringBuffer("SELECT a.master_user_id, b.name, d.group_name, a.role, "
+ "(SELECT count (v.id) FROM project_member_dtl v "
+ "LEFT JOIN project_timeline p ON v.project_timeline_id = p.id "
+ "LEFT JOIN master_project_dtl j ON p.master_project_dtl_rid = j.rid "
+ "LEFT JOIN master_project aa ON j.master_project_sid = aa.sid "
+ "where ((v.member_plan_date > now()) AND (v.member_real_date IS NULL)) "
+ "AND j.is_freeze = 'n' "
+ "AND p.is_active = 'y' "
+ "AND aa.is_deleted = 'n' "
+ "AND v.master_user_id = a.master_user_id"
+ "AND v.master_role_id = a.master_role_id ) as in_progress, "
+ "(SELECT count (w.id) FROM project_member_dtl w "
+ "LEFT JOIN project_timeline q ON w.project_timeline_id = q.id "
+ "LEFT JOIN master_project_dtl k ON q.master_project_dtl_rid = k.rid "
+ "LEFT JOIN master_project bb ON k.master_project_sid = bb.sid "
+ "where ((w.member_plan_date < now()) "
+ "AND (w.member_real_date IS NULL)) "
+ "AND k.is_freeze = 'n' "
+ "AND q.is_active = 'y' "
+ "AND bb.is_deleted = 'n' "
+ "AND w.master_user_id = a.master_user_id "
+ "AND w.master_role_id = a.master_role_id ) as in_progress_late, "
+ "(SELECT count (x.id) FROM project_member_dtl x "
+ "LEFT JOIN project_timeline r ON x.project_timeline_id = r.id "
+ "LEFT JOIN master_project_dtl l ON r.master_project_dtl_rid = l.rid "
+ "LEFT JOIN master_project cc ON l.master_project_sid = cc.sid "
+ "where (x.member_plan_date IS NULL) "
+ "AND l.is_freeze = 'n' AND r.is_active = 'y' "
+ "AND cc.is_deleted = 'n' "
+ "AND x.master_user_id = a.master_user_id "
+ "AND x.master_role_id = a.master_role_id ) as waiting_list, "
+ "(SELECT count (y.id) FROM project_member_dtl y"
+ "LEFT JOIN project_timeline s ON y.project_timeline_id = s.id "
+ "LEFT JOIN master_project_dtl m ON s.master_project_dtl_rid = m.rid "
+ "LEFT JOIN master_project dd ON m.master_project_sid = dd.sid "
+ "where (y.member_plan_date > y.member_real_date) "
+ "AND m.is_freeze = 'n' AND s.is_active = 'y' "
+ "AND dd.is_deleted = 'n' "
+ "AND y.master_user_id = a.master_user_id "
+ "AND y.master_role_id = a.master_role_id ) as finish_on_time, "
+ "(SELECT count (z.id) FROM project_member_dtl z "
+ "LEFT JOIN project_timeline t ON z.project_timeline_id = t.id"
+ "LEFT JOIN master_project_dtl n ON t.master_project_dtl_rid = n.rid"
+ "LEFT JOIN master_project ee ON n.master_project_sid = ee.sid"
+ "where (z.member_plan_date < z.member_real_date)"
+ "AND n.is_freeze = 'n' AND t.is_active = 'y'"
+ "AND ee.is_deleted = 'n' "
+ "AND z.master_user_id = a.master_user_id "
+ "AND z.master_role_id = a.master_role_id ) as finish_over_time"
+ "FROM project_member_dtl a");
sbSQL.append("LEFT JOIN master_user b ON a.master_user_id = b.id ");
sbSQL.append("LEFT JOIN user_group c ON a.master_user_id = c.master_user_id ");
sbSQL.append("LEFT JOIN master_group d ON c.group_id = d.id ");
sbSQL.append(sql);
return sbSQL;
}
thanks
It's a typo. Make sure you keep a space at the end of each line
+"AND v.master_user_id = a.master_user_id "
In the first query you missed a space
+"AND v.master_user_id = a.master_user_id"
+ "AND v.master_role_id = a.master_role_id ) as in_progress, "
Now the query would be
...AND v.master_user_id = a.master_user_idAND v.master_role_id =....
SQL engine thinks that a.master_user_idAND is a column. So the next v is a problem. That's why the error
syntax error at or near "v"

JDBC query returning zero when using simple arithmetic operations and alias even though the data in table is not zero

I am building a web portal through android and a query i am running through JDBC drivers is returning 0 where data should not be zero.
This is the query:
ResultSet set = statement.executeQuery("select it.itcod, it.itnam, it.packn, it.tradp, " +
"sum(nvl(itd.slbox,0) - nvl(itd.srbox,0) - nvl(itd.brbox,0) - nvl(itd.gsbox,0)) as sbox, " +
"sum(nvl(itd.slbbx,0) - nvl(itd.srbbx,0) - nvl(itd.brbbx,0) - nvl(itd.gsbbx,0)) as sbbx, " +
"SUM(NVL(itd.PRBOX,0) - NVL(itd.RPBOX,0) - NVL(itd.TRBOX,0)) as pbox, " +
"SUM(NVL(itd.PRBBX,0) - NVL(itd.RPBBX,0) - NVL(itd.TRBBX,0)) as pbbx " +
"from items it " +
"LEFT join item_daily itd " +
"on (it.cocod = itd.cocod " +
"and it.itcod = itd.itcod " +
"and ITD.ddate between " + fdate + " and " + tdate + ")" +
"WHERE IT.COCOD = " + COCOD +
"AND IT.DCODE = " + DCODE +
"AND NVL(IT.FREZE,'N')!='Y' " +
"group by it.cocod, it.itcod, it.itnam, it.packn, " +
" it.tradp, it.pkqty, it.dcode, it.freze, " +
" it.ishow, it.sltax, it.dcont, it.mcode, " +
" it.nwcod " +
"order by itnam ");
I have tried using resultsetmetadata but that does not work either.
you should be careful to this line
+ fdate + " and " + tdate
because you should use like this:
" .. to_date('"+fdate+"','***your_date_format') and to_date('"+fdate+"','***your_date_format')"

How to Compare date in Prepared statement

I have Sybase Statement which is mentioned below :
select s.airbill_nbr, s.orig, s.dest,p.swa_acct_nbr, s.prod_type, s.outbnd_date, s.shppr_name, s.consgn_name,s.shipping_chrg, s.misc_chrg, s.decl_val_chrg, s.tax_amt, s.fuel_chrg, s.scrty_chrg, s.tot_chrgs, s.tot_pcs, s.tot_chrgbl_wt, s.tot_actl_wt from shipment s, shipment_outbnd_stn o, payment p
where s.sa_shpmt_nbr = o.sa_shpmt_nbr
and o.sa_shpmt_nbr = p.sa_shpmt_nbr
and o.outbnd_date=convert(date,'01/29/2015',101)
and s.outbnd_empl_id is not null
and p.fop_cd = 'SW'
and p.swa_acct_nbr =40584
and s.void_flag is null
I have to create a prepared statement for the above code in Java and I have created the below One :
String variable1="29/01/2015";
psRetrieveAwbData = cattsDbc.prepareStatement( "select s.airbill_nbr, s.orig, s.dest, " +
"p.swa_acct_nbr, s.prod_type, s.outbnd_date, s.shppr_name, s.consgn_name, " +
"s.shipping_chrg, s.misc_chrg, s.decl_val_chrg, s.tax_amt, s.fuel_chrg, " +
"s.scrty_chrg, s.tot_chrgs, s.tot_pcs, s.tot_chrgbl_wt, s.tot_actl_wt " +
"from shipment s, shipment_outbnd_stn o, payment p " +
"where s.sa_shpmt_nbr = o.sa_shpmt_nbr " +
"and o.sa_shpmt_nbr = p.sa_shpmt_nbr " +
"and o.outbnd_date = '"+ variable1 +"'" +
"and s.outbnd_empl_id is not null " +
"and p.fop_cd = 'SW' " +
"and p.swa_acct_nbr = " + iAccount +
"and s.void_flag is null" );
but I dont know how to put date in date Format because the way I have put it is not working by taking the variable1, as variable1 It is taking as String and not finding out the Solution.Could you please tell me how to create the prepared statement where I can directly put date or put the date by storing it in some variable.
PreparedStatement pstmt = con.prepareStatement("select s.airbill_nbr, s.orig, s.dest, " +
"p.swa_acct_nbr, s.prod_type, s.outbnd_date, s.shppr_name, s.consgn_name, " +
"s.shipping_chrg, s.misc_chrg, s.decl_val_chrg, s.tax_amt, s.fuel_chrg, " +
"s.scrty_chrg, s.tot_chrgs, s.tot_pcs, s.tot_chrgbl_wt, s.tot_actl_wt " +
"from shipment s, shipment_outbnd_stn o, payment p " +
"where s.sa_shpmt_nbr = o.sa_shpmt_nbr " +
"and o.sa_shpmt_nbr = p.sa_shpmt_nbr " +
"and o.outbnd_date = ?" +
"and s.outbnd_empl_id is not null " +
"and p.fop_cd = 'SW' " +
"and p.swa_acct_nbr = " + iAccount +
"and s.void_flag is null" );");
pstmt.setDate (dt);

JDBC Oracle Query. Slow when executing with setInt() instead of setObject()

I have the following fragment of code:
public static void main(String[] args) throws SQLException {
Connection c = getConnection();
long time = System.currentTimeMillis();
PreparedStatement ps = c.prepareStatement(sqlQuery);
int index = 1;
for (String param: parameters) {
if (isInt(param)) {
//ps.setInt(index++, Integer.parseInt(param));
ps.setObject(index++, Integer.parseInt(param), java.sql.Types.NUMERIC , 0);
} else {
ps.setString(index++, param);
}
}
displayResult(ps.executeQuery());
System.out.println("It took " + (System.currentTimeMillis()-time) + ".");
time = System.currentTimeMillis();
Statement s = c.createStatement();
ResultSet rs = s.executeQuery(expandParametersInStatement(sqlQuery, parameters));
displayResult(rs);
System.out.println("It took " + (System.currentTimeMillis()-time) + ".");
}
The query executed with a PreparedStatement is slower by a factor of 4000. Compared to the Statement approach. They give the same result and the order of execution makes no huge difference.
Using the setObject() instead of setInt() makes the PreparedStatement as fast as the Statement.
What is the difference? The cast in the Database cannot be that expensive? The data type in the database is a NUMBER(10). I guess it is a matter of the indeces which are used. However, I cannot replicate this in the SQL Developer with CAST(x AS INTEGER)?
Thanks.
The statement is:
private static String sqlQuery = "SELECT sum(value) " +
"FROM a monat, " +
" n jahr, " +
" kunde kunde " +
"WHERE monat.kunde_nr IN " +
" (SELECT DISTINCT kunde.kunde_nr " +
" FROM MASKE_4_KUNDEN kunde " +
" WHERE kunde.firma_nr = ? " +
" AND kunde.verkaufsbereich_nr = ? " +
" AND kunde.vertriebsbereich_nr BETWEEN (CASE WHEN ? <> -1 THEN ? ELSE -9999999999 END) AND (CASE WHEN ? <> -1 THEN ? ELSE 9999999999 END) " +
" AND kunde.vertreter_nr BETWEEN (CASE WHEN ? <> -1 THEN ? ELSE -9999999999 END) AND (CASE WHEN ? <> -1 THEN ? ELSE 9999999999 END)" +
" AND kunde.konzern_nr BETWEEN (CASE WHEN ? <> -1 THEN ? ELSE -9999999999 END) AND (CASE WHEN ? <> -1 THEN ? ELSE 9999999999 END) " +
" AND kunde.geschaeftsjahr = ? " +
" AND kunde.kunde_nr BETWEEN (CASE WHEN ? <> -1 THEN ? ELSE -9999999999 END) AND (CASE WHEN ? <> -1 THEN ? ELSE 9999999999 END))" +
" AND monat.firma_nr = ? " +
" AND monat.verkaufsbereich_nr = ? " +
" AND monat.jahr_nr = ? " +
" AND jahr.kunde_nr = monat.kunde_nr " +
" AND jahr.firma_nr = monat.firma_nr " +
" AND jahr.jahr_nr = monat.jahr_nr " +
" AND jahr.verkaufsbereich_nr = monat.verkaufsbereich_nr " +
" AND kunde.kunde_nr = monat.kunde_nr " +
" AND kunde.firma_nr = monat.firma_nr";
setInt() method receives int as a second parameter (not Integer). now, I know that auto-boxing should work but apparently it doesn't always work: http://www.coderanch.com/t/550628/JDBC/java/setInt-String-int-Lost
My guess is that it didn't work in older versions of JDBC and it works in newer versions but is still buggy...
I would try using Integer.intValue() and see if it works any better!
If anyone has more input in regards - I'd love to hear it!
We observed the same behavior also with a prepared statement having just setString() statements over an Oracle Database 10.2.0.4.0.
The problem has been completely solved updating the jdbc driver from 10.2.0.3.0 to 10.2.0.4.0

Categories