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 9085069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:09:20+00:00 2026-06-16T21:09:20+00:00

I’ve had someone point out that using a private method to handle query execution

  • 0

I’ve had someone point out that using a private method to handle query execution for all queries done by a single class increases the risk of SQL injection attacks.

An example of this method might look like this (below). I have omitted some specifics so as not to distract anyone on implementation.

If you want to talk implementation, please feel free to in the comments. The security review did not comment on the contents of the method, but mainly the fact that it should not be its own method.

Note, queryText is generated from a protected static final string containing SQL text for a prepared statement. The ?’s in the prepared statement text are set using PreparedStatement’s setString (or set whatever) method. The variables that are set on the prepared statement come into the caller method as strongly typed as possible.

queryText is then passed to the private method.

    private ResultSet executeQuery(PreparedStatement stmt) throws SQLException {

    // Declare result set variable
    try{
        try{
            // execute statement and store in variable
        }
        catch(SQLException se){
            // log, close connection, do any special processing, rethrow se
        }

    }
    finally{
                    // This finally block is here to ensure the connection closes if
                    // some special processing (not shown) in the other try generates a runtime exception
        // close connection and statement properly
    }
    // return result set
}

The recommended alternative was to basically inline the same code in each method that does a query.

I did not post this to security.stackexchange.com because I believe it qualifies as a specific security programming problem.

I can think of no reason why duplicating this code (from a private method) into many classes would add any protection. Would it?

Thank you

  • 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-16T21:09:22+00:00Added an answer on June 16, 2026 at 9:09 pm

    Having a central (un-duplicated) place for executing queries is a good idea. Both from a code-maintainability and from a security standpoint. Why have code that could have problems multiple times? It only means that you’ll have to maintain it multiple times!

    What seems important to me (and which has changed by an edit of the question) is that it should be as hard as possible to execute hand-built SQL Strings with it.

    You could, for example, replace any String parameters (which you had initially, but since then replace with a PreparedStatement) with a custom enum:

    public enum SQLQuery {
      QUERY1("SELECT foo FROM BAR", 0),
      QUERY2("SELECT foo from BAR where baz = ?"; 1);
    
      private final String sql;
      private final int argumentCount;
    
      private SQLQuery(final String sql, final int argumentCount) {
        this.sql = sql;
        this.argumentCount = argumentCount;
      }
    
      public String getSQL() {
        return sql;
      }
    
      public int getArgumentCount() {
        return argumentCount;
      }
    }
    

    Then you can write your method like this:

    public ResultSet executeQuery(SQLQuery query, Object... arguments) {
      // implementation left as an exercise for the reader
    }
    

    This way you can be pretty sure that you (or anyone else on your team) don’t accidentally passes in a self-build String into your method.

    If necessary this approach could be extended to handle different parameter types but for many cases using setObject() works just fine.

    For increased modularity you could extract an interface from that enum and allow multiple enums to define queries (for example if you have separate modules in your project). But this has the drawback that malicious (or clueless) developers could use dynamic non-enum implementations of SQLQuery to get their manually-built SQL strings into that method.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I am using Paperclip to handle profile photo uploads in my app. They upload
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am using JSon response to parse title,date content and thumbnail images and place
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.