2

I am trying to get a row and convert it to a array I alredy tried to convert the resultset.getArray() to a ArrayList but for some reason it returned nothing somebody help me with this

code now:

public ArrayList<String> getReport(String name) {
    try {
        Statement sql =  mySql.getConnection().createStatement();
        ResultSet resultSet = sql.executeQuery("SELECT * FROM `reports` WHERE `reportedplayer`='" + name + "';");
        if(!resultSet.next()) {
            sql.close();
            resultSet.close();
            return null;
        }
        ArrayList<String> rowValues = new ArrayList<String>();
        while (resultSet.next()) {
            rowValues.add(resultSet.getString("reason"));
        }
        sql.close();
        resultSet.close();
        return rowValues;
    }catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
}
6
  • How many rows do you have in the reports table? If you only have one row, you will never enter the while loop. Commented May 20, 2015 at 16:02
  • 1
    I wonder what would happen if name is "'; drop table reports --". Commented May 20, 2015 at 16:04
  • @Chetan Kinger well sometimes there are more but sometimes 1 Commented May 20, 2015 at 16:05
  • @DaanSander Does your program fail when there is more than one row? I believe it only fails when you have one row. Commented May 20, 2015 at 16:06
  • 1
    The complete block of checking for no results is superfluous and causing problems. If you get no data, you will fall through the while loop reading the data and close the resultset and statement just as well. Commented May 20, 2015 at 16:10

3 Answers 3

1

You should read column names from resultSet metadata and then iterate over the names for each row. Maybe better idea will be to store the parameters as ArrayList<HashMap<String,String>>than intoArrayList<String>, since select * doesn't say anything about order of the fileds you read. But if you know for sure you have just one row, and you know the order, or maybe the order doesn't matter, you could use something like this:

public ArrayList<String> getReport(String name) {
    try {
        Statement sql =  mySql.getConnection().createStatement();
        ResultSet resultSet = sql.executeQuery("SELECT * FROM `reports` WHERE `reportedplayer`='" + name + "';");
        if(!resultSet.next()) {
            sql.close();
            resultSet.close();
            return null;
        }
        ArrayList<String> rowValues = new ArrayList<String>();
        int columnCount = resultSet.getMetaData().getColumnCount();
        String[] columnNames = new String[columnCount];
        for (int idx=0; idx<columnCount; idx++) {
            columnNames[idx] = resultSet.getMetaData().getColumnName(idx);
        }

        while (resultSet.next()) {
            for (String columnName: columnNames) {
                rowValues.add(resultSet.getString(columnName));
            }
        }
        sql.close();
        resultSet.close();
        return rowValues;
    }catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your line:

if (!resultSet.next())

Is moving to the second row without even looking at the first row. You should also have a finally block to handle closing to ensure that it is always carried out properly.

try {
    Statement sql =  mySql.getConnection().createStatement();
    ResultSet resultSet = sql.executeQuery("SELECT * FROM `reports` WHERE `reportedplayer`='" + name + "';");
    List<String> rowValues = new ArrayList<String>();
    while (resultSet.next()) {
        rowValues.add(resultSet.getString("reason"));
    }
    return rowValues.isEmpty() ? null : rowValues;
} catch (SQLException e) {
    e.printStackTrace();
    return null;
} finally {
    sql.close();
    resultSet.close();
}

4 Comments

The if statement will only create a problem if there is only one row in the table. Unless the OP confirms this, your answer is a guess and not actually an answer.
It's a potential solution to the problem - therefore an answer.
A potential solution is not really the same as a solution. Stackoverflow allows comments for potential solutions.
Unfortunately comments don't allow code blocks and as well as my potential solution I provided another improvement for the OP which would have been harder to demonstrate without them.
1

resultSet.next() will move the cursor to the next row. 1st time when you call resultSet.next() in the if condition it will point to 1st row and when you again call resultSet.next() in the while condition it will point to the 2nd row.

since you are using the resultSet.next() twice you will be getting the 2nd row onwards into the list. If you have only one row in the table you will not get anything in the list.

You can close the connection in the finally block as shown below.

    finally
    {
        try {              
        sql.close();
        resultSet.close();
        } catch (SQLException e) {
        e.printStackTrace();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.