Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be using JDBC getNString() instead of getString()?

We are building a Java application backed by an Oracle database that we access using JDBC (drivers ojdbc6.jar and orai18n.jar). The database schema stores text columns primarily using the NVARCHAR2 data type.

The JDBC documentation for the JDBC ResultSet says that getNString() is particularly intended for use with the NCHAR, NVARCHAR etc. data types, but at the moment we are only using getString().

This seems to work fine, so I am wondering why I should use getNString() rather than getString(). Is getString() going to start failing if non-ASCII characters are entered, or is the Oracle JDBC driver indifferent as to which method I should use?

EDIT: Seems that it may be database-dependent: SQL Server doesn't seem to mind which you use, depending on the connection parameters. Does anyone have any specific information on Oracle?

like image 748
Gnat Avatar asked May 05 '11 03:05

Gnat


People also ask

What is getNString in Java?

This getNString method is specified by the getNString method in the java. sql. SQLServerResultSet interface. This method can be used to retrieve the value of an nvarchar, nchar, nvarchar(max), ntext, or xml column in the current row of this SQLServerResultSet object.

What does getString return in Java?

Retrieves the value of the designated column name in the current row of this SQLServerResultSet object as a String in the Java programming language.

Which method is used to return a ResultSet?

The ResultSet interface declares getter methods (for example, getBoolean and getLong ) for retrieving column values from the current row. You can retrieve values using either the index number of the column or the alias or name of the column.

What is the use of wasNull () in ResultSet interface?

The wasNull() method of the ResultSet interface determines whether the last column read had a Null value. i.e. whenever you read the contents of a column of the ResultSet using the getter methods (getInt(), getString etc...) you can determine whether it (column) contains null values, using the wasNull() method.


2 Answers

I have done a test on our application and it seems that getNString() is unnecessary with Java 6, JDBC 6, Oracle JDBC 6 drivers and Oracle 11.1.0.6.0. The test string I used was "Δ, Й, ק, ‎ م, ๗, あ, 叶, 葉, and 말", copied from http://en.wikipedia.org/wiki/Unicode.

Most of our data access is done via stored procedures. Java was able to set and retrieve the above test string correctly via setObject() and getString() (not setString() for abstraction reasons), collecting data from the interface and writing it back to the interface as expected.

Hence getString() works ok for Unicode data with Oracle 11g (like SQL Server as in the above link) so we will continue to use this rather than getNString().

like image 80
Gnat Avatar answered Nov 14 '22 18:11

Gnat


If your DB uses the NVARCHAR2 datatype, it is designed to be storing multilingual data. Your program will break if any unicode data is stored in those columns. If I were you, I would move over to the getNXXX() methods

like image 37
qwerty Avatar answered Nov 14 '22 18:11

qwerty