Pass Nullable values using Preparement state - java

I have this query, and I would like to pass the null value. How to do this using Preparement state
The column that must be null is E.RTCA_CD_QMNUM IN (:1)
String sqlSigiop = "SELECT E.RTCA_CD_QMNUM"
+ ", E.RTIT_NR_ITEM"
+ ", D.RTSI_NR_SUBITEM"
+ ", B.PLOP_DT_INICIO"
+ ", B.PLOP_DT_FECHAMENTO"
+ ", D.RTSI_DT_MAIS_CEDO"
+ ", A.SSRT_CD_STATUS_SUBITEM_RT"
+ ", D.RTSI_DT_MAIS_TARDE"
+ ", A.PORT_DT_INCLUSAO"
+ ", C.PLCO_DS_PLANEJ_CONFIG"
+ ", F.ROTA_NM_ROTA"
+ ", H.CLLO_NM_CLUSTER_LOG"
+ ", G.ATEM_DT_INICIO"
+ ", I.TIAM_DS_TIPO_ATEND_MAR "
+ "FROM SIGIOP.PLANEJAMENTO_OPERACIONAL_RT A "
+ "INNER JOIN SIGIOP.PLANEJAMENTO_OPERACIONAL B ON B.PLOP_SQ_PLANEJ_OPER = A.PLOP_SQ_PLANEJ_OPER "
+ "INNER JOIN SIGIOP.PLANEJAMENTO_CONFIG C ON C.PLCO_SQ_PLANEJ_CONFIG = B.PLCO_SQ_PLANEJ_CONFIG "
+ "INNER JOIN SIGIOP.RT_SUBITEM D ON D.RTSI_CD_RTSUBITEM = A.RTSI_CD_RTSUBITEM "
+ "INNER JOIN SIGIOP.RT_ITEM E ON E.RTIT_CD_RTITEM = D.RTIT_CD_RTITEM "
+ "INNER JOIN SIGIOP.ROTA F ON F.ROTA_SQ_ROTA = A.ROTA_SQ_ROTA "
+ "INNER JOIN SIGIOP.ATENDIMENTO_MAR G ON G.ATEM_SQ_ATEND_MAR = A.ATEM_SQ_ATEND_MAR "
+ "INNER JOIN SIGIOP.CLUSTER_LOG H ON H.CLLO_SQ_CLUSTER_LOG = G.CLLO_SQ_CLUSTER_LOG "
+ "INNER JOIN SIGIOP.TIPO_ATENDIMENTO_MAR I ON I.TIAM_SQ_TIPO_ATEND_MAR = G.TIAM_SQ_TIPO_ATEND_MAR "
+ "WHERE (B.PLOP_DT_FECHAMENTO >= TO_DATE('"+dataInicio+"', 'DDMMYYYY') OR B.PLOP_DT_FECHAMENTO IS NULL)"
+ "AND (B.PLOP_DT_FECHAMENTO < TO_DATE('"+dataFim+"', 'DDMMYYYY') OR B.PLOP_DT_FECHAMENTO IS NULL) "
+ "AND A.MORE_SQ_MOTIVO_REPLANEJ IS NULL "
+ "AND B.PLOP_IN_STATUS = 2 "
+ "AND E.RTCA_CD_QMNUM IN (:1) "
+ "ORDER BY E.RTCA_CD_QMNUM, E.RTIT_NR_ITEM, D.RTSI_NR_SUBITEM, B.PLOP_DT_INICIO";
/
Statement stSigiop = cnnSigiop.createStatement();
ResultSet resultSigiop = stSigiop.executeQuery(sqlSigiop.replace(nullable ":1", rts.toString().replace("[", "").replace("]", "")));
while (resultSigiop.next()) {TO DO}

you have to change your query to this :
...
+ "AND (E.RTCA_CD_QMNUM IS NULL OR E.RTCA_CD_QMNUM IN (:1)) "
...

Related

