2017-11-17 1 views
-1

쿼리 매개 변수의 값을 기반으로 동적 쿼리를 작성하고 싶습니다. 조건에 따라 검색어 매개 변수의 값이나하지, 문자열 쿼리가 추가 된 것이 있다면 나는, 그 결과이 같은이자바의 쿼리 매개 변수 값을 기반으로하는 동적 쿼리

String strSQL = "select tb_r_orderdata.customerid,\n" + 
          "REPLACE(REPLACE(REPLACE(LEFT(finishtime,10),'-',''),':',''),'T','') as paydate,\n" + 
          "tb_r_orderdata.amount,\n" + 
          "tb_r_orderdata.acquirementid,\n" + 
          "tb_r_orderdata.tracking_ref,\n" + 
          "tb_m_biller_product.billerdesc, \n" + 
          "tb_m_biller_product.productdesc, \n" + 
          "tb_r_orderdata.hpno, \n" + 
          "tb_r_orderdata.email, \n" + 
          "tb_r_orderdata.rc, \n" + 
          "tb_r_orderdata.rcdesc, \n" + 
          "tb_r_orderdata.additionaldata_pay \n" + 
          "from tb_r_orderdata JOIN tb_m_biller_product ON tb_r_orderdata.productid=tb_m_biller_product.productid AND tb_r_orderdata.billerid=tb_m_biller_product.billerid \n" + 
          "where status='SUCCESS';"; 

같은 문자열 쿼리가 있습니다.

는 여기에 내가 시험 그것을 가지고 내 조건

String newstrSQL = strSQL; 
PreparedStatement pst=null; 

if (period != null) { 
    newstrSQL = strSQL + "and REPLACE(REPLACE(REPLACE(LEFT(finishtime,10),'-',''),':',''),'T','')>=?"; 
} 

if (tanggal != null) { 
    newstrSQL = strSQL + "and REPLACE(REPLACE(REPLACE(LEFT(finishtime,10),'-',''),':',''),'T','')<=?"; 
} 

if (billerid != null) { 
    newstrSQL = strSQL + "and tb_m_biller_product.billerid = ?"; 
} 

if (productid != null) { 
    newstrSQL = strSQL + "and tb_m_biller_product.productid = ?"; 
} 

if (custom != null) { 
    newstrSQL = strSQL + "and" + searchby + " = ?"; 
} 

pst = con.prepareStatement(newstrSQL); 
pst.setString(1, period); 
pst.setString(2, tanggal); 
pst.setString(3, billerid); 
pst.setString(4, productid); 
pst.setString(5, custom); 

rs = pst.executeQuery(); 

,하지만 결과는 항상 0과 구문 오류가 없습니다. 고마워, 나쁜 영어에 대해 죄송합니다.

+0

'period'가'null'이라면 여전히'pst.setString (1, period);'을 호출하고 싶습니까? – alfasin

답변

0

세미콜론을 문자열 where status='SUCCESS';에 입력했습니다. 여기에서 검색어가 여기에서 끝나고 나머지는 나중에 시도 할 때 실행되지 않습니다. strSQL + "and REPLACE(REPLACE(REPLACE(LEFT(finishtime,10),'-',''),':',''),'T','')>=?";.

strSql을 세미콜론으로 끝내지 말고 시도하십시오.

+0

나는 당신이하는 말을 시도해 보았지만 여전히 @Tejendra는 얻지 못했습니다. –

+0

그런 다음 결점이있는 경우 인쇄하여 최종 쿼리를 확인하십시오. 여기서 모든 if 조건을 고려하고 있습니다. – Tejendra

+0

나는 그것을 시험해 보았지만 단지'...... 그리고? =?;'. 나는 2 개 이상의 조건이 사실이라면 그것을 원한다. 예를 들어 두 조건이 참이면'....와? =? '와 같은 결과가 나온다. ',', ',' '),'T ',' ')> = ?; @Tejendra (대체 할 수 없습니까? –

관련 문제