Setting dynamic column value in PreparedStatement

Discussion in 'Java' started by Anythingcan, Oct 25, 2004.

  1. Anythingcan

    Anythingcan Guest

    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.
     
    Anythingcan, Oct 25, 2004
    #1
    1. Advertising

  2. Anythingcan

    Sudsy Guest

    Anythingcan wrote:
    > 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.

    --
    Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.
     
    Sudsy, Oct 25, 2004
    #2
    1. Advertising

  3. In article <>,
    Sudsy <> wrote:

    > Anythingcan wrote:
    > > 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.


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

    --
    John
    ----
    jmatthews at wright dot edu
    www dot wright dot edu/~john.matthews/
     
    John B. Matthews, Oct 25, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manoj S. P.
    Replies:
    2
    Views:
    4,400
    Manoj S. P.
    Jul 2, 2003
  2. Raj
    Replies:
    1
    Views:
    406
    Adam Maass
    Aug 25, 2003
  3. Charleees
    Replies:
    2
    Views:
    8,214
    Eliyahu Goldin
    Jun 20, 2006
  4. Greg Smith
    Replies:
    0
    Views:
    541
    Greg Smith
    Apr 12, 2007
  5. Greg Smith
    Replies:
    0
    Views:
    161
    Greg Smith
    Apr 12, 2007
Loading...

Share This Page