Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prepared Statements multiple parameters

Status
Not open for further replies.

thebarslider

Programmer
Dec 21, 2001
80
GB
Hello,

I am writing a Java program to purge some data from a database. I already have a list of the id's that I wish to delete.

Is it possible to write a statement like:

delete from table where autoid in ...

Where the three dots would be an array I passed to the statement rather than having to create a single statement for each id as although this is easy to do using a loop it is inefficient.

Hope someone can help.

Many Thanks.
 
You can execute a statement such as :

Code:
        PreparedStatement ps = c.prepareStatement("delete from ben_test where id in (?,?)");
        ps.setInt(1, 23);
        ps.setInt(2, 24);
        ps.execute();
        ps.close();
        c.close();

So what you would do is build up the PreparedStatement string (ie the number of '?') according to the number of id's you want to delete - and then loop on the 'ps.setInt()' methods, incrementing the parameter position as you go.

This would then achieve what you want to do.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
BTW - It would have been nice for you to have posted your solution to the problem posed here by you : thread269-1171413

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top