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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:50:51+00:00 2026-06-18T02:50:51+00:00

how would i write this sql statement without a hard coded value? resultSet =

  • 0

how would i write this sql statement without a hard coded value?

resultSet = statement
    .executeQuery("select * from myDatabase.myTable where name = 'john'");
// this works

rather have something like:

String name = "john"; 
resultSet = statement
    .executeQuery("select * from myDatabase.myTable where name =" + name);
// Unknown column 'john' in 'where clause' at
// sun.reflect.NativeConstructorAccessorImpl.newInstance0...etc...

thanks in advance..

  • 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-18T02:50:52+00:00Added an answer on June 18, 2026 at 2:50 am

    It is a terrible idea to construct SQL queries the way you currently do, as it opens the door to all sorts of SQL injection attacks. To do this properly, you’ll have to use Prepared Statements instead. This will also resolve all sorts of escaping issues that you’re evidently having at the moment.

    PreparedStatement statement = connection.prepareStatement("select * from myDatabase.myTable where name = ?");    
    statement.setString(1, name);    
    ResultSet resultSet = statement.executeQuery();
    

    Note that prepareStatement() is an expensive call (unless your application server uses statement caching and other similar facilities). Theoretically, it’d be best if you prepare the statement once, and then reuse it multiple times (though not concurrently):

    String[] names = new String[] {"Isaac", "Hello"};
    PreparedStatement statement = connection.prepareStatement("select * from myDatabase.myTable where name = ?");
    
    for (String name: names) {
        statement.setString(1, name);    
        ResultSet resultSet = statement.executeQuery();
        ...
        ...
        statement.clearParameters();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a SQL statement: select id from myTable where id = -1 How do
Is this SQL query statement: SELECT p.id, p.[name], SUM(ps.sales_amount) AS GROSS_SALES FROM products p
I would like to know how to write this inside my hittest if statement:
Trying to find the best way to write this SQL statement. I have a
How do I write a SQL statement that would arrange a column of serial
Hi I would like to write one SQL statement that returns both the count
I would like to transfer this question to active record: The SQL statement I
how would I write a single SQL statement to extract TEMPLATE_NAME (TEMPLATES) and all
All, I'm trying to write a sql statement to be something like this: $sql
Any easier way to write this if statement? if (value==1 || value==2) For example...

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.