Friday, October 3, 2008

Using for loop a method returning arraylist in java struts using jdbc

Here is the sample method is given which is retuning the database values with the arraylist in which the for loop is used to execute the query multiple time.

public ArrayList attToatlAndPresenty(String[] grNumber,String datefrom,String dateto) throws DataAccessException,java.sql.SQLException{

ArrayList list= new ArrayList();

MYSQLConnect connect=new MYSQLConnect();

Connection conn = connect.connection;

Statement smt = conn.createStatement();

ResultSet rs = null;

//write select query for checking password

for(int i=0;i

{

String query="select SUM(attendance) As presenty,count(attendance)as

total,SUM(attendance)/count(attendance)*100 as percentage

from attendance where gr_number='"+grNumber[i]+"' and (Date

between '"+datefrom+"' and '"+dateto+"') order by `date` ";

rs=smt.executeQuery(query);

while(rs.next()== true){

StudentDetailsForm form =new StudentDetailsForm();

form.setPresentdays(rs.getInt("presenty"));

form.setTotaldays(rs.getInt("total"));

form.setAverage(rs.getInt("percentage"));

System.out.println(rs.getInt("percentage"));

list.add(form);

}

}

smt.close();

rs.close();

conn.close();

return list;

}

No comments: