IBM Informix JDBC Driver does not automatically convert between code sets for TEXT, BYTE, CLOB, and BLOB data types.
You can convert between code sets for TEXT and CLOB data types in one of the following ways:
You can automate the following pair of code-set conversions for TEXT and CLOB data types:
To automate code-set conversion for TEXT and CLOB data types, set the IFX_CODESETLOB environment variable in the connection URL. For example: IFX_CODESETLOB = 4096. You can also use the following methods of the IfxDataSource class to set and get the value of IFX_CODESETLOB:
public void setIfxIFX_CODESETLOB(int codesetlobFlag);
public int getIfxIFX_CODESETLOB();
IFX_CODESETLOB can have the values listed in the following table.
Automatic code-set conversion is not enabled.
If the number of allocated bytes is less than the size of the large object, an error is returned.
To perform conversion in memory, you must specify an amount that is smaller than the memory limits of the client machines and larger than the possible size of any converted large object.
When you are using any of the following java.sql.Clob interface methods or Informix extensions to the Clob interface, no codeset conversion is performed, even if the IFX_CODESETLOB environment variable is set. These methods include:
IfxCblob::setAsciiStream(long Clob::setAsciiStream(long position, InputStream fin, int length)
IFX_CODESETLOB takes effect only for methods from the java.sql.PreparedStatement interface.
However when using any of following java.sql.Clob interface methods or Informix extensions to Clob interface, Unicode characters are always converted automatically to the database locale codeset. Here is a list of those methods:
Clob::setCharacterStream(long) throws SQLException Clob::setString(long, String) throws SQLException Clob:: setString(long pos, String str, int offset, int len) IfxCblob::setSubString(long position, String str, int length)
The getBytes(), getString(), InputStreamReader(), and OutputStreamWriter() methods take a code-set parameter that converts to and from Unicode and the specified code set. These methods are covered in detail in Sun's JDK documentation.
Here is sample code that shows how to convert a file from the client code set to Unicode and then from Unicode to the database code set:
File infile = new File("data_jpn.dat"); File outfile = new File ("data_conv.dat");.. .pstmt = conn.prepareStatement("insert into t_text values (?)");.. .// Convert data from client encoding to database encoding System.out.println("Converting data ...\n"); try { String from = "SJIS"; String to = "8859_1"; convert(infile, outfile, from, to); } catch (Exception e) { System.out.println("Failed to convert file"); } System.out.println("Inserting data ...\n"); try { int fileLength = (int) outfile.length(); fin = new FileInputStream(outfile); pstmt.setAsciiStream(1 , fin, fileLength); pstmt.executeUpdate(); } catch (Exception e) { System.out.println("Failed to setAsciiStream"); }.. .public static void convert(File infile, File outfile, String from, String to) throws IOException { InputStream in = new FileInputStream(infile); OutputStream out = new FileOutputStream(outfile); Reader r = new BufferedReader( new InputStreamReader( in, from)); Writer w = new BufferedWriter( new OutputStreamWriter( out, to)); //Copy characters from input to output. The InputStreamReader converts // from the input encoding to Unicode, and the OutputStreamWriter // converts from Unicode to the output encoding. Characters that can // not be represented in the output encoding are output as '?' char[] buffer = new char[4096]; int len; while ((len = r.read(buffer)) != -1) w.write(buffer, 0, len); r.close(); w.flush(); w.close(); }
When you retrieve data from the database, you can use the same approach to convert the data from the database code set to the client code set.
Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]