0

how to add conditions at run time with prepare statement using jdbc in java

in search when the parameter are too many i want to use condition at runtime

 dim sqlstmt as new StringBuilder
  sqlstmt.add("SELECT * FROM Products")
  sqlstmt.add(" WHERE 1=1") 

  ''// From now on you don't have to worry if you must 
  ''// append AND or WHERE because you know the WHERE is there
  if (ProductCategoryID <> 0){
    sqlstmt.append(" AND ProductCategoryID = ?")
  }
  if (MinimunPrice > 0){
    sqlstmt.AppendFormat(" AND Price >= ?")
  }

how can i using prepare statement in this case , what is you experience? and when my query is like this:

with data as (  SELECT QUESTION_ID, count(*) COUNTT, dense_rank() over (order by count(*) desc) dense_rank "
               FROM EXAM_STD_QUESTIONS esq 
    inner join EXAM e
    where e.teacherId=?
                 group by QUESTION_ID )
    select q.id,q.difficulty_degree ,q.page_number,q.text,q.teacher_guide, q.description,q.picture,q.time,t.fullname,s.name Subject,p.name Part, d.COUNTT,d.QUESTION_ID from data d "
                inner join question q on q.id=d.QUESTION_ID "
                inner join teacher t on t.id= q.teacher_id "
                inner join subject s on s.id = q.subject_id "
                inner join part p on p.id=q.part_id "
                where d.dense_rank=? and q.LESSON_ID=? and q.REFRENCE_ID=? and q.STATUS=1 ORDER BY DBMS_RANDOM.VALUE FETCH NEXT ? ROWS ONLY

in this query we have to where and ... how can i add condition in this case at run time and handle prepare statement?

  • Possible duplicate of [Prepared statement with dynamic where clause](https://stackoverflow.com/questions/15405288/prepared-statement-with-dynamic-where-clause) – ivanjermakov Sep 14 '19 at 06:09
  • i know how add string (condition) to my query but when i use sub query and i have tow where in my query how can i handle it and then i am looking for the ways to handle prepare statement – Kambiz Akbarzadeh Sep 14 '19 at 06:35
  • Please describe the problem you are having in more detail. Currently it is hard to understand exactly what your actual question is. – Mark Rotteveel Sep 14 '19 at 06:47

0 Answers0