日期:2014-05-17  浏览次数:20801 次

ArrayList求助
求大神帮助小弟下,
开始定义了DaoVote类
package dao;
import java.sql.*;
import java.util.*;
import vo.Vote;
public class VoteDao {
    private Connection conn=null;
    public void initConnection() throws Exception{
     Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","mysqladmin");
    }
    public ArrayList<Vote> getAllVotes() throws Exception{
     ArrayList<Vote> al=new ArrayList<Vote>();
      
     initConnection();
     String sql="select teacherno,teachername,vote from t_vote";
     Statement stat=conn.createStatement();
     ResultSet rs=stat.executeQuery(sql);
     while(rs.next()){
     Vote vote=new Vote();
     vote.setTeacherno(rs.getString("teacherno"));
     vote.setTeachername(rs.getString("teachername"));
     vote.setVotenumber(rs.getInt("vote"));
     al.add(vote);
     }
     conn.close();
     return al;
    }
    public void updateVotes(String[] teacherno)throws Exception{
     initConnection();
     String sql="update t_vote set vote=vote+1 where teacherno=?";
     PreparedStatement ps=conn.prepareStatement(sql);
     for(int i=0;i<teacherno.length;i++){
     ps.setString(1,teacherno[i]);
     ps.executeUpdate();
     }
     conn.close();
    }
}
在JSP中使用
VoteDao vdao=new VoteDao();
                ArrayList<Vote> votes=vdao.getAllVotes();
                for(int i=0;i<votes.size();i++){
                    Vote vote=(Vote)votes.get(i);
总是出现错误,
org.apache.jasper.JasperException: An exception occurred processing JSP page /display.jsp at line 51

48:                     //  String teachername = rs.getString("teachername");
49:                       //int vote = rs.getInt("vote");
50:                 VoteDao vdao=new VoteDao();
51:                 ArrayList<Vote> votes=vdao.getAllVotes();
52:        &