Solved

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

Posted on 2008-10-20
4
556 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now