Hello,

I discovered a problem with improper transport of ASCII strings through the
JDBC driver.

I use SAPDB in ASCII mode on a machine with default encoding Cp1250 and want
to store ISO8859-2 strings transparently (as ASCII) in SAPDB.

The problem occures when I want to store an ASCII string (containing bytes
over 127) using a prepared statement. Unfortunately the driver converts the
string to another encoding (machine default, in my case "Cp1250") before
storing it in the DB loosing most of the characters in this way. The same
task using normal statements works fine.

I looked at the code (version 7.3.0.23a) and found following lines where
default encoding is used instead of the database encoding (ASCII):

com\sap\dbtech\jdbc\translators\BytesTranslator.java(83):
        result = new String (rawResult);
----------------------------------------
com\sap\dbtech\jdbc\translators\StringTranslator.java(225):
        return this.transStringForInput (new String (val));
----------------------------------------
com\sap\dbtech\jdbc\translators\GetvalLob.java(90):
        return new String (bytes);
----------------------------------------
com\sap\dbtech\jdbc\translators\ReaderInputStream.java(131):
        this.byteBuf = converter.getBytes ();
----------------------------------------
com\sap\dbtech\jdbc\translators\StreamTranslator.java(340):
        return this.transAsciiStreamForInput (
 new ByteArrayInputStream (val.getBytes ()));
----------------------------------------
com\sap\dbtech\util\StructuredBytes.java(493):
        bytes = value.getBytes ();

I'm not sure whether these are all the lines responsible for the problem
(neither whether I didn't missed some), but telling the driver to use
"ISO8859_1" in the above lines makes the driver work fine for ASCII mode.


Below there is a piece of code, that tests the ASCII transport problem using
the special polish characters. For the example you need to create a test
table:
CREATE TABLE test_tab (id INTEGER, name VARCHAR(100))


private static void testISO(Connection conn) throws Exception {
  System.out.println("Default encoding:
"+System.getProperty("file.encoding"));

  // these are all extra polish characters in ISO8859_2
  byte[] iso2Bytes = new byte[]{
      (byte)177,(byte)230,(byte)234,(byte)179,(byte)241,(byte)243,
      (byte)182,(byte)191,(byte)188,(byte)(byte)161,(byte)198,(byte)202,
      (byte)163,(byte)209,(byte)211,(byte)166,(byte)175,(byte)172};
  // create a string from bytes
  String iso1Str = new String(iso2Bytes, "ISO8859_1");

  // prepare statements
  Statement stmt = conn.createStatement();
  PreparedStatement pstmt = null;

  // clear table
  stmt.executeUpdate("DELETE FROM test_tab");

  // update table using prepared statement and our string
  pstmt = sapdbConn.prepareStatement("INSERT INTO test_tab(id,name)
VALUES(1,?)");
  pstmt.setString(1, iso1Str);
  pstmt.executeUpdate();

  // get the result from DB
  ResultSet rs = stmt.executeQuery("SELECT name FROM test_tab WHERE id=1");
  rs.next();
  String res = rs.getString(1);
  byte[] resBytes = res.getBytes("ISO8859_1");

  // check the result
  boolean ok = true;
  for (int i=0; (i < iso2Bytes.length && ok); i++) {
    if (resBytes[i] != iso2Bytes[i]) {
      ok = false;
    }
  }

  // output the result
  if (ok) {
    System.out.println("OK :-)))");
  } else {
    System.out.println("Wrong :-(((");
    for (int i=0; i < iso2Bytes.length; i++) {
      System.out.println((iso2Bytes[i]+256)%256
+"\t-->\t"+(resBytes[i]+256)%256);
    }
  }

}


On my machine the test fails with the original driver showing the result
mapping of characters:

Connected to SAPDB.
Default encoding: Cp1250
Wrong :-(((
177 --> 177
230 --> 63
234 --> 63
179 --> 63
241 --> 63
243 --> 243
182 --> 182
191 --> 63
188 --> 63
161 --> 63
198 --> 63
202 --> 63
163 --> 63
209 --> 63
211 --> 211
166 --> 166
175 --> 63
172 --> 63


After recompiling the driver according to the above description all the
bytes get through properly:

Connected to SAPDB.
Default encoding: Cp1250
OK :-)))


Can I hope the problem will be fixed in the next release of the driver?

Best regards,

Piotr Biernat
T-Systems Polska

_______________________________________________
sapdb.general mailing list
[EMAIL PROTECTED]
http://listserv.sap.com/mailman/listinfo/sapdb.general

Reply via email to