How can I enter a variable on a ResultSet Execute Query Where clause?

Posted on 2008-10-20
Last Modified: 2012-05-05
This is the Query I need to put a variable on the Where clause. Variable is itemnamestr that will be entered by the user.
rs = stmt.executeQuery("SELECT itemid, itemname FROM inventorytable WHERE itemname = itemnamestr ");
I got this error when putting itemnamestr as variable on SQL Where clause

Got an exception!
Unknown column 'itemnamestr' in 'where clause'

How can I correctly do it?
import java.sql.*; 
import javax.swing.*;
public class NewClass {
    public static void main (String[] args) { 
        String itemnamestr;
        itemnamestr = JOptionPane.showInputDialog(null, "Enter item name.");
        try { 
            String url = "jdbc:mysql://localhost:3306/test1"; 
            Connection conn = DriverManager.getConnection(url,"root","mypassword"); 
            Statement stmt = conn.createStatement(); 
            ResultSet rs; 
            rs = stmt.executeQuery("SELECT itemid, itemname FROM inventorytable WHERE itemname = itemnamestr "); 
            while ( ) { 
                String itemidvar = rs.getString("itemid"); 
                String itemnamevar = rs.getString("itemname"); 
        } catch (Exception e) { 
            System.err.println("Got an exception! "); 

Open in new window

Question by:glynco
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22764067
you have to pass variable like this.the string to be in single quote.

   rs = stmt.executeQuery("SELECT itemid, itemname FROM inventorytable WHERE itemname = ' "+itemnamestr +" ' " );
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22764085
instead of statement use PreparedStatement;

 PreparedStatement stmt = conn.prepareStatement(("SELECT itemid, itemname FROM inventorytable WHERE itemname = ?");

Author Comment

ID: 22764104
rs = stmt.executeQuery("SELECT itemid, itemname FROM inventorytable WHERE itemname = ' "+itemnamestr +" ' " );

The records do not show-up.
LVL 20

Accepted Solution

chaitu chaitu earned 500 total points
ID: 22764115
may be space was there within the single quote in the string

the string should be 'itemvarable';

 itemname = '"+itemnamestr +"'" ;

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 67
java mysql insert application 14 68
Where to place postgres JDBC driver jar on tomcat 8 70
Java List 4 76
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question