[pgsql-jp: 28898] JDBC経由でのnull文字の挿入でエラーが発生
KOBAYASHI Kazuhiko
kobayasi @ pscnet.co.jp
2003年 2月 2日 (日) 05:38:54 JST
小林@PSCと申します。
JDBCを使用してテーブルに"\0"が含まれる文字列を挿入したところ
エラーが発生し、その後、そのConnectionオブジェクトに対する操作
(更新、検索、close等)がすべてエラーになるというトラブルが
発生しました。
これは、JDBCドライバのバグでしょうか?
対策としては\0を別の文字に変換してやれば良いのですが、変更箇所が多く
大変です。出来ればSQLExceptionを拾うだけで済ませたいのですが・・・。
あと、質問なのですが、\0以外に問題が発生する文字はありますでしょうか?
環境
サーバー Redhat Linux8.0
PostgreSQL7.3
クライアント Windows2000Server
J2SDK1.4.1
テーブル
CREATE TABLE test (
col1 VARCHAR
);
テストプログラム
public class Test {
public static void main(String args[]) {
try {
source = new Jdbc3PoolingDataSource();
source.setDataSourceName("A Data Source");
source.setServerName("servername");
source.setDatabaseName("databaseName");
source.setUser("user");
source.setPassword("pass");
source.setMaxConnections(10);
Connection con = source.getConnection();
try {
PreparedStatement pstmt =
con.prepareStatement("INSERT INTO TEST(col1) VALUES(?)");
pstmt.setString(1, "\0"); // null文字を挿入
pstmt.executeUpdate(); // エラーになる。
} catch(Exception ex) {
ex.printStackTrace();
}
System.out.println("区切り-----------------");
PreparedStatement pstmt =
con.prepareStatement("INSERT INTO TEST(col1) VALUES(?)");
pstmt.setString(1, "abc");
pstmt.executeUpdate(); // 以降、何をやってもエラーになる。
con.commit();
con.close(); // close()もエラーになる。
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
実行結果
java.sql.SQLException: ERROR: parser: unterminated quoted string at or near
"'" at character 31
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:131)
at
org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connection
.java:505)
at
org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.j
ava:320)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.j
ava:48)
at
org.postgresql.jdbc1.AbstractJdbc1Statement.executeUpdate(AbstractJdbc1State
ment.java:197)
at Test.main(Test.java:678)
区切り-----------------
The backend has broken the connection. Possibly the action you have
attempted has caused it to close.
at org.postgresql.PG_Stream.ReceiveChar(PG_Stream.java:140)
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:76)
at
org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connection
.java:505)
at
org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.j
ava:320)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.j
ava:48)
at
org.postgresql.jdbc1.AbstractJdbc1Statement.executeUpdate(AbstractJdbc1State
ment.java:197)
at Test.main(Test.java:686)
pgsql-jp メーリングリストの案内