[英]Java - MySql : Query to get all the values of the column
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. 我正在使用上面的SQL查询,我必须获取col_name3中具有超过5个值的所有值。
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. 但是字符串value1和value2中的所有值都是col_name3的最后一个值。 Why?
为什么?
This problem is because if you need to store all of the data you can't do it with only a 'String' object but with a Collection maybe so you will do: 这个问题是因为如果您需要存储所有数据,则不能仅使用“ String”对象而是使用Collection来做到这一点,所以您可以这样做:
List<String> list = new ArrayList();
while(rs.next()){
String value=rs.getString("col_name3");
list.add(value);
}
Because as are you doing you store every time a new String in value1 and value2, overwriting the previous values so you will get only the last one. 因为在执行操作时,每次都会在value1和value2中存储一个新的String,因此会覆盖以前的值,因此您只会得到最后一个。
With using a Collection you can dynamically store 'String' that you get in 'ResultSet' and save them all. 通过使用Collection,您可以动态存储在'ResultSet'中获得的'String'并将其全部保存。
Then to operate on them you can use a forEach like this: 然后,可以对它们进行操作,如下所示:
for(String value : list){
System.out.prinln(value);
}
This will show in console every value in 'list'. 这将在控制台中显示“列表”中的每个值。
If you want to get only the result of one columns then you can use a List<String>
如果只想获取一列的结果,则可以使用
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 : 如果要获取所有列,则需要将列表的类型更改为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 : 您可以创建一个包含Object的所有列的类,例如:
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 : 如果要显示信息,则首先有两种方式,例如@Mick Mnemonic在注释中说,它
override toString()
类MyObject中的override toString()
,如下所示:
@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());
}
because every time you overwrite previous value. 因为每次您覆盖以前的值。 Try using a List:
尝试使用列表:
List<string> myValues=new List<string>();
while(rs.next()){
myValues.Add(rs.getString("col_name3"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.