Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prepared statement null setting

 
Monmohan Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a query

select * from vma where status=?

now when I create a preparedstatement for this query and then set null
like
pstmt.setNull(1,java.sql.Types.VARCHAR);
executing this query returns an empty result set

where as if I directly run a query like
select * from vma where status is null
this is returning many records

Is the preparedstatment behaviour correct? shouldn't it correspond to the same query?
Any help is appreciated
Regards
Monmohan
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you are getting different results is because you are running two different queries. On is:

The other is:

Try running the first query direct against your DB and see the results you get.
 
Monmohan Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually I also thought the same but the first query that you have written is not a valid query at all


plus I think that since there is a seperate interface for setting nulls meant that preparedstatement would automatically
make the corresponding query
as select * from vma where status is null
 
Monmohan Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right,
I understand that
"where status = null" i think is not a valid syntax
 
Monmohan Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks like some problem with this thread..i could see an answer a while before but its gone with my new post
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35266
383
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monmohan,
Users can delete their posts. So it's possible that whoever posted an answer deleted it between your posts.

Anyway, you can put the "is null" syntax in your prepared statement. It wouldn't need a "?" because there is nothing to substitute.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic