aspose file tools*
The moose likes JDBC and the fly likes How to make SELECT statement case sensitive? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to make SELECT statement case sensitive?" Watch "How to make SELECT statement case sensitive?" New topic
Author

How to make SELECT statement case sensitive?

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi, I have the following code...




when I try to invoke

checkUsernamePassword(request.getParameter("username"), request.getParameter("password");

it runs ok, but I noticed that my query behaves in a non-case sensitive manner. How can I make it case sensitive? Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You don't say which database you are using so this might not be right for you, but you can use an UPPER function to force both the field and the bind values to be upper case, e.g. :

Or you could configure your database to be cased insensetive.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Moving to the JDBC Forum.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746


but this would make my query case insensitive... I don't think that is advisable when querying password fields right? I need it to be case sensitive like...

HELLO is not the same as hello...
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

I agree, the proposed solution is the opposite of what you want. The problem is actually with the database itself. Some databases can be configured to perform case insensitive matching or not, and you need to check the database settings for this. I don't recall whether or not this configured on a table/field level granularity, you'd have to consult your database software.

One alternate solution is to perform the query than perform the case sensitive matching yourself using String.equals() with the returned data although this has some other minor drawbacks such as transmitting the password back to the user (security).


My Blog: Down Home Country Coding with Scott Selikoff
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

I dont think which ever Database it is, you will never get case insensitive results, you will get what you queried for ( that is the minimum contract of a query).

an execute() call returning true does not mean that it has returned some case insensitive results, this means that the query was executed successfully. return instead of what you are doing now, or use executeQuery() and then ResultSet's method to see if there were really any records in the result set. correct me if I am wrong.

I would suggest, use a Query to select count * From ... if it is not too expensive on the applicaiton process.
[ October 25, 2005: Message edited by: RajaniKanth Bhargava ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Timothy Sam:


but this would make my query case insensitive... I don't think that is advisable when querying password fields right? I need it to be case sensitive like...

HELLO is not the same as hello...


Woops! Miss-read your post. Sorry. Scott Selikoff is right - you'll need to configure your database to be case sensitive. If that's not an option, you'll have to check that results in you applcaition after they are returned.
Anil Kumar Saha
Ranch Hand

Joined: Apr 07, 2004
Posts: 111
What will be the solution if the database is DB2?


Regards,

Anil Kumar Saha
SCJP 1.4
http://www.agilej.blogspot.com/
Sabarish Sasidharan
Ranch Hand

Joined: Aug 29, 2002
Posts: 73
Originally posted by Anil Kumar Saha:
What will be the solution if the database is DB2?


If database is DB2 UDB, it should be case sensitive by default. To make it case insensitive we use UPPER() when comparing as outlined in the posts above.


Sab<br /> <br />Perfection does not come from belief or faith. Talk does not count for anything. Parrots can do that. Perfection comes through selfless work.<br />Swami Vivekananda
Sirish Kumar Gongal Reddy
Ranch Hand

Joined: Oct 25, 2004
Posts: 109
Hi,
The solution for your problem is while obtaining the Connection it self you need to select the type of connection i.e it may be a sensitive or insensitive.the code is like this,

DriverManger.registerDriver(Driver name here);
Connection con=DriverManager.getConnection(url here);
Statement stmt=con.createStatement(RESULTSET_TYPE_SENSITIVE OR INSENSITIVE);
ResultSet rs=stmt.executeQuery(your Query here);

Hope that you got my point.If you have any concerns please put a mail then i'll come up with full code.

Thanks & Regards,
G Sirish Reddy.,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make SELECT statement case sensitive?