Im daling with sql query using java with parameters that maybe exists or not in a query.
For example, there is a http request parameter of :
- name
- start
- limit
in the jsp, i did something like this.
if (request.getParameter("query") != null) {
query = request.getParameter("query");
}
if (request.getParameter("start") != null) {
start = Integer.parseInt(request.getParameter("start"));
}
if (request.getParameter("limit") != null) {
limit = Integer.parseInt(request.getParameter("limit"));
}
....
if (query != null) {
sql += " AND dbo.TABLENAME.namelike '%?%'";
}
if (start != null) {
sql += " AND RowNum >= ?";
}
if (limit != null) {
sql += " AND RowNum <= ?";
if (start == null)
start =0;
}
is there any easy way to do this with PreparedStatement ? or is there any cleaner way to do this. If im not mistaken, one must specify the SQL string first in prepared statement, and not latter.