Casting objects from Hibernate native multiple join query

I have a hibernate native sql query joining three tables, and I'm trying to retrive 3 columns from the result
public void doTestQuery() {
try (Session session = HibernateUtilities.getSessionFactory().openSession()) {
transaction = session.beginTransaction();
String sql = "SELECT\r\n"
+ " users.username, \r\n"
+ " user_roles.role_name, \r\n"
+ " address.address\r\n"
+ "FROM\r\n"
+ " address\r\n"
+ " INNER JOIN\r\n"
+ " users\r\n"
+ " ON \r\n"
+ " address.iduser = users.iduser\r\n"
+ " INNER JOIN\r\n"
+ " user_roles\r\n"
+ " ON \r\n"
+ " users.iduser = user_roles.iduser";
NativeQuery query = session.createNativeQuery(sql);
List<Object[]> results = query.list();
for (Object[] arr : results) {
System.out.println(arr[0].toString() +" "+ arr[1].toString() +" "+ arr[2].toString());
}
transaction.commit();
}
If I replace the System.out.println with this code below, it gives me an error. Is there a way to cast objects from this kind of hibernate queries?
Users user = (Users) arr[0];
UserRoles userRole = (UserRoles) arr[1];
Address _address = (Address) arr[2];
System.out.println(user.getUsername() + userRole.getRolename() + _address.getAddress());
Hibernate requires special aliases to be able to fetch the data from a result set. For this purpose, Hibernate supports a special template in native SQL.
String sql = "SELECT "
+ " {u.*},"
+ " {r.*},"
+ " {a.*} "
+ "FROM "
+ " address a "
+ " INNER JOIN "
+ " users u "
+ " ON a.iduser = u.iduser "
+ " INNER JOIN "
+ " user_roles r "
+ " ON u.iduser = r.iduser";
NativeQuery query = session.createNativeQuery(sql);
query.addEntity("u", Users.class);
query.addEntity("r", UserRoles.class);
query.addEntity("a", Address.class);

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

how to find out table name after we combine some record from multiple table?

okay i have source like this
public List<SearchRecord> getResult() {
List<SearchRecord> searchResult = new ArrayList<SearchRecord>();
String query = "SELECT " + A + ", " + B+ ", " + C
+ " FROM " + TABLE_A+ " UNION ALL SELECT " + A+ ", "
+ B+ ", " + C+ " FROM " + TABLE_B + " UNION ALL SELECT " + A+ ", " + B+ ", "
+ C+ " FROM " + TABLE_C + " UNION ALL SELECT "
+ A+ ", " + B+ ", " + C+ " FROM "
+ TABLE_D;
Cursor cursor = mDatabase.rawQuery(query, null);
if (cursor != null) {
cursor.moveToFirst();
do {
SearchRecord sr = new SearchRecord();
sr.setRecordA(cursor.getString(0));
sr.setRecordB(cursor.getString(1));
sr.setRecordC(cursor.getString(2));
searchResult.add(sr);
} while (cursor.moveToNext());
return searchResult;
}
return null;
}
with this code i can get some record from multiple table who have same column name, and store the result in a List object. but how to i find out which table is this record belongs ? is it from TABLE_A, TABLE_B, TABLE_C, or the other
As per SQL, no, you are joining 3 sets of rows and operating them the same way, each record cannot be distinguished by its origin.
Of course, you could do something like
String query = "SELECT 'TABLE_A', " + A + ", " + B+ ", " + C
+ " FROM " + TABLE_A+ " UNION ALL SELECT 'TABLE_B' " + A+ ", "
+ B+ ", " + C+ " FROM " + TABLE_B
+ " UNION ALL SELECT 'TABLE_C', " + A+ ", " + B+ ", "
+ C+ " FROM " + TABLE_C + " UNION ALL SELECT 'TABLE_D', "
+ A+ ", " + B+ ", " + C+ " FROM "
+ TABLE_D;
:-)

Categories