关闭

jsp从数据库取数据到页面上

标签: jsp数据库ListHashMapforEach
2324人阅读 评论(0) 收藏 举报
分类:

环境:eclipse Mars Release (4.5.0)

java代码:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        
        List listProjectName = new ArrayList();
        String toJSP="";
        String userid=request.getParameter("userid"); 
        String sql="select * from project where createid="+userid;  
        JdbcBean jdbc=new JdbcBean();  
        try{  
            ResultSet rs=jdbc.query(sql);
            ResultSetMetaData md = rs.getMetaData();
            while(rs.next())
            {
            	Map rowData = new HashMap();
            	for (int i = 1; i < md.getColumnCount(); i++) {
            		rowData.put(md.getColumnName(i), rs.getObject(i));
            	}
            	listProjectName.add(rowData);
            }
            rs.close();
            
            request.setAttribute("listProjectName", listProjectName);
            toJSP = "myproject.jsp";
        }  
        catch(Exception e){  
            System.out.println("发生查询异常");  
        }
        RequestDispatcher rd=request.getRequestDispatcher(toJSP);  
        rd.forward(request, response);  
    }

jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <c:if test="${not empty listProjectName}">
        <c:forEach items="${listProjectName}" var="row">
            ${row['ID']}------${row['PROJECTNAME']}<br/>
        </c:forEach>
    </c:if>
</body>
</html>

jsp里面查询hashmap字段时居然是大小写敏感的,平时写hml/xml写多了,在这里卡了半天

参考:

http://www.cnblogs.com/frostbelt/archive/2010/06/24/1764140.html

http://q.cnblogs.com/q/54918/


http://bbs.csdn.net/topics/390373573

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:883631次
    • 积分:10150
    • 等级:
    • 排名:第1668名
    • 原创:352篇
    • 转载:220篇
    • 译文:0篇
    • 评论:50条
    最新评论