Setting dynamic column value in PreparedStatement

A

Anythingcan

Hi gurus,

The following two sql statements delete all entries in a database.

1) DELETE FROM table_name;
2) DELETE FROM table_name WHERE column = column;

However, I would like my java code to only make use of option 2 so
that I can use it to delete all entries AND specific entry using a
PreparedStament, as follows:

con.prepareStatement("DELETE FROM table_name WHERE column = ?");

If I want to delete 'a', the code would be:

pstmt.setSring(1, "a");
pstmt.execute();

But what is the code to delete all the entries.

Is it the setRef(..) method? If yes, how? If not, how?

Thanks.
 
S

Sudsy

Anythingcan said:
Hi gurus,

The following two sql statements delete all entries in a database.

1) DELETE FROM table_name;
2) DELETE FROM table_name WHERE column = column;

However, I would like my java code to only make use of option 2 so
that I can use it to delete all entries AND specific entry using a
PreparedStament, as follows:

con.prepareStatement("DELETE FROM table_name WHERE column = ?");

If I want to delete 'a', the code would be:

pstmt.setSring(1, "a");
pstmt.execute();

But what is the code to delete all the entries.

This isn't a Java question; it's pure SQL. And while there are
wildcards in standard SQL, it's not going to help you in this
case. Your equivalency test will fail with wildcards. You'd have
to use the LIKE qualifier, which might have other unintended
consequences if you incorporate that into the prepare statement.

Better to refer to a good SQL reference tome.
 
J

John B. Matthews

Sudsy said:
This isn't a Java question; it's pure SQL. And while there are
wildcards in standard SQL, it's not going to help you in this
case. Your equivalency test will fail with wildcards. You'd have
to use the LIKE qualifier, which might have other unintended
consequences if you incorporate that into the prepare statement.

Better to refer to a good SQL reference tome.

Indeed. As an alternative, look at the "TRUNCATE table_name" command.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top