Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8738071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:39:59+00:00 2026-06-13T10:39:59+00:00

I am creating a small web based tool that allows users to query a

  • 0

I am creating a small web based tool that allows users to query a large lookup table in various ways. Since its a small tool, I’m using JSP/Servlets.

The lookup table has a definition something like this:

column1 | column2 | column3 | date | user | count

The user can query by column values or a range. Also, the results can be sorted by a specific column. Because the table has several hundred thousand of records, and growing rapidly, I’m using Oracle’s ROWNUM and only returning a small subset of results.

I have a form page that gets the search criteria from the user. I generate my query based on a series of conditions, for example:

query = "SELECT * FROM mytable WHERE 1=1 "
if(searchCriteria1 != "")
    query += "AND column1='searchCriteria1' "
if(searchCriteria2 != "")
    query += "AND column2='searchCriteria2' "
if(searchCriteria2 != "")
    query += "AND column2='searchCriteria2' "
if(searchCriteria3 != "")
    query += "AND column3='searchCriteria3' "
if((searchCriteria4 != "") && (searchCriteria5 != ""))
    query += "AND date>='searchCriteria4' AND date<='searchCriteria5' "
etc...

(This is just simplified pseudo code)

The sorting is handled after the first results page is displayed. The user clicks on the column header of that page to sort by that column. This would be doing a post back and querying the database. Basically, I run the same code above but with this at the end:

if(sortColumn1)
    query += "ORDER BY column1"
if(sortColumn2)
    query += "ORDER BY column2"
if(sortColumn3)
    query += "ORDER BY column3"

So, as you might imagine, my query building code is very long with all these different conditions. Any suggestions on a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:40:00+00:00Added an answer on June 13, 2026 at 10:40 am

    String concatenation is not the best choice for SQL. The best you can do is to use QueryDSL or JOOQ and do it in the OO way. I am more familiar with QueryDSL. Look at the examples here.

    SQLQuery query = new SQLQueryImpl(connection, dialect); 
    query.from(myTable);
    
    BooleanBuilder wheres = new BooleanBuilder();
    
    if(notBlank(searchCriteria1))
       wheres.and(myTable.column1.eq(searchCriteria1));
    if(notBlank(searchCriteria2))
       wheres.and(myTable.column2.eq(searchCriteria2));
    if(notBlank(searchCriteria4) && notBlank(searchCriteria5))
       wheres.andAllOf(myTable.date.goe(searchCriteria4),  myTable.date.loe(searchCriteria5));  //you may want to use myTable.date.between(...)
    
    if (...) {
       query.orderBy(myTable.column1.asc());
    } else if (...){
       query.orderBy(myTable.column2.asc());
    }
    
    query.limit(100); //it is good to limit a result
    query.list(myTable.all());
    

    As for SQL injection query engine will wrap your arguments in named parameters while constructing SQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a small, web based, mail client in PHP and noticed that a
I'm building a small web app that allows users to list their goals. I
I'm creating a small web site that will mostly be used with modern smart
I'm creating pages for a small web-based game which must have some background music
I'm creating a small application on an embedded device that has a boa web
I'm creating a simple web-based lookup utility for a board game called Rail Baron.
I'm creating a small application that I will distribute via NuGet. My web app
Hello i am creating a small webpage that give our users an interface to
I'm creating a small app in ASP.NET MVC that generates ics (iCal) files based
I am creating a small commercial web project running Apache/WSGI/Django/MySQL. I have a development

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.