我得到一個表,其中數據可能在字符之間包含空值。正如我已經定義的表爲VARCHAR,它會引發我一個錯誤Postgres在字符之間插入字符串爲空
org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00
應該有辦法,我可以插入Postgres的基於空字符串。 這同時插入到Postgres的
private void postGrestest() throws ClassNotFoundException, SQLException
{
Class.forName("org.postgresql.Driver");
String dropStmt = "DROP TABLE PUBLIC.TEST";
String createStmt = "CREATE TABLE PUBLIC.TEST(COL1 VARCHAR(50), COL2 BOOLEAN)";
String insertStmt = "INSERT INTO PUBLIC.TEST VALUES (?, ?)";
try (Connection connection = DriverManager.getConnection(
"jdbc:postgresql://url:5432/objectserver?stringtype=unspecified",
"username", "password");
Statement stmt = connection.createStatement();
PreparedStatement ps = connection.prepareStatement(insertStmt);)
{
stmt.execute(dropStmt);
stmt.execute(createStmt);
Random r = new Random();
for (int i = 0; i < 100; i++)
{
Object str = "Test" + i;
str = ((String) str).replace('s', '\0');
logger.info("Inserting " + str);
// str = ((String) str).replace("\0", "");
ps.setObject(1, str);
Object obj = String.valueOf(r.nextBoolean());
ps.setObject(2, obj);
ps.executeUpdate();
}
}
}
樣品插入已失敗是否有處理這種類型的數據,之前的任何考慮?這個數據是一個基於字符串的數據,其中源可能包含它們之間包含null的數據。這在使用NVARCHAR的不同數據庫實例SQL Server上處理得很好。
我明白。這可以在後端用任何其他分隔符或不使用的代碼點替換嗎? – dmachop
查看更新的答案。 – Patrick
用\ uC080替換\ 0並不能解決問題,因爲它代表了삀。它寫爲'Te삀t0'。我希望後端用戶在不應該在表格中表示的位置運行查詢。在這種情況下WIN1252會有幫助嗎? – dmachop