Note that there are some explanatory texts on larger screens.

plurals
  1. PO’ character being converted to ’ in jdbc
    primarykey
    data
    text
    <p>I am trying to read a UTF-8 string from my MySql database, which I create using:</p> <pre><code>CREATE DATABASE april DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; </code></pre> <p>I make the table of interest using:</p> <pre><code>DROP TABLE IF EXISTS `article`; CREATE TABLE `article` ( `id` int(11) NOT NULL AUTO_INCREMENT, `text` longtext NOT NULL, `date_created` timestamp DEFAULT NOW(), PRIMARY KEY (`id`) ) CHARACTER SET utf8; </code></pre> <p>If I <code> select * from article</code> in the MySql command line util, I get:</p> <pre><code>OIL sands output at Nexen’s Long Lake project dropped in February. </code></pre> <p>However, when I do </p> <pre><code>ResultSet rs = st.executeQuery(QUERY); long id = -1; String text = null; Timestamp date = null; while (rs.next()) { text = rs.getString("text"); LOGGER.debug("text=" text); } </code></pre> <p>the output I get is: </p> <pre><code>text=OIL sands output at Nexen’s Long Lake project dropped in February. </code></pre> <p>I get my Connection via:</p> <pre><code>DriverManager.getConnection("jdbc:" + this.dbms + "://" + this.serverHost + ":" + this.serverPort + "/" + this.dbName + "?useUnicode&amp;user=" + this.username + "&amp;password=" + this.password); </code></pre> <p>I've also tried, instead of the <code>useUnicode</code> parameter:</p> <pre><code>characterEncoding=UTF-8 and characterEncoding=utf8 </code></pre> <p>I also tried, instead of the line <code>text = rs.getString("text")</code></p> <pre><code>rs.getBytes("text"); String[] encodings = new String[]{"US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16", "Latin1"}; for (String encoding : encodings) { text = new String(temp, encoding); LOGGER.debug(encoding + ": " + text); } // Which outputted: US-ASCII: OIL sands output at Nexen��������s Long Lake project dropped in February. ISO-8859-1: OIL sands output at Nexenââ¬â¢s Long Lake project dropped in February. UTF-8: OIL sands output at Nexen’s Long Lake project dropped in February. UTF-16BE: 佉䰠獡湤猠潵瑰畴⁡琠乥硥滃ꋢ芬ꉳ⁌潮朠䱡步⁰牯橥捴⁤牯灰敤⁩渠䙥扲畡特� UTF-16LE: 䥏⁌慳摮⁳畯灴瑵愠⁴敎數썮겂蓢玢䰠湯⁧慌敫瀠潲敪瑣搠潲灰摥椠敆牢慵祲� UTF-16: 佉䰠獡湤猠潵瑰畴⁡琠乥硥滃ꋢ芬ꉳ⁌潮朠䱡步⁰牯橥捴⁤牯灰敤⁩渠䙥扲畡特� Latin1: OIL sands output at Nexenââ¬â¢s Long Lake project dropped in February. </code></pre> <p>I load the strings into the DB using some pre-defined sql in a file. This file is UTF-8 encoded.</p> <pre><code>mysql -u april -p -D april &lt; insert_articles.sql </code></pre> <p>This file includes the line: </p> <pre><code> INSERT INTO article (text) value ("OIL sands output at Nexen’s Long Lake project dropped in February."); </code></pre> <p>When I print out that file within my application using: </p> <pre><code>BufferedReader reader = new BufferedReader(new FileReader(new File("/home/path/to/file/sql_article_inserts.sql"))); String str; while((str = reader.readLine()) != null) { LOGGER.debug("LINE: " + str); } </code></pre> <p>I get the correct, expected output:</p> <pre><code>LINE: INSERT INTO article (text) value ("OIL sands output at Nexen’s Long Lake project dropped in February."); </code></pre> <p>Any help would be much appreciated.</p> <p>Some System Details: I am running on linux (Ubuntu)</p> <p>Edits:<br/> * Edited to specify OS<br/> * Edited to detail output of reading sql input file.<br/> * Edited to specify more about how the data is inserted into the DB.<br/> * Edited to to fix typo in code, and clarify example.<br/></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.
    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