I have been breaking my head over the past few hours at trying to figure out what's wrong with my code. This piece of code was all working fine until i received a file which had japanese characters in it. Notepad++ and even some online utility tools say that the encoding of the file is UTF-8. Notepad says its UTF-8-BOM. I have read my data from the file and I have processed it and finally want to write it out to the database.
I get the error org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0xee My database encoding is UTF8 only..
package citynet.dta.pump;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
import citynet.common.ServerException;
public class TestEncoding {
public static void main(String[] args) {
byte[] bytes = null;
try {
//use the below sql to create table 'testtable'
// create table testtable (text1 character varying, text2 character varying,text3 character varying)
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
DataOutputStream out = new DataOutputStream(baos);
out.writeBytes("INR,字仮名交じり文,3255104BTK1");
bytes = baos.toByteArray();
}
Class.forName("org.postgresql.Driver");
Connection c = DriverManager.getConnection("jdbc:postgresql://server:5432/dbname", "username", "password");
if (bytes != null) {
try (ByteArrayInputStream input = new ByteArrayInputStream(bytes)) {
String sql = "COPY testtable FROM stdin delimiter ',' NULL AS 'null' ENCODING 'UTF8' ";
BaseConnection pgcon = (BaseConnection) c;
CopyManager mgr = new CopyManager(pgcon);
try {
mgr.copyIn(sql, input);
} catch (SQLException ex) {
throw new ServerException("Error while copying data in Postgres DB:" + ex);
}
}
}
} catch (Exception e) {
System.out.println("Error:" + e);
}
}
}