Re: Newlines in String inserts with JDBC - Mailing list pgsql-interfaces

From Jens Carlberg
Subject Re: Newlines in String inserts with JDBC
Date
Msg-id 39D30D7F.3259B35@lysator.liu.se
Whole thread Raw
In response to Newlines in String inserts with JDBC  (Ken Kachnowich <khkachn@toad.net>)
List pgsql-interfaces
Ken Kachnowich wrote:
> 
> Is there a method that could be used to insert Strings containing
> newline charaters using the JDBC interface?

I haven't tried this, but allow me to speculate:

1. If new-line isn't allowed as a char, it can't be done at all. Then
you have to invent a mapping on your own. I would recommend "\n"; it's
failrly standard. Note that this means you have to escape "\" too; I
recommend "\\".

2. However, if new-line is allowed (and I suspect it is), it should
probably be 'escaped' somehow. This might be database specific; your
Java-code shouldn't be, though. To solve this, use the PreparedStatement
class, and simply set the String:

String aString = "Row 1: Before newline\nRow 2: After newline";
PreparedStatement aStmt = aConn.prepareStatement();
aStmt.setString(aString);

This way, you delegate to the classes developed for this specific
database to handle all issues specific to the database, as it should be.

> Ken
///Jens Carlberg



pgsql-interfaces by date:

Previous
From: tjm2@mail.ikasths.dk (Troels Jegbjaerg Moerch)
Date:
Subject: JDBC Interface Using Large Objects
Next
From: "Daniel Gayo Avello"
Date:
Subject: SQLException from Postgres JDBC