Postgresql Postgres在字符之间插入空字符串

Postgresql Postgres在字符之间插入空字符串,postgresql,jdbc,varchar,Postgresql,Jdbc,Varchar,我得到了一个表,其中的数据可能包含字符之间的null。因为我已经将表定义为VARCHAR,所以它会抛出一个错误 org.postgresql.util.PSQLException:错误:的字节序列无效 编码“UTF8”:0x00 应该有一种方法可以在postgres中插入一个基于null的字符串。 这是插入postgres时失败的插入示例 private void postGrestest() throws ClassNotFoundException, SQLException { C

我得到了一个表,其中的数据可能包含字符之间的null。因为我已经将表定义为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();
        }
    }
}
private void postGrestest()抛出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=“插入PUBLIC.TEST值(?,)”;
try(Connection=DriverManager.getConnection(
“jdbc:postgresql://url:5432/objectserver?stringtype=unspecified",
“用户名”、“密码”);
语句stmt=connection.createStatement();
PreparedStatement ps=connection.prepareStatement(insertStmt);)
{
执行(dropStmt);
执行(createStmt);
随机r=新随机();
对于(int i=0;i<100;i++)
{
Object str=“Test”+i;
str=((字符串)str).replace('s','\0');
logger.info(“插入”+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中不能在字符串中包含null。从:

代码为零的字符不能在字符串常量中


Java使用了一种略微简化的方案,其中
U+0000
可以编码为
0xC0 0x80
,这是一种双字节编码。您可以替换字符串中的这些值,而不是二进制null。PostgreSQL很乐意接受它。

我理解。是否可以在后端将其替换为任何其他分隔符或不使用的代码点?将\0替换为\uC080没有帮助,因为它代表삀. 它写的是'Te'삀t0'。我希望后端用户在不应在表中表示的位置运行查询。WIN1252在这种情况下有用吗?在utf-8中,U+C080扩展到0xEC 0x82 0x80。不应直接插入该代码点,而应仅插入构成该代码点的两个字节。