Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 20th, 2008, 02:26 PM
umesh.awasthi umesh.awasthi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 1 umesh.awasthi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 3 sec
Reputation Power: 0
Question Problem in SELECT QUERY

Hi All,

i am new to the world of data base,facing a small problem. i am developing an application where i have to authenticate the user from the database.

it is a web based from from where i have to pick the user ID nad password, up to this everything is working fine.

i am able to get the connection properly,but facing problem while using the select query,

i have a user whose ID is aum. when i am using the query like this

rSet=stmt.executeQuery("SELECT * FROM user WHERE userName='"+"aum"+"'");

here i am entering the username as static value i.e aum

butif i am trying dynamic values like if i am trying to use variable in placve of aum like this:

rSet=stmt.executeQuery("SELECT * FROM user WHERE userName='uName'");
or rSet=stmt.executeQuery("SELECT * FROM user WHERE userName='"+"uName"+"'");

where uNAme is a string type and is used to store the user ID of the user from the front end which it is storing properly

in this case using variable it is giving that result set is empty while the userID in the varibale is perfecdtly fine..


any pointer in correcting the problem is much appriciated..

--thanks in advance
umesh

Reply With Quote
  #2  
Old March 21st, 2008, 07:17 AM
Annie79 Annie79 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Location: Beaverton OR
Posts: 17 Annie79 User rank is Just a Lowly Private (1 - 20 Reputation Level)  Folding Points: 49426 Folding Title: Beginner FolderFolding Points: 49426 Folding Title: Beginner FolderFolding Points: 49426 Folding Title: Beginner Folder
Time spent in forums: 7 h 25 m 11 sec
Reputation Power: 0
Your code is putting the variable uName within the String; it will be treated as a part of the String and not as a variable. Modify that line as:
java Code:
Original - java Code
  1.  
  2. rSet=stmt.executeQuery("SELECT * FROM user WHERE userName='" +uName +"'");


If you want to get over the jugglery of adding single-quotes around Strings, make use of PreparedStatement - http:// java(dot)sun(dot)com/docs/books/tutorial/jdbc/basics/prepared.html

Last edited by Annie79 : March 21st, 2008 at 07:20 AM. Reason: Added more information

Reply With Quote
  #3  
Old April 8th, 2008, 11:34 PM
ricardoz ricardoz is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: Montevideo, Uruguay
Posts: 9 ricardoz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 34 sec
Reputation Power: 0
1 more cent., you might always think of escaping variables when making a SQL sentence to avoid any SQL injection (most likely your uName variable was entered at some point by a user).

An easy way to do this is with the Apache Commons Lang package StringEscapeUtils class.

Reply With Quote
  #4  
Old April 9th, 2008, 10:35 AM
Annie79 Annie79 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Location: Beaverton OR
Posts: 17 Annie79 User rank is Just a Lowly Private (1 - 20 Reputation Level)  Folding Points: 49426 Folding Title: Beginner FolderFolding Points: 49426 Folding Title: Beginner FolderFolding Points: 49426 Folding Title: Beginner Folder
Time spent in forums: 7 h 25 m 11 sec
Reputation Power: 0
Quote:
Originally Posted by ricardoz
1 more cent., you might always think of escaping variables when making a SQL sentence to avoid any SQL injection (most likely your uName variable was entered at some point by a user).

An easy way to do this is with the Apache Commons Lang package StringEscapeUtils class.


PreparedStatement can solve that problem without requiring a third party package.

Reply With Quote
  #5  
Old April 9th, 2008, 12:02 PM
ricardoz ricardoz is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Location: Montevideo, Uruguay
Posts: 9 ricardoz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by Annie79
PreparedStatement can solve that problem without requiring a third party package.


True, it just applies to escenarios where you want to construct your sentence and use a normal statement.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Problem in SELECT QUERY


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT