Re: JDBC - escaping quotes?

From: Barry Lind <barry(at)xythos(dot)com>
To: lloyd <subscr001(at)twilight-systems(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: JDBC - escaping quotes?
Date: 2002-05-08 00:54:15
Message-ID: 3CD87737.8010700@xythos.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Lloyd,

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

[blind(at)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(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2002-05-08 01:25:27 Re: Performance issues with compaq server
Previous Message postgres 2002-05-07 23:21:32 Re: Performance issues with compaq server