Re: Difference between Statement and preparedStatement (for SQL databases) ?

From: David Harper (devnull_at_obliquity.u-net.com)
Date: 10/18/04

  • Next message: Tor Iver Wilhelmsen: "Re: Difference between Statement and preparedStatement (for SQL databases) ?"
    Date: Mon, 18 Oct 2004 05:34:15 GMT
    
    

    There's one advantage of prepared statements that hasn't been mentioned
    yet. They handle quotes more cleanly.

    Consider an update such as

       update mytable set blurb = "some string" where id = 1234

    Suppose that, instead of "some string", you wanted to insert this text:

       He said "You can't do that, O'Brian!"

    A prepared statement makes this very simple:

       PreparedStatement pstmt = conn.prepareStatement("update mytable
         set blurb = ? where id = ?");

       pstmt.setString(1, "He said \"You can't do that, O'Brian!\"");
       pstmt.setInt(2, 1234);

       int rows = pstmt.executeUpdate();

    With a plain statement, the string itself is a quoted string inside the
    query string:

       String query = "update mytable set blurb = \"He said \"You can't do
    that, O'Brian!\"\" where id = 1234";

    You can see that this is almost certainly wrong, and the correct syntax
    will be database-dependent.

    David Harper
    Cambridge, England


  • Next message: Tor Iver Wilhelmsen: "Re: Difference between Statement and preparedStatement (for SQL databases) ?"

    Relevant Pages

    • Re: Access / inserting binary data with PreparedStatement
      ... > for the password column, which is a binary datatype in the Access DB, ... > What is the corresponding PreparedStatemnt code to insert the String as ... You will have to actually convert your string to a binary object. ... any real performance win with prepared statements. ...
      (comp.lang.java.databases)
    • Re: server-side PreparedStatement minimum version of connector-j and server
      ... > updates (because the sql string is even longer). ... server-side prepared statements, but it does in yours, it appears, ... next version of server-side prepared statements that caches execution ... my guess it has to do with all of the numeric data you ...
      (comp.lang.java.databases)
    • Re: Update existing values incrementally w/UPDATE SQL
      ... I think it would be safe to say, use the Dim statement any time you are ... Dim basically tells vba that you want to initiate a new variable. ... As far as quotes go, this was one of the trickiest ... Consider vba's interpretation of a string: ...
      (microsoft.public.access.modulesdaovba)
    • Re: Update existing values incrementally w/UPDATE SQL
      ... and in the book; Access 2007 VBA Programmer's Reference. ... As far as quotes go, this was one of the trickiest ... Consider vba's interpretation of a string: ... when an SQL is processed (I use an SQL example because it is the most ...
      (microsoft.public.access.modulesdaovba)
    • Re: Update existing values incrementally w/UPDATE SQL
      ... pretend that Me.txtString is a control on your form, ... You cant put Me.txtString inside the double quotes, or VBA just reads it as a ... and in the book; Access 2007 VBA Programmer's Reference. ... when an SQL is processed (I use an SQL example because it is the most ...
      (microsoft.public.access.modulesdaovba)