简体   繁体   中英

How to display more than one row from database in a textarea

I'm trying to get all rows of beneficiaries table where localgovernment is the content of a text field "search" to display in a single textarea "result". the database is not having fix number of rows.
here is my code:

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);

how do i make this to display rows of variable length in a textarea?

Simply use loop.

One more thing to add, you can use List types like ArrayList or LinkedList .

Another simple way to display data is to use JTable instead of Textarea .
Try this...

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM