您的位置:首页 > 数据库

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

2016-05-31 11:10 513 查看
环境: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息