I have a small application that reads from an Oracle 9i database and sends data via email using JavaMail. The database has NLS_CHARACTERSET = "WE8MSWIN1252" that it is, CP1252.
If I run the application without any parameters, it works fine and emails are sent correctly. However, I have a request that forces me to run the application with the -Dfile-encoding=utf8 parameter, as a result of which the text is sent with corrupted characters.
I tried to change the encoding of the data read from the database using
String textToSend = new String(textRead.getBytes("CP1252"), "UTF-8");
But that does not help. I tried all possible combinations with CP1252, windows-1252, ISO-8859-1 and UTF-8 , but still no luck.
Any ideas?
Update to clarify my problem: when I do the following:
Statement stat = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); stat.executeQuery("SELECT blah FROM blahblah ..."); ResultSet rs = stat.getResultSet(); String textRead = rs.getString("whatever");
I get textRead corrupted because the database is CP1252 and the application is running in UTF-8. Another approach that I tried but also failed:
InputStream is = rs.getBinaryStream("whatever"); Writer writer = new StringWriter(); char[] buffer = new char[1024]; Reader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8")); while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } String textRead = writer.toString();
source share