neo4j jdbc中文乱码

neo4j的jdbc连接实际上就是发送http请求(使用到了httpClient),对于中文而言,在插入数据时,jdbc用utf-8编码post提交,但是中文数据返回来的时候,并没有说明数据是utf-8的编码,因此httpClient会用平台的编码解析数据,如果平台编码是gbk等其他编码,好的情况下,可以将平台编码解析后的ResultSet,再用正确的编码解析数据;坏的情况下,在转存ResultSet时无法解析数据,直接抛个异常。

 

解决方案:

因为neo4j使用到了restlet处理请求,可以修改restlet的ClientResource的post方法,如下:

 /**
     * Posts a representation. If a success status is not returned, then a
     * resource exception is thrown.
     * 
     * @param entity
     *            The posted entity.
     * @return The optional result entity.
     * @throws ResourceException
     * @see <a
     *      href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5">HTTP
     *      POST method</a>
     */
    public Representation post(Representation entity) throws ResourceException {
        Representation r = handle(Method.POST, entity);
        r.setCharacterSet(CharacterSet.UTF_8);   //通过多次实验,neo4j只用到了这个post方法,其他post方法无需设置
        return r;
    }

然后在将改好的源码打成jar包,即可解决中文乱码问题。

打好包的jar:http://pan.baidu.com/s/152uia

posted @ 2015-02-06 23:30  世间安得两全法  阅读(2292)  评论(0编辑  收藏  举报