JSP Can I Pass a variable to a SQL Where clause

M

Mike

I am playing around learning JSP, connecting it to my testing
(non-production) MS-SQL database.

In my java code I have a string variable tran_no

Is there a way to pass this off to my SQL code where clause?
ResultSet rs = sel.executeQuery("select * from sample_table where
trans_num=tran_no");

I know that this is not correct code. Just trying to get across what I
want to do.

I can accomplish this is my code, by returning all records in the
table, using an if statement to re-assign the results to new variables
and then only returning these new variables to the screen. This
however seems to be the long way to go about it if I could just pass
the variable as a parameter to the SQL statement in the first place.

Thanks in advance.
 
B

Bryce

I am playing around learning JSP, connecting it to my testing
(non-production) MS-SQL database.

In my java code I have a string variable tran_no

Is there a way to pass this off to my SQL code where clause?
ResultSet rs = sel.executeQuery("select * from sample_table where
trans_num=tran_no");

Several ways to do it.
PreparedStatement statement = new PreparedStatement("select * from
sample_table where trans_num = ?");
statement.setString(1, tran_no);

ResultSet rs = statement.executeQuery();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top