这是我用来从“GeneID”列中检索数据的编码。使用此编码,我可以在“GeneID”列下打印所有名称。但我想存储列中的每个名称以分别访问这些名称。任何人都可以帮我这个吗?
String name1[] = new String[100];
int i = 0;
String query = "select distinct GeneID from gene1";
PreparedStatement pest = connection.prepareStatement(query);
ResultSet rs = pest.executeQuery();
while (rs.next()) {
name1[i] = rs.getString("GeneID");
System.out.println(name1[i]);
}
rs.close();
pest.close();
答案 0 :(得分:2)
对于使用数组,您必须在i
while (rs.next()) {
String name1[] = new String[100];
int i = 0;
String query = "select distinct GeneID from gene1";
PreparedStatement pest = connection.prepareStatement(query);
ResultSet rs = pest.executeQuery();
while (rs.next()) {
name1[i] = rs.getString("GeneID");
System.out.println(name1[i]);
i++;
}
rs.close();
pest.close();
}
要访问,您可以遍历数组
for(int i;i<name1.length-1;i++){
System.out.println("Id is "+name1[i]);
}
或者您可以使用
for (String id: name1) {
System.out.println("Id is "+name1[i]);
}
您还可以使用ArrayList存储变量,如下所示
ArrayList ar=new ArrayList();
String query = "select distinct GeneID from gene1";
PreparedStatement pest = connection.prepareStatement(query);
ResultSet rs = pest.executeQuery();
while (rs.next()) {
String id = rs.getString("GeneID");
ar.add(id);
}
rs.close();
pest.close();
}
迭代ArrayList
for (int i = 0; i < ar.size(); i++) {
System.out.println("Id is "+ar.get(i));
}
或
for (String id : ar) {
System.out.println("Id is "+ar.get(i));
}
答案 1 :(得分:0)
使用arrayList,这样你就可以使用.add()的方法了,这证明很有帮助
List ls=new ArrayList();
ResultSet rsp=pss.executeQuery("select * from student");
while(rsp.next()){
ls.add(rsp.getString("your_column_name"));
}
rsp.close();