Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Prepared statement

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using PreparedStatement for table retrieval. I would need to fetch the records with column value null. Table Column is VARCHAR2 type.

String query = "SELECT * FROM <table_name> WHERE <col_name> = ?";
PreparedStatement ps = c.prepareStatement(query);
ps.clearParameters();
ps.setNull(1, Types.VARCHAR);

ResultSet rs = ps.executeQuery();
while (rs.next()) {
}

Here, if I set null value, like above, for the where condition, it cannot able to fetch the record even though for this column, table value is null.

If I use the query, SELECT * FROM <table_name> WHERE <col_name> is null, it can fetch the record.


Can anyone please help. thanks
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how SQL works. One NULL value never equals another NULL value. If you want to search for NULL values, use the "is null" idiom as you've done.
 
Raja kamatchi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

Thanks for your immediate reply..

Actually, the input parameter is variable one..It can have some value or can be null. How can i change the query in prepared statement, based on the input param value ?
can you please advise me. thanks.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok lets assume if this code below works fine for you.


now if you have some value in the string then. you are not checking the value and just setting null there. anyways try this.

[ December 21, 2004: Message edited by: Adeel Ansari ]
 
Sheriff
Posts: 67398
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Raja",

We're pleased to have you here with us in the JDBC forum, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
[ December 21, 2004: Message edited by: Bear Bibeault ]
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic