Saturday, April 4, 2015

Simple Converter of Java ResultSet into JSONArray

I am now working on a program to upload Data from SQLite to MySQL database on the server. This is one of the tasks to complete this. I found this function on this blog

Here's your full code :

import org.json.JSONArray;
import org.json.JSONObject;
import java.sql.ResultSet;

public class Convertor {
     * Convert a result set into a JSON Array
     * @param resultSet
     * @return a JSONArray
     * @throws Exception
    public static JSONArray getJSON(ResultSet resultSet)
            throws Exception {
        JSONArray jsonArray = new JSONArray();
        while ( {
            int total_rows = resultSet.getMetaData().getColumnCount();
            JSONObject obj = new JSONObject();
            for (int i = 0; i < total_rows; i++) {
                obj.put(resultSet.getMetaData().getColumnLabel(i + 1)
                        .toLowerCase(), resultSet.getObject(i + 1));
        return jsonArray;

1 comment:

Unknown said...

The problem that you're having is a common issue that can be fixed in collaboration with (credentials by the link).