Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set character encoding for Oracle 10g with JDBC

I am using Java and Oracle 10g database.

How can I specify the character encoding like UTF-8 for the Oracle database with JDBC?
And how can I find out the current encoding used by JDBC?

like image 474
Jack Avatar asked Nov 04 '11 19:11

Jack


People also ask

How do I change character encoding in Java?

java -Dfile. encoding="UTF-8" HelloWorld, we can specify UTF-8 charset. Method 2: Specifying the environment variable “JAVA_TOOLS_OPTIONS.” In case we start JVM starts up using some scripts and tools, the default charset can be set using the environment variable JAVA_TOOL_OPTIONS to -Dfile.

What is the default character set in Oracle?

Starting with Oracle Database 12c Release 2 (12.2), the default database character set of a database created from the General Purpose/Transaction Processing or the Data Warehousing template is Unicode AL32UTF8. Oracle recommends that you use Unicode AL32UTF8 as the database character set.

What is the correct format of JDBC URL?

CFS will construct the Oracle JDBC Connection URL as jdbc:oracle:thin:@<Host Name>:<Port Number>:<Instance Name> using the values from the input fields. In summary, CFS only supports Oracle JDBC thin connection URL syntax using SID jdbc:oracle:thin:@<HOST>:<port>:<SID>.

How do I change the character set in Oracle SQL Developer?

To select an encoding setting in Oracle SQL Developer: On the Tools menu, click Preferences. Under Environment, in the Encoding box, select the encoding setting you want to use.


2 Answers

The data transferred by the thin Oracle JDBC driver is always sent as UTF-16 (java's internal representation). The database server will translate that into whatever national character set it has been configured to use (so if the database was set up to be UTF-8, this conversion will happen automatically). Note that the character set is set at the Database level, not at the schema or connection level.

To find out the character set configured on the DB, execute this query:

SELECT value$ FROM sys.props$ WHERE name = 'NLS_CHARACTERSET' ;

(the account you're using to connect to the db will need to have the proper permissions to read system tables to do this)

like image 189
Chris Avatar answered Oct 18 '22 18:10

Chris


I'm not sure I understand the question.

The Oracle database character set is set when the database is created and is quite painful, in general, to change. Your Java application is not going to be able to specify the database character set. You can see what the database and national character set are

SELECT *
  FROM v$nls_parameters
 WHERE parameter LIKE '%CHARACTERSET'

Since your current database character set is ISO 8859-1, it will not be able to store characters from Asian languages. You can follow the instructions on character set migration in the 10g Globalization Support Guide to change the database character set of your existing database. You'll need to work with the DBA to do this since it's going to affect the entire database.

Internally, Java strings are always Unicode (UTF-16 in particular) so there is not much you can do to configure that. The output of your Java application may not be Unicode-- if your Java application is, for example, generating a web site, there is a good possibility that the web pages that are generated are using some non-Unicode character set. But I don't think that's what you're asking about.

like image 39
Justin Cave Avatar answered Oct 18 '22 19:10

Justin Cave