0

I am trying to gather information from a MySQL database and then exporting that information to a Excel document using POI and JAVA.

The only problem I am experiencing is increasing the number after data.put, each time if(rs.next()) is executed, for example:

Map<String, Object[]> data = new HashMap<>();
            data.put("1", new Object[] {"Emp No.", "Name", "Salary"});

            if(rs.next()) {
                data.put("2", new Object[] {1d, "John", 1500000d, "Test"});
            }

The 2 should increase to 3 then to 4 then to 5, depending on the rows in the MySQL database.

Any help is appreciated.

1 Answer 1

1
int cnt=2;

while(rs.next()) 
{
    data.put(""+cnt, new Object[] {1d, "John", 1500000d, "Test"});
    cnt++;//increment to 3, 4, etc
}

try like this

Sign up to request clarification or add additional context in comments.

2 Comments

For some reason, this is only writing one line.
use while not if. if is going to check the condition only on time.

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.