Search code examples
javasql-serverunicodejtds

JTDS: Unicode parameters using CallableStatement with sendStringParametersAsUnicode=false


We have encountered the performance issues described in the JTDS documentation regarding index scans (SQL Server 2000 and upwards), and have therefore had to set the sendStringParametersAsUnicode parameter to false.

This is fine for 99.9% of our cases, however, we have an application that does rely on unicode data in an ntext field. We write to the aforementioned table using a stored procedure, which has an NTEXT parameter. Since changing the above setting, our unicode strings are translated to '?' characters, which is not particularly useful.

I have fiddled with various things, including:

  • setObject(1, unicode_string, Types.NCLOB); //as well as NVARCHAR

  • stmt.setUnicodeStream(1, new ByteArrayInputStream(unicode_string.getBytes("UTF16")), unicode_string.length());

  • setNClob(1, unicode_string);

None of these however work. Any ideas?


Solution

  • One workaround (though its not the correct answer), is to use a Statement rather than a CallableStatement:

    stmt = cn.createStatement();
    stmt.execute("INSERT INTO test_unicode (my_unicode) VALUES (N'" + input + "')");
    

    This is however presents a significant performance overhead.