我试图获取所有受益人行表,其中localgovernment是文本字段的内容" search"显示在单个textarea"结果"。数据库没有固定行数 这是我的代码:
String bs = search.getText();
String SQL = "select * from BENEFICIARIES where localgovernment = '" +bs+"'";
rs = stmt.executeQuery(SQL);
String[] kjh;
kjh = new String[20];
int sd = 0;
while(rs.next()){
int id = rs.getInt("bid");
String name = rs.getString("name");
String gender = rs.getString("gender");
String Phone = rs.getString("phone");
String mail = rs.getString("email");
String lga = rs.getString("localgovernment");
String address = rs.getString("address");
String etype = rs.getString("typeofempowerment");
String benefits = rs.getString("listofbenefits");
mega = id + " "+ name +" "+gender+" "+Phone+" "+mail+" "+lga+" "+address+" "+etype+" "+benefits;
kjh[sd] = mega;
sd = sd + 1;
}
String jhg = kjh[0] +"\n"+ kjh[1] +"\n"+ kjh[2] +"\n"+ kjh[3] +"\n"+ kjh[4] +"\n"+ kjh[5] +"\n"+ kjh[6] +"\n"+ kjh[7] +"\n"+ kjh[8] +"\n"+ kjh[9] +"\n"+ kjh[10] +"\n"+ kjh[11] +"\n"+ kjh[12] +"\n"+ kjh[13] +"\n"+ kjh[14] +"\n"+ kjh[15] +"\n"+ kjh[16] +"\n"+ kjh[17] +"\n"+ kjh[18] +"\n"+ kjh[19];
result.setText(jhg);
如何让它在textarea中显示可变长度的行?
答案 0 :(得分:1)
只需使用循环。
还有一件事需要添加,您可以使用List
或ArrayList
等LinkedList
类型。
另一种显示数据的简单方法是使用JTable
代替Textarea
试试这个......
String bs = search.getText();
String SQL = "select * from BENEFICIARIES where localgovernment = '" +bs+"'";
rs = stmt.executeQuery(SQL);
List<String> kjh = new ArrayList<String>();
while(rs.next()){
int id = rs.getInt("bid");
String name = rs.getString("name");
String gender = rs.getString("gender");
String Phone = rs.getString("phone");
String mail = rs.getString("email");
String lga = rs.getString("localgovernment");
String address = rs.getString("address");
String etype = rs.getString("typeofempowerment");
String benefits = rs.getString("listofbenefits");
mega = id + " "+ name +" "+gender+" "+Phone+" "+mail+" "+lga+" "+address+" "+etype+" "+benefits;
kjh.add(mega);
}
String jhg="";
for(String s:kjh)
jhg+=s+"\n";
result.setText(jhg);