Search code examples
javaoracle-databasejdbcutf-8netweaver

Russian symbols via JDBC (SAP NetWeaver 7.3.1, Oracle 12c)


I'm trying to save Russian texts via JDBC connect on SAP Java instance (SAP NetWeaver 7.3.1). I'm using next Java code to obtain the JDBC connection and save the data:

    InitialContext ctx = new InitialContext();      
    DataSource ds = (DataSource) ctx.lookup("jdbc/DS_Test");
    Connection conn = ds.getConnection();
    PreparedStatement stmt = conn.prepareStatement("INSERT INTO Z_TEST_TAB VALUES(?, ?, ?)");
    stmt.setString(1, "KEY_LINE");
    stmt.setString(2, "Текст2");
    stmt.setString(3, "Текст3");
    stmt.execute();
    stmt.close();       

The data from the query are inserted, but the Russian texts are not saved properly.

It's usually recommended to add UTF-8 parameters to the DataSource URL to fix the issue. I have added useUnicode and characterEncoding paramters to the DataSource URL:

jdbc:oracle:thin:@db_inst:1521:RR0?useUnicode=true&characterEncoding=UTF-8

But it hasn't resolved the problem. The connection to the DataBase cannot be established at all, if I use the parameters. At the same time the connection works fine without the parameters (but the Russian symbols are not saved properly)

The DataBase looks to be set up for saving Russian texts, because I can save the texts using another tools (for example queries from NetWeaver Developer Studio).

The oracle version is Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production 0

The texts are read from HTML page which is UTF-8 encoded. The values are read using Javascript and passed into Java servlet which stores the texts in the Oracle DataBase.

function save() {
  var texts = document.querySelectorAll('.text_fields'); 
  var text1 = texts[0].textContent; 
  var text2 = texts[1].textContent;
  var text3 = texts[2].textContent;
  var body = 'TEXT1='+encodeURIComponent(text1)+'&TEXT2='+encodeURIComponent(text2)+'&TEXT3='+encodeURIComponent(text3);

  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open( "POST", "DBClassSave", true );
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlHttp.onload = function() {
  if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            alert("Data have been saved");
            location.reload();              
        } else {
            alert("An exception has been occurred: "+xmlHttp.status+xmlHttp.responseText); 
        }
    }
  }     
xmlHttp.send(body);      
}

Solution

  • As discussed in the comments, the problem was character-encoding corruption along the data pipe.

    Next time try using duckie ;)