0

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);      
}
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Skalozub
  • 539
  • 7
  • 26
  • https://stackoverflow.com/a/31113436/5471574 answers your question? – D. Kovács Jun 29 '17 at 09:51
  • I had tried it, but unfortunately it hadn't resolved my problem. – Skalozub Jun 29 '17 at 09:54
  • Is the encoding of the Java source file UTF-8? – D. Kovács Jun 29 '17 at 09:56
  • I'm taking the texts to be stored from html page. The html page charset is set to UTF-8. – Skalozub Jun 29 '17 at 10:12
  • 1
    And do you read the HTML with UTF-8 as Charset in Java? – D. Kovács Jun 29 '17 at 13:03
  • The texts are read using JavaScript code and passed to Java servlet which stores the data into Oracle DataBase. Please see an update in my question above. If the servlet returns the texts to HTML back and show them using alert operator, they will look properly. – Skalozub Jun 29 '17 at 13:17
  • Debug the Java code and see whether there is still correct or not. – D. Kovács Jun 29 '17 at 13:30
  • You are right. The texts from the HTML page are corrupt. The solution is to set a correct charset while sending data from html page to the Java servlet. The charset is set in the javascript line xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); Thank you! – Skalozub Jun 29 '17 at 18:06

1 Answers1

1

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

Next time try using duckie ;)

D. Kovács
  • 1,232
  • 13
  • 25