Kbase 21420: JDBC Example Code Page Conversion of Special Characters ÅÄÖÜ
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  16/10/2008 |
|
Status: Unverified
GOAL:
JDBC Example Code Page Conversion of Special Characters ÅÄÖÜ
FACT(s) (Environment):
Progress 9.1x
FIX:
The simplest way to avoid loosing special characters on the way to and from the Database is to make the Database code page the same as that from the Java application.
It's possible to get the locale encoding setting with the System property:
file.encoding
like shown with the code example with the line:
System.getProperty("file.encoding"));
If there is no way of changing the database code page or changing the locale of the underlying platform than there still exists the possibility to apply the JVM argument:
-Dfile.encoding
If for example the Database code page is ISO8859-1 than the the JVM argument would be:
-Dfile.encoding=ISO8859-1
If the JDBC example application with Current CON code page: 437 is started with:
java -Dfile.encoding=ISO8859-1 ConsConvChar ÅÄÖÜåäöüß IBM437 IBM437 >> test.log
The screen output is:
UPDATE Test SET ftest = '+-+_sS÷n¯' WHERE fidx = 1
Got back from database: +-+_sS÷n¯
Decoded with: IBM437 got: ÅÄÖÜåäöüß
Notice that between the console screen and the Java application the first conversion automatically took place.
The code below can also be used to get more information on how the code page conversion takes place and the possibilities:
// file ConsConvChar.java
import java.sql.*;
class ConsConvChar {
JDBCTest jtest;
public static String chars = "ÅÄÖÜåäöüß";
public static String enc = "IBM437";
public static String dec = "Cp1252";
public static byte[] b;
public ConsConvChar() {
jtest = new JDBCTest();
jtest.connect();
}
public static void main(String [] args) {
ConsConvChar app = new ConsConvChar();
System.out.println("Usage : java ConsConvChar <String> <encode_to> <decode_to>");
if (args.length > 2) dec = args[2];
if (args.length > 1) enc = args[1];
if (args.length > 0) chars = args[0];
app.setit(chars);
app.getit();
System.exit(0);
} // end main
public void getit(){
String s = jtest.get();
System.out.println("Got back from database: " + s);
String s2 = decode(s);
System.out.println("Decoded with: " + enc + " got: " + s2);
System.out.println("Encoded with: " + dec + " got: " + encode(s2));
}
public void setit(String arg) {
System.out.println("System Encoding = " + System.getProperty("file.encoding"));
jtest.set(arg);
}
public String decode (String sorig) {
String sret = sorig;
try {
b = new byte[sret.length()];
b = sret.getBytes(enc);
sret = new String(b);
} catch (Exception e) {e.printStackTrace();}
return sret;
}
public String encode (String sorig) {
String sret = sorig;
try {
b = new byte[sret.length()];
b = sret.getBytes();
sret = new String(b,dec);
} catch (Exception e) {e.printStackTrace();}
return sret;
} // end encode
} // end ConsConvChar.java
//file JDBCTest.java
import java.sql.*;
class JDBCTest {
Connection con = null;
public JDBCTest () {
super();
}
public void connect (){
try {
Class.forName("com.progress.sql.jdbc.JdbcProgressDriver");
con = DriverManager.getConnection("jdbc:jdbcprogress:T:pcrwe2:7777:sports","pub","pub");
} catch (Exception e) {e.printStackTrace();}
} // end connect
public String get() {
String stemp = "SELECT ftest FROM pub.Test where fidx = 1";
try {
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(stemp);
stemp = null;
while (rs.next()) {
stemp = rs.getString(1);
} // end while
} catch (Exception e) {e.printStackTrace();}
return stemp;
} // end get
public void set(String svalue) {
try {
State.ment statement = con.createStatement();
String stemp = get();
if (stemp != null)
stemp = "UPDATE Test SET ftest = '" + svalue + "' WHERE fidx = 1";
else
stemp = "INSERT INTO Test VALUES('" + svalue + "',1)";
System.out.println(stemp);
statement.executeUpdate(stemp);
} catch (Exception e) {e.printStackTrace();}
} // end set
} // end JDBCTest.java.