Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting string encoding in java
    primarykey
    data
    text
    <p>Related to this question: <a href="https://stackoverflow.com/q/2622911/684934">&quot;Fix&quot; String encoding in Java</a></p> <p>My project encoding is UTF-8.</p> <p>I need to make a query to a DB that uses a particular varchar encoding (apparently EUC-KR).</p> <p>I take the input as UTF-8, and I want to make the DB query with the EUC-KR encoded version of that string.</p> <p>First of all, I can select and display the encoded strings using the following:</p> <pre><code>ResultSet rs = stmt.executeQuery("SELECT name FROM mytable"); while(rs.next()) System.out.println(new String(rs.getBytes(1), "EUC-KR")); </code></pre> <p>I want to do something like:</p> <pre><code>PreparedStatement ps = conn.prepareStatement("SELECT * FROM MYTABLE WHERE NAME=?"); ps.setString(1,input); ResultSet rs = ps.executeQuery(); </code></pre> <p>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:</p> <pre><code>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")); </code></pre> <p>I am using <strong>Oracle 10g 10.1.0</strong> </p> <p><em>More details of my attempts follow:</em></p> <p>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.</p> <p>That is,</p> <pre><code>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(); </code></pre> <p>... will result with the 1 correct entry in <code>rs</code>. Great, so now I just need to convert my input to whatever format that thing seems to be in.</p> <p>However, nothing I have tried seems to match the "correct" string when I try reading their bytes using</p> <pre><code>byte[] mybytearray = myString.getBytes(); for(byte b : mybytearray) System.out.print(b+" "); </code></pre> <p>In other words, I can turn <code>°í»ê</code> into <code>고산</code> but I can't seem to turn <code>고산</code> into <code>°í»ê</code>.</p> <p>The byte array given by </p> <pre><code>rs.getBytes(1) </code></pre> <p>is different from the byte array given by any of the following:</p> <pre><code>rs.getString(1).getBytes() rs.getString(1).getBytes("UTF8") rs.getString(1).getBytes("EUC-KR") </code></pre> <p>Unhappiness: it turns out that for my DB, <code>NLS_CHARACTERSET = US7ASCII</code></p> <p>Which means that what I'm trying to do is unsupported. Thanks for playing everyone :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload