Java - MySql : Query to get all the values of the column

balan
SELECT col_name1, col_name2, col_name3 from Table_Name;

I am using above SQL query and I have to get all the values in col_name3 which has more than 5 value.

I am using :

while(rs.next()){
    String value1 = rs.getString("col_name3");
    String value2 = rs.getString("col_name3");
}

But all value in the String value1 and value2 is the last value of the col_name3. Why?

YCF_L

If you want to get only the result of one columns then you can use a List<String>

List<String> list = new ArrayList<>();

while(rs.next()){
   list.add(rs.getString("col_name3"));
}

If you want to get all your columns then you need to change the type of your list to your Object :

List<MyObject> list = new ArrayList<>();

while(rs.next()){
   list.add(new MyObject(rs.getString("col_name1"), rs.getString("col_name2"), rs.getString("col_name3")));
}

You can create a class that contain all the columns of your Object for example :

Class MyObject{
   private String col1;
   private String col2;
   private String col3;

   //Constructor
   public MyObject(String col1, String col2, String col3){
      this.col1 = col1;
      this.col2 = col2;       
      this.col3 = col3
   }

   //getter and setters
}

EDIT

If you want to show your information you have two ways first like @Mick Mnemonic said in comment override toString() in your class MyObject like this :

@Override
public String toString() {
    return "MyObject{" + "col1=" + col1 + ", col2=" + col2 + ", col3=" + col3 + '}';
}

and your loop should look like this :

for(int i = 0; i<list.size; i++){
    System.out.println(list.get(i));
}

Or you can get element by element :

for(int i = 0; i<list.size; i++){
    System.out.println(list.get(i).getCol1() + " " + list.get(i).getCol2());
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL query to get values based on same column

From Dev

How to get all values in one column in mysql

From Dev

Get all values in column MySQL and count PHP

From Dev

SQL query to get all values of first distinct (given) column values

From Dev

Django Query to get count of all distinct values for particular column

From Dev

Query function in Java, return a list of all values from single column

From Dev

MySql Query/PHP to get all column value with same customer name

From Dev

Get column names of all the fields of a join Query in MySql

From Dev

MySQL query to get all duplicates based on values from two columns

From Dev

MySQL - Get values by Column

From Dev

Based on a variable: SQL Query to fetch data for multiple values from one column OR get all values from that column

From Dev

MySQL to append all the column values

From Dev

mysql query: searching based on all values in column rather than one value in column

From Java

SQL query to get Column A with all Column B

From Dev

MySQL query to get the sum of a column

From Dev

Get NULL values in a MySQL query

From Dev

MySQL query to get all photos

From Dev

Query to return all values in a specific table column

From Dev

Query to count the distinct words of all values in a column

From Dev

Dynamodb is it possible to query all values in a column

From Dev

CFDump of query column does not display all values

From Dev

Dynamodb is it possible to query all values in a column

From Dev

SQL query to get the Sum of all column values in the last row in access table

From Dev

filter rows based on column values in mysql query

From Dev

MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table?

From Dev

MySQL Query for ALL Comma Separated Values In String

From Dev

MySQL query to get value of coumn 1 having set of values in column 2

From Dev

Not able to extract all values of a column in mysql

From Dev

Update All Values in MySQL Column to Follow Pattern

Related Related

  1. 1

    MySQL query to get values based on same column

  2. 2

    How to get all values in one column in mysql

  3. 3

    Get all values in column MySQL and count PHP

  4. 4

    SQL query to get all values of first distinct (given) column values

  5. 5

    Django Query to get count of all distinct values for particular column

  6. 6

    Query function in Java, return a list of all values from single column

  7. 7

    MySql Query/PHP to get all column value with same customer name

  8. 8

    Get column names of all the fields of a join Query in MySql

  9. 9

    MySQL query to get all duplicates based on values from two columns

  10. 10

    MySQL - Get values by Column

  11. 11

    Based on a variable: SQL Query to fetch data for multiple values from one column OR get all values from that column

  12. 12

    MySQL to append all the column values

  13. 13

    mysql query: searching based on all values in column rather than one value in column

  14. 14

    SQL query to get Column A with all Column B

  15. 15

    MySQL query to get the sum of a column

  16. 16

    Get NULL values in a MySQL query

  17. 17

    MySQL query to get all photos

  18. 18

    Query to return all values in a specific table column

  19. 19

    Query to count the distinct words of all values in a column

  20. 20

    Dynamodb is it possible to query all values in a column

  21. 21

    CFDump of query column does not display all values

  22. 22

    Dynamodb is it possible to query all values in a column

  23. 23

    SQL query to get the Sum of all column values in the last row in access table

  24. 24

    filter rows based on column values in mysql query

  25. 25

    MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table?

  26. 26

    MySQL Query for ALL Comma Separated Values In String

  27. 27

    MySQL query to get value of coumn 1 having set of values in column 2

  28. 28

    Not able to extract all values of a column in mysql

  29. 29

    Update All Values in MySQL Column to Follow Pattern

HotTag

Archive