I got a table where the data may contain null between the characters. As I have already defined the table as VARCHAR, it throws me an error
org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00
There should be a way where I can insert a null based string in postgres. This is the sample insert that has failed while inserting onto 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();
}
}
}
Are there any considerations before dealing with this type of data? This data is a string based one where the source may contain data containing null between them. This is handled well on a different database instance SQL Server using NVARCHAR.