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

环境: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

阅读更多
个人分类: jsp
想对作者说点什么? 我来说一句

jsp直接读取mysql数据库内容

u013076044 u013076044

2015-08-24 15:48:56

阅读数:2544

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