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

  • Home
  • SEARCH
  • 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 99959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:29:16+00:00 2026-05-11T00:29:16+00:00

I have several instances where that a section of legacy sql statements is based

  • 0

I have several instances where that a section of legacy sql statements is based on a dependency. for example.

if (x !=null) {   SQL = 'SELECT z WHERE x > y'; } else {   SQL = 'SELECT z WHERE x <= y'; }   SQL2 = SQL + ' JOIN a ON b'; 

I am creating PreparedStatements out of this legacy code. What is the best-practice here. Should I create a PreparedStatement for the var SQL and nest it inside of SQL2 of should there be multiple PreparedStatements based on SQL2 without nesting, or something totlly different?

The code is much more complex than the example, as the SQL var is reused inside many long and complex SQL queries.

EDIT: Project Design requires using PreparedStatements, I don’t have the choice of using libraries at this moment.

  • 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. 2026-05-11T00:29:17+00:00Added an answer on May 11, 2026 at 12:29 am

    >Should I create a PreparedStatement for the var SQL and nest it inside of SQL2

    No

    >Or should there be multiple PreparedStatements based on SQL2 without nesting

    Yes

    Furthermore: If you could create one string per query that would be better. I don’t really like to mix SQL with code. It makes it harder to debug and to understand, you can’t copy/paste to a SQL tool to test it easily. By separating the SQL from your code you’ll isolate your query from the operation ( the actual fetch ) and it would be easier to maintain. Plus if the code is not yours it will be a lot easier to understand.

    It doesn’t matter it looks like your’re repeating strings, the point would be to simplify the statements as much as possible.

    I would do something like this:

    final class DatabaseQueries {     public final static String SOME_SCENARIO       = 'SELECT z WHERE x > y JOIN A, B ';     public final static String SOME_OTHER_SCENARIO = 'SELECT z WHERE x <= y JOIN A, B';  } 

    And then use it from your class:

     PreparedStatement pstmt = getCon().prepareStatement( getQuery() );    private String getQuery() {       if( x != null ) {            return DatabaseQueries.SOME_SCENARIO;      } else {             return DatabaseQueries.SOME_OTHER_SCENARIO;      }  } 

    While creating the class ‘DatabaseQueries’ you’ll find you’re repeating a lot of strings, I think it would be fine to susbtitute some part with other constants.

    final class DataBaseQueries {      // this one is private     private final static String JOIN_A_B = ' join A, B ';     public final static String SOME_SCENARIO       = 'SELECT z WHERE x > y ' + JOIN_A_B ;     public final static String SOME_OTHER_SCENARIO = 'SELECT z WHERE x <= y ' + JOIN_A_B ;  } 

    The point here is to make things simpler. This is the first step. In a second step you can create a class to create those queries that are really really complex, but probably YAGNI.

    If the queries are too much you can replace it to load them from a ResourceBundle like in this question

    I hope this helps.

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer (Int32)Convert.ToDateTime('2008-12-31T00:00:00.0000000+01:00').DayOfWeek + 1 May 11, 2026 at 1:30 pm
  • added an answer That sounds very peculiar. You're not calling an overriding method… May 11, 2026 at 1:30 pm
  • added an answer If you consult the Java API docs appropriate for the… May 11, 2026 at 1:30 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.