This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes sql statement problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "sql statement problem" Watch "sql statement problem" New topic

sql statement problem

Rekha Pande
Ranch Hand

Joined: Jan 29, 2004
Posts: 145
hi all...

when we have to pass a string value to a sql stmt .. we usually write
(select * from atable where username = '"+username+"').. but supposing we have an int variable
int userid = 4;
int serialno=2;

how will i use this in the sql statement... if i use the below stmt it gives me an error...???
(select * from atable where userid = '"+userid+"' and serailno = '"+serialno+"')..

plz tell me how i can use numeric values in sql statements......plz

Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Rekha,

The reason you're getting an error is because the single quotes are used to delimit a string/varchar in SQL. Remove them and it'll work fine. A better solution, however, is to use a PreparedStatement instead of a plain old Statement. Check it out in the Javadoc, a book or one of Sun's JDBC tutorials.

Your other question about paging has been discussed recently on this site. I can't remember exactly where (JDBC?, HTML/JavaScript?, possibly this forum) but you might try searching a few forums.

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32818

The other thread Jules was alluding to.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63540

Moving to the JDBC forum.

[Asking smart questions] [About Bear] [Books by Bear]
Don't get me started about those stupid light bulbs.
subject: sql statement problem
It's not a secret anymore!