I have a problem to convert existing code to PrepareStatement as code below. In this case I'm using LIKE clause. My database is Oracle 19c.
public int getCountReprintHistory(String request_date, String statement_type, String option_type, String cust_no) {
String sql = "";
if ( request_date.equals("") && statement_type.equals("") && option_type.equals("") && cust_no.equals("") ) {
sql += "SELECT COUNT(REPRINTLOG_DATE) FROM TBL_REPRINT_HISTORY2 ";
sql += "ORDER BY REPRINTLOG_JOBID DESC ";
} else {
sql += "SELECT COUNT(REPRINTLOG_DATE) FROM TBL_REPRINT_HISTORY2 ";
sql += "WHERE TO_CHAR(REPRINTLOG_DATE,'dd/mm/yyyy') LIKE '%" + request_date +"%' ";
sql += "AND REPRINTLOG_STATEMENT_TYPE LIKE '%" + statement_type +"%' ";
sql += "AND REPRINTLOG_OPTION_TYPE LIKE '%" + option_type +"%' ";
sql += "AND REPRINTLOG_CUSTOMER_NO LIKE '%" + cust_no +"%' ";
sql += "ORDER BY REPRINTLOG_JOBID DESC ";
}
return jdbcTemplate.queryForObject(sql, Integer.class);
}
I tried this query after that it was logout and returns to the main page.
public int getCountReprintHistory(String request_date, String statement_type, String option_type, String cust_no) {
String sql = "";
if ( request_date.equals("") && statement_type.equals("") && option_type.equals("") && cust_no.equals("") ) {
sql += "SELECT COUNT(REPRINTLOG_DATE) FROM TBL_REPRINT_HISTORY2 ";
sql += "ORDER BY REPRINTLOG_JOBID DESC ";
} else {
sql += "SELECT COUNT(REPRINTLOG_DATE) FROM TBL_REPRINT_HISTORY2 ";
sql += "WHERE TO_CHAR(REPRINTLOG_DATE,'dd/mm/yyyy') LIKE '%' || ? || '%' ";
sql += "AND REPRINTLOG_STATEMENT_TYPE LIKE '%' || ? || '%' ";
sql += "AND REPRINTLOG_OPTION_TYPE LIKE '%' || ? || '%' ";
sql += "AND REPRINTLOG_CUSTOMER_NO LIKE '%' || ? || '%' ";
sql += "ORDER BY REPRINTLOG_JOBID DESC ";
}
return jdbcTemplate.queryForObject(sql, new Object[]{request_date,statement_type,option_type,cust_no}, Integer.class);
}