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
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
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- JSP/PHP基于Ajax的分页功能实现
- c语言实现hashmap(转载)
- 数据库链接字符串查询网站
- 开发阶段Jetty运行Jsp报错且响应空白
- DB2实例管理
- DB2实例管理
- 保障MySQL数据安全的14个最佳方法
- mysql问答汇集
- 第三章 数据库备份和还原
- 创建一个空的IBM DB2 ECO数据库的方法
- Access 2000 数据库 80 万记录通用快速分页类
- 开通一个数据库失败的原因的和解决办法
- 一个简单的asp数据库操作类
- CentOS下DB2数据库安装过程详解
- EasyASP v1.5发布(包含数据库操作类,原clsDbCtrl.asp)第1/2页
- sql2008 还原数据库解决方案