2

I am referring to this link but I have slightly modified my approach..

This is the code that I have :

 public JSONArray generateJSON(ResultSet rs) {

        JSONArray respJSON = new JSONArray();

        try {
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            int numColumns = rsmd.getColumnCount();
            while (rs.next()) {
                JSONObject obj = new JSONObject();
                for (int i = 1; i < numColumns + 1; i++) {

                    String columnName = rsmd.getColumnName(i);
                    if (rsmd.getColumnType(i) == java.sql.Types.ARRAY) {
                        obj.put(columnName, rs.getArray(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.BIGINT) {
                        obj.put(columnName, rs.getInt(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.BOOLEAN) {
                        obj.put(columnName, rs.getBoolean(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.BLOB) {
                        obj.put(columnName, rs.getBlob(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.DOUBLE) {
                        obj.put(columnName, rs.getDouble(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.FLOAT) {
                        obj.put(columnName, rs.getFloat(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.INTEGER) {
                        obj.put(columnName, rs.getInt(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.NVARCHAR) {
                        obj.put(columnName, rs.getNString(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.VARCHAR) {
                        obj.put(columnName, rs.getString(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.TINYINT) {
                        obj.put(columnName, rs.getInt(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.SMALLINT) {
                        obj.put(columnName, rs.getInt(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.DATE) {
                        obj.put(columnName, rs.getDate(i));
                    } else if (rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP) {
                        obj.put(columnName, rs.getTimestamp(i));
                    } else {
                        obj.put(columnName, rs.getObject(i));
                    }

                }
                respJSON.put(obj);
                //respJSON.add(obj);

            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        respJSON.toString();

        return respJSON;

        System.out.print(respJSON.toString());
    }

My problem is that I can not print the json string to see it in the console ...

I have tried the respJSON.toString(); and it doesnt seem to work ..

Could you please help me ? ...

Thank you

Community
  • 1
  • 1
David
  • 1,469
  • 5
  • 33
  • 51

2 Answers2

4

this is a simple typo. instead of this:

    respJSON.toString();
    return respJSON;
    System.out.print(respJSON.toString());

do this

    System.out.print(respJSON.toString());
    return respJSON;
bpgergo
  • 15,669
  • 5
  • 44
  • 68
1

Your program cannot reach to the print statement as there is a return statement before that. If you had been using an IDE like Netbeans or Eclipse, that would have given you a clear warning.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
Muhammad Saifuddin
  • 1,284
  • 11
  • 29