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. Advertisements

  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. Advertisements

  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. Advertisements

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. John E.
    Replies:
    3
    Views:
    4,737
    masoud bayan
    Mar 25, 2005
  2. vishnu
    Replies:
    1
    Views:
    1,451
    Patrick.O.Ige
    Mar 25, 2006
  3. Charleees
    Replies:
    2
    Views:
    8,518
    Eliyahu Goldin
    Jun 20, 2006
  4. Greg Smith
    Replies:
    0
    Views:
    706
    Greg Smith
    Apr 12, 2007
  5. johnsonlim026
    Replies:
    0
    Views:
    1,093
    johnsonlim026
    Feb 27, 2010
  6. Esperanza

    Template column base on another column value

    Esperanza, Feb 18, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    5
    Views:
    341
    Elton Wang
    Feb 21, 2005
  7. John E.

    Move bound column to right of dynamic column in datagrid?

    John E., Mar 24, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    470
    Elton Wang
    Mar 28, 2005
  8. Greg Smith
    Replies:
    0
    Views:
    291
    Greg Smith
    Apr 12, 2007
Loading...