Solved

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

Posted on 2008-10-20
4
560 Views
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 ( rs.next() ) { 
                String itemidvar = rs.getString("itemid"); 
                System.out.println(itemidvar); 
                String itemnamevar = rs.getString("itemname"); 
                System.out.println(itemnamevar); 
            } 
            conn.close(); 
        } catch (Exception e) { 
            System.err.println("Got an exception! "); 
            System.err.println(e.getMessage()); 
        } 
    } 
}

Open in new window

0
Comment
Question by:glynco
  • 3
4 Comments
 
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 +" ' " );
       
0
 
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 = ?");
   
          stsmt.setString(1,itemnamestr);
          rs=stsmt.executeQuery();
   
0
 

Author Comment

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

The records do not show-up.
0
 
LVL 20

Accepted Solution

by:
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 +"'" ;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 49
login form jsp example 2 38
ejb mdb examples 1 10
how to debug htl and js pages 8 39
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

821 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