sqlite默认使用UTF-8编码,从数据库(UTF-8)查出来的时候遍历ResultSet时候一定使用:rs.getBytes(columnName),不能使用re.getString(XXX);可参照下面代码:

public static String resultSetToJson(ResultSet rs) throws SQLException, JSONException, UnsupportedEncodingException { // json数组 JSONArray array = new JSONArray(); // 获取列数 ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); // 遍历ResultSet中的每条数据 while (rs.next()) { JSONObject jsonObj = new JSONObject(); // 遍历每一列 for (int i = 1; i <= columnCount; i++) { String value = null; String columnName = metaData.getColumnLabel(i);//列名称 if (rs.getString(columnName) != null&&!rs.getString(columnName).equals("")) { value = new String(rs.getBytes(columnName));//列的值,有数据则转码 System.out.println("==="+value); } else { value = "";//列的值,为空,直接取出去 } jsonObj.put(columnName, value);} array.add(jsonObj); } return array.toString(); }

 

实际操作中,获取某个字段的中文字符,这样也是可以的,不会乱码:String bb = new String(rs1.getString("sql_expression").getBytes("utf-8"), "utf-8");