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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:30:09+00:00 2026-06-12T07:30:09+00:00

I am trying to implement PreparedStatement, which won’t work with sql DB. Suppose I

  • 0

I am trying to implement PreparedStatement, which won’t work with sql DB.

Suppose I have the following sql query:

String selectSqlQuery = "SELECT * FROM customer WHERE f1 = ? AND f2 =? AND f3 > ?";

and the following code:

       //----

        prest = con.prepareStatement(selectSqlQuery );
        prest.setString(1, "val1");
        prest.setString(2, "val2");
        prest.setInt(3, 108);
        ResultSet rs = prest.executeQuery();
        //---

My question is how to implement setString and setInt methods for injecting params?

For now I save parameters’ indexes and values into HashMap, but after it I can’t make injection into sql query string.

  • 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-12T07:30:12+00:00Added an answer on June 12, 2026 at 7:30 am

    Since you’re writing your own driver, you can play with your class a little. Let’s change the approach. If you have a query like this one:

    "SELECT * FROM table WHERE id = ? AND name = ?"
    

    Replace the ? to turn it into

    "SELECT * FROM table WHERE id = {0} AND name = {1}"
    

    About your set methods, those will have to save your new parameters in an Object array, again matching against the index.

    Object parameterArray = new Object[1];
    
    public boolean setString(int paramIndex, String param) {
        if(paramIndex < 0 || paramIndex > parameterArray.length)
            throw new IllegalArgumentException("Can't set parameter " + paramIndex + ", The query only has " + parameterArray.length + " parameters."); 
        parameterArray[paramIndex - 1] = param;
    }
    

    Before executing the query, take advantage of your formatted string and set the parameters:

    MessageFormat messageFormat = new MessageFormat(query);
    String newQuery = messageFormat.format(parameterArray);
    

    The format method will replace the {number} substrings for the corresponding element in the index represented by the number between brackets.

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

Sidebar

Related Questions

Trying to implement the following behavior on an iPad. I have a map-centric application
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
Trying to implement NSCopying for the first time, and I have a question about
I'm trying implement Data Annotation to my Linq to SQL objects. The .dbml file
I have this weird kind of error. I am trying implement basic Euclidean algorithm
Trying to implement what I thought was a simple concept. I have a user
Im trying to implement mapping in grails over hibernate for a table which has
I am trying implement an alphabetical range to perform a query in Solr 3.3.
Trying to implement a shell, mainly piping. I've written this test case which I
trying to implement the following but its not working <script> function loadmap(lat,lng) { //alert(lat,lang);

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.