Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4576624
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:13:56+00:00 2026-05-21T20:13:56+00:00

Related to this question: "Fix" String encoding in Java My project encoding is UTF-8.

  • 0

Related to this question: "Fix" String encoding in Java

My project encoding is UTF-8.

I need to make a query to a DB that uses a particular varchar encoding (apparently EUC-KR).

I take the input as UTF-8, and I want to make the DB query with the EUC-KR encoded version of that string.

First of all, I can select and display the encoded strings using the following:

ResultSet rs = stmt.executeQuery("SELECT name FROM mytable");
while(rs.next())
    System.out.println(new String(rs.getBytes(1), "EUC-KR"));

I want to do something like:

PreparedStatement ps = conn.prepareStatement("SELECT * FROM MYTABLE WHERE NAME=?");
ps.setString(1,input);
ResultSet rs = ps.executeQuery();

Which obviously won’t work, because my Java program is not using the same encoding as the DB. So, I’ve tried replacing the middle line with each of the following, to no avail:

ps.setString(1,new String(input.getBytes("EUC-KR")));
ps.setString(1,new String(input.getBytes("EUC-KR"), "EUC-KR"));
ps.setString(1,new String(input.getBytes("UTF-8"), "EUC-KR"));
ps.setString(1,new String(input.getBytes("EUC-KR"), "UTF-8"));

I am using Oracle 10g 10.1.0

More details of my attempts follow:

What does seem to work is saving the name from the first query into a string without any other manipulation, and passing that back as a parameter. It matches itself.

That is,

ResultSet rs = stmt.executeQuery("SELECT name FROM mytable");
rs.next();
String myString = rs.getString(1);
PreparedStatement ps = conn.prepareStatement("SELECT * FROM mytable WHERE name=?");
ps.setString(1, myString);
rs = ps.executeQuery();

… will result with the 1 correct entry in rs. Great, so now I just need to convert my input to whatever format that thing seems to be in.

However, nothing I have tried seems to match the “correct” string when I try reading their bytes using

byte[] mybytearray = myString.getBytes();
for(byte b : mybytearray)
    System.out.print(b+" ");

In other words, I can turn °í»ê into 고산 but I can’t seem to turn 고산 into °í»ê.

The byte array given by

rs.getBytes(1)

is different from the byte array given by any of the following:

rs.getString(1).getBytes()
rs.getString(1).getBytes("UTF8")
rs.getString(1).getBytes("EUC-KR")

Unhappiness: it turns out that for my DB, NLS_CHARACTERSET = US7ASCII

Which means that what I’m trying to do is unsupported. Thanks for playing everyone 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:13:57+00:00Added an answer on May 21, 2026 at 8:13 pm

    You can’t accomplish anything with a String constructor. String is always UTF-16 inside. Converting UTF-16 chars to EUC-KR and back again won’t help you.

    Putting invalid Unicode into String values in the hopes that they will then be converted to EUC-KR is a really bad idea.

    What you are doing is supposed to ‘just work’. The oracle driver is supposed to talk to the server, find out the desired charset, and go from there.

    What, however, is the database charset? If someone is storing EUC-KR without having set the charset to EUC-KR, you are more or less up a creek.

    What you need to do is to tell your jdbc driver what charset to use to communicate with the server. You haven’t mentioned if you are using Thin or OCI, the answer might be different.

    Judging from http://download.oracle.com/docs/cd/E14072_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html, you might want to try turning on defaultNChar.

    In general, it’s the job of the jdbc driver to transcode String to what the Oracle server wants. You may need tnsnames.ora options if you are using ‘OCI’.

    edit

    OP reports that the nls_charset of the database is US7ASCII. That means that all JDBC drivers will think that it is their job to convert Unicode String values to ASCII. Korean characters will be reduced to ? at best. Officially, then, your are up a creek.

    There are some possible tricks to try. One is the very dangerous trick of

     new String(string.getBytes("EUC-KR"), "ascii")
    

    that will try to make a string of Unicode chars that just so happens to have the values of EUC-KR in their low bytes. My belief is that this will corrupt data, but you could experiment.

    Or, perhaps, ps.setBytes(n, string.getBytes("EUC-KR")), but I myself do not know if Oracle defines the conversion of bytes to chars as a binary copy. It might. Or, perhaps, adding a stored proc that takes a blob as an argument.

    Really, what’s called for here is to repair the database to use an nls_charset of UTF-8 or EUC-KR, but that’s a whole other job.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.