-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Using direct SQL in hibernate.
PostPosted: Mon Jan 15, 2007 3:21 pm 
Newbie

Joined: Wed Nov 29, 2006 2:49 am
Posts: 5
Hi,
I am trying to use a direct sql query using a prepared statement as follows in hibernate.
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
try {
PreparedStatement preparedStatement = session.connection().prepareStatement("select * from messagerecipients where name = ?");
preparedStatement.setString(1, "\'first recipient\'");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
System.out.println("Got :"+resultSet.getString(1));
}
preparedStatement.close();
}catch(SQLException ex) {
System.out.println(ex.getMessage());
throw new RuntimeException();
}
tx.commit();
session.close();

However I do not get anything in the resultset, which I should get. I tried using a Statement instead of PreparedStatement and that works fine. Could anybody explain to me what is happening here?
Feedback will be greatly appreciated.
Thank you.

_________________
A newbie in Hibernate


Top
 Profile  
 
 Post subject: What database?
PostPosted: Mon Jan 15, 2007 5:16 pm 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
You need to post more info about the database and table definition.

Wild guess:
The name column is CHAR and not VARCHAR. In some databases, this means you have to match the length when comparing strings using prepared statements, but not when using a constant in the query.
This certainly is the case with Oracle.

Try padding your string with spaces to the exact column length.

_________________
Edwin van der Elst
Finalist IT Group


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 9:24 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
More likely, you're assuming that prepared statement IN parameter population is very dumb, when in fact it's only slightly dumb. Try this line instead:
Code:
preparedStatement.setString(1, "first recipient");
java.sql takes care of little things like converting "string" to 'string' for you.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 5:02 am 
Newbie

Joined: Mon Jan 15, 2007 6:27 am
Posts: 7
Actually, it's very important (and smart) that you have to not include the quotes in your bind value. What if your bind value was taken from the user? What if it was "Rock'n'roll"? You'd have to search the value for quotes and escape them.
If you're using regular Statement objects, that's what you have to do. If you're using PreparedStatement objects, there's no need to scan, any value is valid. Any time you use a Statement and concatenate String values from the user in the process you're risking SQL Injection!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 6:21 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Why are you using PreparedStatement instead of session.createSQLQuery()?

This method let's you execute plain SQL...

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 2:04 pm 
Newbie

Joined: Wed Nov 29, 2006 2:49 am
Posts: 5
tenwit wrote:
More likely, you're assuming that prepared statement IN parameter population is very dumb, when in fact it's only slightly dumb. Try this line instead:
Code:
preparedStatement.setString(1, "first recipient");
java.sql takes care of little things like converting "string" to 'string' for you.


Yep, it works without the quotes. Thanks alot tenwit.

_________________
A newbie in Hibernate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.