0
while(rs.next()) {      
    String s = rs.getString("first_name");
    String ss = rs.getString("last_name")
}

how do I get all columns along with records without having to create multiple strings? lets say I have 10 columns in that table and I dont't need to store it to an object

thanks.

  • Hello and welcome to StackOverflow, a website where you can get help on specific problems with code. Start with the [tour](https://stackoverflow.com/tour) and what questions are [suitable for asking](https://stackoverflow.com/help/on-topic) and which ones are [not](https://stackoverflow.com/help/dont-ask). You need to post more code for us to help you. Check out [Minimal, Complete and Verifiable Examples](https://stackoverflow.com/help/mcve). – Raymo111 Apr 22 '18 at 16:02
  • Have a read about [data access objects](https://stackoverflow.com/questions/19154202/data-access-object-dao-in-java). – jsheeran Apr 22 '18 at 16:04
  • 1
    Possible duplicate of [Mapping a JDBC ResultSet to an object](https://stackoverflow.com/questions/21956042/mapping-a-jdbc-resultset-to-an-object) – Emre Savcı Apr 22 '18 at 16:06
  • You can use database metadata and store the result set in a hash map. This way, you can easily store column names (as keys) and values (something like this https://stackoverflow.com/questions/49892329/how-to-provide-database-independency-in-jdbc-without-using-hibernate/49899622#49899622 ). – dsp_user Apr 23 '18 at 07:30

1 Answers1

0
    while (set.next()) {
        List<String> list = new ArrayList<String>();

        for (int i = 0; i < 10; i++) {
            list.add(set.getString(i + 1));
        }


    }
shanees mk
  • 70
  • 11
  • this is for 10 columns if you want to change the count of columns you can change the condition in for loop – shanees mk Apr 23 '18 at 13:49
  • Add some explanation to your answer . Code only answer are not appreciated. – ADM Apr 23 '18 at 14:43
  • rs.getString("first_name"); this is not required for getting data from a table, rs.getString(1); is okay for getting data from the first column, so I am implemented this type 10 iteration through a for loop.. then added to a list.. this is collecting datas from first 10 columns, thats all – shanees mk Apr 23 '18 at 15:12