Results 1 to 6 of 6

Thread: Read utf-8 from DataBase

  1. #1

    Default Read utf-8 from DataBase

    Hello,

    I my database (MySQL) I have names of the european cities ex. Wrocław. Whan I read it with code :
    Code:
    	public List<CityDO> getCityList(Integer countryId) throws Exception {
    		StringBuffer sb=new StringBuffer();
    		sb.append("SELECT ");
    		sb.append("idcity, ");
    		sb.append("name ");
    		sb.append("FROM ");
    		sb.append("city ");
    		sb.append("WHERE ");
    		sb.append("idcountry =").append(countryId);
    	    
    		System.out.println("Query :"+sb.toString());
    
    		DBInterface db=DBInterface.getInstance();		
    		DataSource ds=db.getJdbcTemplate().getDataSource();
            System.out.println("DB:"+ds+"        "+ds.getConnection());
    		CityQuery pq = new CityQuery(ds,sb.toString());
    		List<CityDO> result=pq.execute();
    
            return result;		
        }
    I get crappy characters : Wrocław.


    I have transformed my DB table to support UTF-8, but I don't know why it doesn't work.

    Any ideas ?

  2. #2
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

    Try displaying the characters in a browser page.

    Not sure printing them to the console will work.

  3. #3

    Default

    I have done it. I am displaying the result of query in select list, but the result is the same. I also made sure my browser supports UTF-8 characters.

    any other ideas ?

    Regards

  4. #4
    Join Date
    Apr 2007
    Location
    London
    Posts
    5

    Default

    Are you sure that the data in the table your trying to get isn't in a different encoding? I had a similar problem before where I was entering data into the database but when it came back it wasn't UTF8. The problem was due to the fact the data I was entering was being entered in the database as 8859-1.

    Try converting the text you get from the database into UTF8 and see if that works. Can't remember where I got this from but it helped with the converting of the text.
    Code:
        public static String toUTF8(String isoString) {
            String utf8String;
            if (StringUtils.isNotBlank(isoString)) {
                try {
                    byte[] stringBytesISO = isoString.getBytes("ISO-8859-1");
                    utf8String = new String(stringBytesISO, "UTF-8");
                }
                catch (UnsupportedEncodingException e) {
                    // As we can't translate just send back the best guess.
                    log.error("UnsupportedEncodingException is: {}", e);
                    utf8String = isoString;
                }
            } else {
                utf8String = isoString;
            }
            return utf8String;
        }

  5. #5
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

    Couple of gotchas to check for.

    1. Make sure you're setting the charset encoding in your page. Probably easiest done with org.springframework.web.filter.CharacterEncodingFi lter and using an init-param of encoding=UTF-8

    2. Avoid raw InputStream and OutputStream usage. Use InputStreamReader and OutputStreamWriter if you must.

    3. Seemed to be the case but not sure now...use <spring:message> in favour of <c:out>, since <c:out> seems to message up encoding. otherwise just use JSP 2.0 ${someproperty} notation.

    How are you building your select list?

  6. #6

    Default

    I will try tonight. I have feeling that it will work otherwise I will be back on the forum. My select list is build dynamiclly by DWR (addOptions function.)
    Thank for your input.

    Michal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •