Common pitfalls in JDBC Prepared Statement

DO NOT USE single quotes when using setString method.

Usually we tend to forget this when using like statement as in

select projectId from t_project where projectName like ?

// Setting the value
// The following is incorrect and will not work
pstmt.setString(1,"'%abc%'");

// This is correct
// Remember we are using setString method that will automatically add single quotes around it.
// The developer need not add it explicitly.
pstmt.setString(1, "%abc%");

About Vineet Verma

Developer/Blogger/Gamer/Lazy Couch Potato...:P Need PDF Books: Knowledge Base
This entry was posted in JDBC. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s