I have PreparedStatement like this:
PreparedStatement st = conn
.prepareStatement("Select * from "
+ "(select Count(*) from uch_otstyp b,prov_uch p "
+ "where b.\"ID_Poezd\"=?"
+ "and (b.\"NachKm\"*1000+b.\"NachM\")>? "
+ "and (b.\"NachKm\"*1000+b.\"NachM\")<=? "
+ "and b.\"ID_Poezd\"=p.\"ID_Poezd\" "
+ "and b.\"ID_Uch\"=p.\"ID_Uch\" "
+ "and p.\"MES\"=? "
+ "and p.\"GOD\"=? "
+ "and p.\"Nput\"=? "
+ "and b.\"Kod_Otstup\"=? "
+ "and b.\"DEPTH\"<1),"
+ ""
+ "(select Count(*) from uch_otstyp b,prov_uch p "
+ "where b.\"ID_Poezd\"=?"
+ "and (b.\"NachKm\"*1000+b.\"NachM\")>? "
+ "and (b.\"NachKm\"*1000+b.\"NachM\")<=? "
+ "and b.\"ID_Poezd\"=p.\"ID_Poezd\" "
+ "and b.\"ID_Uch\"=p.\"ID_Uch\" "
+ "and p.\"MES\"=? "
+ "and p.\"GOD\"=? "
+ "and p.\"Nput\"=? "
+ "and b.\"Kod_Otstup\"=? "
+ "and b.\"DEPTH\">=1)"
+ "and b.\"DEPTH\"<2)");
Parameter values of each subquery are identic. How can I set parameters only for one subquery and automaticaly fill parameters of another(not for each subquery)?
You could use named parameters, so that when a parameter appears multiple times in a query you would only have to specify it once. JDBC doesn't support named parameters, but you can write code to find the parameter names in a SQL string and figure out the order they appear so that the arguments can be added to the PreparedStatement. Spring-Jdbc does this for you with the NamedParameterJdbcTemplate, here's an example. This is in core spring, the package that implements the parameter-munging is org.springframework.jdbc.core.namedparam.
Related
I have a requirement where i need to fetch number of record both the table using JPA native UNION query for Pagination
table 1=>txrh_bcaterms
table 2=> txrm_bcaterms
Ex: txrh_bcaterms UNION txrm_bcaterms
In Below code i am trying to fetching the record in both table Using UNION
#Query(nativeQuery = true,value= "SELECT "
+ " customerid, "
+ "finance_type, "
----------
-------etc----
+ "institution, "
+ "limit_groupid, "
+ "tx_booking_location, "
+ "finance_tenor_days, "
+ "margin_type , "
+ "additional_margin_percentage, "
+ "margin_percentage, "
+ "stale_document_condition_proforma_fin, "
+ "customer_name "
+ "from "
+ "txrh_bcaterms "
+ "where "
+ " institution=:institution "
+ "AND tx_booking_location=:txBookingLocation "
+ "OR finance_type=:financeType "
+ "OR customerid=:customerID "
+ "OR limit_groupid=:limitGroupID "
+ "UNION "
+ "SELECT "
+ "customerid, "
+ "finance_type, "
----------
-------etc----
+ "institution, "
+ "limit_groupid, "
+ "tx_booking_location, "
+" finance_tenor_days, "
+ "margin_type, "
+ "additional_margin_percentage, "
+ "margin_percentage, "
+ "stale_document_condition_proforma_fin, "
+ "customer_name "
+ "from txrm_bcaterms "
"
+ " institution=:institution "
+ "AND tx_booking_location=:txBookingLocation "
+ "OR finance_type=:financeType "
+ "OR customerid=:customerID "
+ "OR limit_groupid=:limitGroupID "
)
Page<Map<String,Object>> historyAndMaster(#Param("institution") String institution, #Param("txBookingLocation") String txBookingLocation,
#Param("financeType") String financeType, #Param("customerID") String customerID, #Param("limitGroupID") String limitGroupID, Pageable page);
NOTE:Actually stepid is the primary key txrh_bcaterms table and stepid is not there txrm_bcaterms table
ERROR
-------
in ui i passed institution =BC/AC/EC then UNION query able to execute and getting data
but if i passed SC then getting below exception
Caused by ERROR: each UNION query must have the same number of columns
Position: 581
at org.axonframework.axonserver.connector.ErrorCode.lambda$static$15(ErrorCode.java:107)
... 9 common frames omitted
=>If there is no same number of columns then how it is worked BC/AC/EC
I am new the Spring Data JPA Could you please help me to resolve this issue
Any other better suggestion welcome!
Hi i have written a join query for my app and its working. My next requirement is to convert that query to prepared statement where I am stuck.
"SELECT * FROM " + TableA.TABLE + " as d LEFT JOIN " + TableB.TABLE + " as c ON d."+ TableA.DAMAGECODE + "=c." + TableB.DAMAGECODE + " AND d."
+ TableA.INDEX + "=c." + TableB.DAMAGECODEINDEX + " AND d."
+ TableA.OBJECTTYPE + "=c." + TableB.OBJECTCLASS + " WHERE d."+ TableA.LEAF + " = '1' AND d." + TableA.OBJECTTYPE + " = ? AND " + "(d."+ TableA.DAMAGECODE + " LIKE ? OR d." + TableA.DAMAGETEXT + " LIKE ?) AND c." + TableB.CONSTRUCTIONSERIES + " = ? ORDER BY " + TableA.DAMAGETEXT;
cursor = db.rawQuery(sql,new String[]{String.valueOf(objectClass),"'%" + query + "%'","'%" + query + "%'",constructionSeries});
When i ran raw query i am getting results ,but when ran the above prepared statements i am getting cursor count always zero
You don't need to add the ' yourself when you pass a String parameters, the PreparedStatement will manage those itself.
"'%" + query + "%'"
For the moment you have condition like
where columnA = "'%somethingToFind%'"
So unless you have a value in columnA like 'somethingToFindInColumnA' (note the quote at the begin and the end of that String). You will never get a result.
Remove those to get something like :
"%" + query + "%"
Full answer :
db.rawQuery(sql,new String[]{String.valueOf(objectClass),"'%" + query + "%'","'%" + query + "%'",constructionSeries});
Become :
db.rawQuery(sql,new String[]{String.valueOf(objectClass),"%" + query + "%","%" + query + "%",constructionSeries});
I'm converting all our sql queries to use Hibernate parameters. Now when I add a parameter in my query and map it with an variable it always give me the error:
ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Error converting data type
nvarchar to bigint.
It doesn't make any sense because I added a string parameter.
The query looks like:
Query query = session.createSQLQuery("SELECT Sum([qtyrequired]) as pcs, o.materialrequireddate, itemid "
+ "FROM [materialManagement].[pcsorderrequirement] r JOIN materialmanagement.pcsorders o "
+ "ON o.id = r.pcsorderid WHERE itemid IN ( :items ) "
+ "AND Cast(o.materialrequireddate AS DATE) >= Cast(Getdate() AS DATE) "
+ "AND o.materialrequireddate <= Cast( :endDate AS DATE) "
+ "GROUP BY o.materialrequireddate, itemid")
.setString("items", items).setString("endDate", endDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
When I return to my original working query it looks like:
Query query = session.createSQLQuery("SELECT Sum([qtyrequired]) as pcs, o.materialrequireddate, itemid "
+ "FROM [materialManagement].[pcsorderrequirement] r " + "JOIN materialmanagement.pcsorders o "
+ "ON o.id = r.pcsorderid " + "WHERE itemid IN (" + items + ") "
+ "AND Cast(o.materialrequireddate AS DATE) >= Cast(Getdate() AS DATE) "
+ "AND o.materialrequireddate <= Cast('" + endDate + "' AS DATE) "
+ "GROUP BY o.materialrequireddate, itemid");
What can create this exception when I use named parameters? I'm using Hibernate 5.1 and for the database MS SQL server.
I changed my query to not cast to a date and sets the parameters with a correct type:
Query query = session.createSQLQuery("SELECT Sum([qtyrequired]) as pcs, o.materialrequireddate, itemid "
+ "FROM [materialManagement].[pcsorderrequirement] r JOIN materialmanagement.pcsorders o "
+ "ON o.id = r.pcsorderid WHERE itemid IN ( :items ) "
+ "AND Cast(o.materialrequireddate AS DATE) >= Cast(Getdate() AS DATE) "
+ "AND o.materialrequireddate <= :endDate "
+ "GROUP BY o.materialrequireddate, itemid").setParameterList("items", items.stream().map(Item::getId).collect(Collectors.toList()))
.setDate("endDate", Date.from(endDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
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')"
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);