Question

I have a java application which keeps data in a database (MySQL), and I use JSON to transmit data in a proper form. I can fetch and insert data into a JSONArray, but whenever the ResultSet cursor reaches to the last column of the first row, execution is ended. And I have two rows in the table, SQL query is executed successfully.

I've tried to fix the issue by using many loops, conditions, etc. but the Resultset inside those loops either returns a NullPointer or SQLSyntaxError. I never succeded, so I would appreciate it if you could reply my question.


The original java class without the loops

public class JsonSuccessReport {

public JsonSuccessReport() {
}

private Connection connection;
private Statement statement;

public void GenerateSuccessReportAsJson(String idNumber) throws SQLException
{
    String query = "SELECT USERS.idnumber, SUCCESS_REPORT.subject_name, "
            + "SUCCESS_REPORT.hours, SUCCESS_REPORT.midterm, "
            + "SUCCESS_REPORT.`final`, SUCCESS_REPORT.average, "
            + "SUCCESS_REPORT.attandance, SUCCESS_REPORT.semester, "
            + "SUCCESS_REPORT.academic_year "
            + "FROM USERS "
            + "INNER JOIN SUCCESS_REPORT "
            + "ON USERS.iduser=SUCCESS_REPORT.`USERS_iduser` "
            + "WHERE USERS.idnumber=" + idNumber;

    ResultSet rs = null;

    try {
        connection = ConnectionFactory.getConnection();
        statement = connection.createStatement();

        rs = statement.executeQuery(query);

        JsonObject jsonResponse = new JsonObject();
        JsonArray data = new JsonArray();


        if (rs.next()) {
            JsonArray row = new JsonArray();
            row.add(new JsonPrimitive(rs.getString("subject_name")));
            row.add(new JsonPrimitive(rs.getString("hours")));
            row.add(new JsonPrimitive(rs.getString("midterm")));
            row.add(new JsonPrimitive(rs.getString("final")));
            row.add(new JsonPrimitive(rs.getString("average")));
            row.add(new JsonPrimitive(rs.getString("attandance")));
            row.add(new JsonPrimitive(rs.getString("semester")));
            row.add(new JsonPrimitive(rs.getString("academic_year")));
            data.add(row);
        }

        jsonResponse.add("jsonSuccessReport", data);
        System.out.println(data);

    } finally {
        ConnectionUtility.close(rs);
        ConnectionUtility.close(statement);
        ConnectionUtility.close(connection);
    }}}

GenerateSuccessReportAsJson("09010102234");

[["Information Technologies","3","0","0","0","0","spring","2013-2014"]]

The original SQL syntax and its result

SELECT USERS.idnumber, SUCCESS_REPORT.subject_name, 
       SUCCESS_REPORT.hours, SUCCESS_REPORT.midterm, 
       SUCCESS_REPORT.`final`, SUCCESS_REPORT.average, 
       SUCCESS_REPORT.attandance, SUCCESS_REPORT.semester, 
       SUCCESS_REPORT.academic_year
FROM USERS INNER JOIN SUCCESS_REPORT ON USERS.iduser=SUCCESS_REPORT.`USERS_iduser` WHERE USERS.idnumber=09010102234;

Executed query

+-------------+--------------------------+-------+---------+-------+---------+------------+----------+---------------+
| idnumber    | subject_name             | hours | midterm | final | average | attandance | semester | academic_year |
+-------------+--------------------------+-------+---------+-------+---------+------------+----------+---------------+
| 09010102234 | Information Technologies |     3 |       0 | 0     |       0 |          0 | spring   | 2013-2014     |
| 09010102234 | Conflict Management      |     3 |       0 | 0     |       0 |          0 | spring   | 2013-2014     |
+-------------+--------------------------+-------+---------+-------+---------+------------+----------+---------------+
2 rows in set (0.00 sec)

What I want to see in result

[["Information Technologies","3","0","0","0","0","spring","2013-2014"],["","3","0","0","0","0","spring","2013-2014"]]
Was it helpful?

Solution

Try changing

if (rs.next())

to

while (rs.next())

next() moves the resultset cursor and returns true if new row is valid. Check out the documentation here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top