How to convert Java result sets to JSON?

I used the MySQL query of JDBC connector to get a result set So my job is to convert the result set to JSON format So I can send it to the client as an Ajax response Some people can explain how to convert to JSON format because I'm not familiar with Java and JSON concepts

Solution

Many people answered this question correctly However, I think I can add more value to the post with the following short piece of code It uses Apache - dbutils and gson libraries

public static String resultSetToJson(Connection connection,String query) {
        List<Map<String,Object>> listOfMaps = null;
        try {
            QueryRunner queryRunner = new QueryRunner();
            listOfMaps = queryRunner.query(connection,query,new MapListHandler());
        } catch (sqlException se) {
            throw new RuntimeException("Couldn't query the database.",se);
        } finally {
            DbUtils.closeQuietly(connection);
        }
        return new Gson().toJson(listOfMaps);
    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>