Thread: JDBC - escaping quotes?

JDBC - escaping quotes?

From
lloyd
Date:
Using a PreparedStatement under pgjdbc2.jar, shouldn't the driver escape
quotes in Strings?

If I try something like this:

  String sql = "insert into book_list (name) values ? ";
  PreparedStatement stmt = cn.prepareStatement(sql);

  String name = "\"Media Unlimited\", by Todd Gitlin";
  stmt.setString(1, name);
  stmt.addBatch();
  stmt.execute();

I would expect a row in the db with a name column of:

   "Media Unlimited", by Todd Gitlin

Instead, though the row is added, the column is blank.




Re: JDBC - escaping quotes?

From
Barry Lind
Date:
Lloyd,

It works for me.  (See my test case below).  I am running on 7.2.1 with
the corresponding jdbd driver.

[blind@barry work]$ java test4
"Media Unlimited", by Todd Gitlin

--Barry




import java.sql.*;

public class test4 {

   public static void main(String[] p_args) {
     try {
       Class.forName("org.postgresql.Driver");
       Connection l_conn;
       l_conn =
DriverManager.getConnection("jdbc:postgresql://localhost:5432/files",
"blind", "");
       l_conn.setAutoCommit(false);

       PreparedStatement stmt1 = l_conn.prepareStatement( "insert into
test values (?)" );
       stmt1.setString(1, "\"Media Unlimited\", by Todd Gitlin");
       stmt1.addBatch();

       stmt1.execute();
       PreparedStatement stmt2 = l_conn.prepareStatement( "select * from
test" );
       ResultSet l_rset = stmt2.executeQuery();
       while (l_rset.next()) {
         System.out.println(l_rset.getString(1));
       }
     } catch (Exception l_se) {
       System.out.println(l_se.toString());
     }

   }

}


lloyd wrote:
> Using a PreparedStatement under pgjdbc2.jar, shouldn't the driver escape
> quotes in Strings?
>
> If I try something like this:
>
>   String sql = "insert into book_list (name) values ? ";
>   PreparedStatement stmt = cn.prepareStatement(sql);
>
>   String name = "\"Media Unlimited\", by Todd Gitlin";
>   stmt.setString(1, name);
>   stmt.addBatch();
>   stmt.execute();
>
> I would expect a row in the db with a name column of:
>
>    "Media Unlimited", by Todd Gitlin
>
> Instead, though the row is added, the column is blank.
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>