【发布时间】:2016-06-23 02:29:15
【问题描述】:
我有一个表格,其中的数据可能在字符之间包含空值。因为我已经将表定义为 VARCHAR,所以它会给我一个错误
org.postgresql.util.PSQLException: 错误: 无效的字节序列 编码“UTF8”:0x00
应该有一种方法可以在 postgres 中插入基于 null 的字符串。 这是插入到 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 上处理得很好。
【问题讨论】:
标签: postgresql jdbc varchar