使用servlet查询数据库,得不到数据库的数据,我是用断点测试,直接执行request.getRequestDispatcher("/second.jsp").forward(request,response);语句,我用request.setAttribute("list",list);保存数据,在jsp文件用 List list = (List)request.getAttribute("list");时,等到list为null
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:49680;DatabaseName=student", "sa", "123456");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from dbo.scores");
List<Stu> list = new ArrayList<Stu>();
while (resultSet.next()) {
Stu stu = new Stu();
String sno = resultSet.getString("学号");
String sname = resultSet.getString("姓名");
String sex = resultSet.getString("性别");
int grade = resultSet.getInt("得分");
stu.setSno(sno);
stu.setSname(sname);
stu.setSex(sex);
stu.setGrade(grade);
list.add(stu);
}
request.setAttribute("list",list);
PrintWriter out = response.getWriter();
out.println("ajiaaaaa");
int i =1;
resultSet.close();
statement.close();
connection.close();
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
}
request.getRequestDispatcher("/second.jsp").forward(request,response);
}