jsp+servlet+javabean怎么显示数据库里面的数据 本人刚刚学习 希望高手多指教指教 谢谢
createtablebbs_user(uidintauto_increment,unamevarchar(32),upwdvarchar(32),usexvarchar...
create table bbs_user
(
uid int auto_increment,
uname varchar(32),
upwd varchar(32),
usex varchar(10),
uemail varchar(40),
primary key (uid)
);
这是一张表 有对应的UserBean 想问一下 怎么在页面上显示数据库里面的内容
就是上面一行是表头 下面几行对应数据库里面相应的字段的值 最好有代码 本人初学 需要直观一点 讲述的可能看起来很费劲 谢谢
回复一楼的 我知道在JSP里面怎么写 我想要的是用JavaBean和servlet再加JSP的 不过还是谢谢了 展开
(
uid int auto_increment,
uname varchar(32),
upwd varchar(32),
usex varchar(10),
uemail varchar(40),
primary key (uid)
);
这是一张表 有对应的UserBean 想问一下 怎么在页面上显示数据库里面的内容
就是上面一行是表头 下面几行对应数据库里面相应的字段的值 最好有代码 本人初学 需要直观一点 讲述的可能看起来很费劲 谢谢
回复一楼的 我知道在JSP里面怎么写 我想要的是用JavaBean和servlet再加JSP的 不过还是谢谢了 展开
5个回答
展开全部
我这里用的是JSP+JavaBean+Servlet+MSSQLServer2000实现分页查询。希望能够办到你!
====================================================================
数据库操作Bean:DBManage.java
package com.student.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBManage
{
public Connection getCon()throws ClassNotFoundException,SQLException{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:sun","sa","");
return con;
}
public Statement getSt()throws ClassNotFoundException,SQLException{
Connection con = this.getCon();
Statement st = con.createStatement(1004,1007);
return st;
}
public ResultSet getResult(String sql)throws ClassNotFoundException,SQLException{
Statement st = this.getSt();
ResultSet rs = st.executeQuery(sql);
return rs;
}
public int getUpdate(String sql)throws ClassNotFoundException,SQLException{
Statement st = this.getSt();
int result = st.executeUpdate(sql);
return result;
}
}
分页Bean:PageBean
package com.common;
public class PageBean
{
public int getPageCount(int totalCount,int pageSize)
{
int pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
return pageCount;
}
public int getCurrentPage(String sp,int pageCount)
{
int currentPage = 1;
if(sp!=null)
{
try{
currentPage = Integer.parseInt(sp);
}catch(NumberFormatException nfe){
currentPage = 1;
}
}
if(currentPage < 1)
{
currentPage = 1;
}
if(currentPage > pageCount)
{
currentPage = pageCount;
}
return currentPage;
}
}
服务类:StudentService.java
package com.student.stu.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import com.student.db.DBManage;
import com.student.vo.StuVO;
public class StudentService
{
com.student.db.DBManage dbm = new com.student.db.DBManage();
public int getTotalCount()throws ClassNotFoundException,SQLException
{
int totalCount = 0;
String sql = "select count(*) from stu";
ResultSet rs = dbm.getResult(sql);
boolean b = rs.next();
if(b)
{
totalCount = rs.getInt(1);
}
return totalCount;
}
public List getStudentList(int currentPage,int pageSize)throws ClassNotFoundException,SQLException
{
List<StuVO> stuList = new ArrayList<StuVO>();
String sql = "select * from stu";
ResultSet rs = dbm.getResult(sql);
int position = (currentPage-1)*pageSize+1;
rs.absolute(position);
for(int i = 0 ; i < pageSize ; i ++)
{
if(rs.isAfterLast()==false)
{
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
int age = rs.getInt(4);
StuVO stu = new StuVO();
stu.setId(id);
stu.setName(name);
stu.setSex(sex);
stu.setAge(age);
stuList.add(stu);
rs.next();
}
}
return stuList;
}
}
数据表对应的实体类VO:StuVO.java
package com.student.vo;
public class StuVO
{
int id;
String name;
String sex;
int age;
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return this.id;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setSex(String sex)
{
this.sex = sex;
}
public String getSex()
{
return this.sex;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
}
查询的Servlet:StudentServlet.java
package com.student.servlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
import com.student.stu.service.StudentService;
import com.common.PageBean;
import java.util.List;
import java.sql.SQLException;
public class StudentServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
int totalCount = 0;
int pageSize = 2;
int pageCount = 0;
int currentPage = 1;
try{
//学生信息管理服务类:特定业务类->StudentService
StudentService ss = new StudentService();
totalCount = ss.getTotalCount();//1.获取总记录数
if(totalCount>0)
{
//通用的分页组件:PageBean
PageBean pb = new PageBean();
pageCount = pb.getPageCount(totalCount,pageSize);//1.计算总页数
String sp2 = request.getParameter("sp");
currentPage= pb.getCurrentPage(sp2,pageCount);//2.确定当前页的范围,返回当前页
List stuList = ss.getStudentList(currentPage,pageSize);//2.获取当前页的集合
//新增的内容:
request.setAttribute("st",stuList);
request.setAttribute("currentPage",String.valueOf(currentPage));
request.setAttribute("pageCount",String.valueOf(pageCount));
}
}catch(ClassNotFoundException cnfe){
System.out.print(cnfe);
}catch(SQLException se){
System.out.print(se);
}
//将集合派发至JSP页面进行显示
RequestDispatcher rd = request.getRequestDispatcher("studentIndex.jsp");
rd.forward(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
this.doGet(request,response);
}
}
JSP显示页面:studentIndex.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import="java.util.List"%>
<%@ page import="com.student.vo.StuVO"%>
<html>
<head>
<title>JSP+JavaBean+Servlet分页</title>
</head>
<body>
<table align="center" cellpadding="3" cellspacing="1" bgcolor="#0099FF" width="700">
<%
Object obj = request.getAttribute("st");//获取在Servlet中保存的信息
if(obj!=null)
{
Object cp = request.getAttribute("currentPage");
Object pc = request.getAttribute("pageCount");
int currentPage = Integer.parseInt((String)cp);
int pageCount = Integer.parseInt((String)pc);
List stuList = (List)obj;
for(int i = 0 ; i < stuList.size() ; i ++)
{
Object o = stuList.get(i);
StuVO stu = (StuVO)o;
%>
<tr bgcolor="#FFFFFF">
<td><%=stu.getId()%></td>
<td><%=stu.getName()%></td>
<td><%=stu.getSex()%></td>
<td><%=stu.getAge()%></td>
</tr>
<%
}
%>
<tr bgcolor="#FFFFFF">
<td colspan="4">
[<a href="?sp=1">首页</a>]
[<a href="?sp=<%=currentPage-1%>">前页</a>]
[<a href="?sp=<%=currentPage+1%>">后页</a>]
[<a href="?sp=<%=pageCount%>">尾页</a>]
</td>
</tr>
<%
}
else
{
%>
<tr bgcolor="#FFFFFF">
<td colspan="4">暂无信息</td>
</tr>
<%
}
%>
</table>
</body>
</html>
====================================================================
数据库操作Bean:DBManage.java
package com.student.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBManage
{
public Connection getCon()throws ClassNotFoundException,SQLException{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:sun","sa","");
return con;
}
public Statement getSt()throws ClassNotFoundException,SQLException{
Connection con = this.getCon();
Statement st = con.createStatement(1004,1007);
return st;
}
public ResultSet getResult(String sql)throws ClassNotFoundException,SQLException{
Statement st = this.getSt();
ResultSet rs = st.executeQuery(sql);
return rs;
}
public int getUpdate(String sql)throws ClassNotFoundException,SQLException{
Statement st = this.getSt();
int result = st.executeUpdate(sql);
return result;
}
}
分页Bean:PageBean
package com.common;
public class PageBean
{
public int getPageCount(int totalCount,int pageSize)
{
int pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
return pageCount;
}
public int getCurrentPage(String sp,int pageCount)
{
int currentPage = 1;
if(sp!=null)
{
try{
currentPage = Integer.parseInt(sp);
}catch(NumberFormatException nfe){
currentPage = 1;
}
}
if(currentPage < 1)
{
currentPage = 1;
}
if(currentPage > pageCount)
{
currentPage = pageCount;
}
return currentPage;
}
}
服务类:StudentService.java
package com.student.stu.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import com.student.db.DBManage;
import com.student.vo.StuVO;
public class StudentService
{
com.student.db.DBManage dbm = new com.student.db.DBManage();
public int getTotalCount()throws ClassNotFoundException,SQLException
{
int totalCount = 0;
String sql = "select count(*) from stu";
ResultSet rs = dbm.getResult(sql);
boolean b = rs.next();
if(b)
{
totalCount = rs.getInt(1);
}
return totalCount;
}
public List getStudentList(int currentPage,int pageSize)throws ClassNotFoundException,SQLException
{
List<StuVO> stuList = new ArrayList<StuVO>();
String sql = "select * from stu";
ResultSet rs = dbm.getResult(sql);
int position = (currentPage-1)*pageSize+1;
rs.absolute(position);
for(int i = 0 ; i < pageSize ; i ++)
{
if(rs.isAfterLast()==false)
{
int id = rs.getInt(1);
String name = rs.getString(2);
String sex = rs.getString(3);
int age = rs.getInt(4);
StuVO stu = new StuVO();
stu.setId(id);
stu.setName(name);
stu.setSex(sex);
stu.setAge(age);
stuList.add(stu);
rs.next();
}
}
return stuList;
}
}
数据表对应的实体类VO:StuVO.java
package com.student.vo;
public class StuVO
{
int id;
String name;
String sex;
int age;
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return this.id;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setSex(String sex)
{
this.sex = sex;
}
public String getSex()
{
return this.sex;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
}
查询的Servlet:StudentServlet.java
package com.student.servlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
import com.student.stu.service.StudentService;
import com.common.PageBean;
import java.util.List;
import java.sql.SQLException;
public class StudentServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
int totalCount = 0;
int pageSize = 2;
int pageCount = 0;
int currentPage = 1;
try{
//学生信息管理服务类:特定业务类->StudentService
StudentService ss = new StudentService();
totalCount = ss.getTotalCount();//1.获取总记录数
if(totalCount>0)
{
//通用的分页组件:PageBean
PageBean pb = new PageBean();
pageCount = pb.getPageCount(totalCount,pageSize);//1.计算总页数
String sp2 = request.getParameter("sp");
currentPage= pb.getCurrentPage(sp2,pageCount);//2.确定当前页的范围,返回当前页
List stuList = ss.getStudentList(currentPage,pageSize);//2.获取当前页的集合
//新增的内容:
request.setAttribute("st",stuList);
request.setAttribute("currentPage",String.valueOf(currentPage));
request.setAttribute("pageCount",String.valueOf(pageCount));
}
}catch(ClassNotFoundException cnfe){
System.out.print(cnfe);
}catch(SQLException se){
System.out.print(se);
}
//将集合派发至JSP页面进行显示
RequestDispatcher rd = request.getRequestDispatcher("studentIndex.jsp");
rd.forward(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
this.doGet(request,response);
}
}
JSP显示页面:studentIndex.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import="java.util.List"%>
<%@ page import="com.student.vo.StuVO"%>
<html>
<head>
<title>JSP+JavaBean+Servlet分页</title>
</head>
<body>
<table align="center" cellpadding="3" cellspacing="1" bgcolor="#0099FF" width="700">
<%
Object obj = request.getAttribute("st");//获取在Servlet中保存的信息
if(obj!=null)
{
Object cp = request.getAttribute("currentPage");
Object pc = request.getAttribute("pageCount");
int currentPage = Integer.parseInt((String)cp);
int pageCount = Integer.parseInt((String)pc);
List stuList = (List)obj;
for(int i = 0 ; i < stuList.size() ; i ++)
{
Object o = stuList.get(i);
StuVO stu = (StuVO)o;
%>
<tr bgcolor="#FFFFFF">
<td><%=stu.getId()%></td>
<td><%=stu.getName()%></td>
<td><%=stu.getSex()%></td>
<td><%=stu.getAge()%></td>
</tr>
<%
}
%>
<tr bgcolor="#FFFFFF">
<td colspan="4">
[<a href="?sp=1">首页</a>]
[<a href="?sp=<%=currentPage-1%>">前页</a>]
[<a href="?sp=<%=currentPage+1%>">后页</a>]
[<a href="?sp=<%=pageCount%>">尾页</a>]
</td>
</tr>
<%
}
else
{
%>
<tr bgcolor="#FFFFFF">
<td colspan="4">暂无信息</td>
</tr>
<%
}
%>
</table>
</body>
</html>
参考资料: 研发经验
展开全部
我今晚也刚刚在做这个,做出来了!不过没有用到javabean,希望能帮得到你吧!!!呵呵
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>按姓名查找的学生信息</title>
</head>
<body>
<%
String agemin=request.getParameter("agemin");
if(agemin==null)
{
agemin="0";
}
String agemax=request.getParameter("agemax");
if(agemax==null)
{
agemax="100";
}
Connection con;
Statement sql;
ResultSet rs;
try
{//建立JDBC-ODBC桥驱动程序,用到java.lang包中的类Class,调用其方法forName().
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try
{//创建连接对象con,它属于Connection类,然后用类DriverManager调用getConnection().
con=DriverManager.getConnection("jdbc:odbc:stu");
sql=con.createStatement();//创建SQL语句对象.
rs=sql.executeQuery("SELECT* FROM stuInfo WHERE stuAge<="+agemax+" AND stuAge>="+agemin+"");;//执行SQL语句并返回结果.
%>
<center>
<h2>按年龄查找的学生信息</h2>
<br />
<br />
<h4>输出年龄在<%=agemin%>与<%=agemax%>之间的学生信息:</h4>
<table border="1" bgcolor="#cccccc"><!--设置用以表格显示.-->
<tr>
<th width="50">学号</th>
<th width="50">姓名</th>
<th width="50">性别</th>
<th width="50">年龄</th>
<th width="50">系别</th>
</tr>
<%
while(rs.next())//顺序取出结果集中的数据.
{%>
<tr>
<td><%=rs.getString(1)%></td> <!---取出表中第一个字段-->
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getInt(4)%></td>
<td><%=rs.getString(5)%></td>
</tr>
<%}%>
</table>
</center>
<%
con.close();
}
catch(SQLException e1){}
%>
</body>
</html>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>按姓名查找的学生信息</title>
</head>
<body>
<%
String agemin=request.getParameter("agemin");
if(agemin==null)
{
agemin="0";
}
String agemax=request.getParameter("agemax");
if(agemax==null)
{
agemax="100";
}
Connection con;
Statement sql;
ResultSet rs;
try
{//建立JDBC-ODBC桥驱动程序,用到java.lang包中的类Class,调用其方法forName().
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try
{//创建连接对象con,它属于Connection类,然后用类DriverManager调用getConnection().
con=DriverManager.getConnection("jdbc:odbc:stu");
sql=con.createStatement();//创建SQL语句对象.
rs=sql.executeQuery("SELECT* FROM stuInfo WHERE stuAge<="+agemax+" AND stuAge>="+agemin+"");;//执行SQL语句并返回结果.
%>
<center>
<h2>按年龄查找的学生信息</h2>
<br />
<br />
<h4>输出年龄在<%=agemin%>与<%=agemax%>之间的学生信息:</h4>
<table border="1" bgcolor="#cccccc"><!--设置用以表格显示.-->
<tr>
<th width="50">学号</th>
<th width="50">姓名</th>
<th width="50">性别</th>
<th width="50">年龄</th>
<th width="50">系别</th>
</tr>
<%
while(rs.next())//顺序取出结果集中的数据.
{%>
<tr>
<td><%=rs.getString(1)%></td> <!---取出表中第一个字段-->
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getInt(4)%></td>
<td><%=rs.getString(5)%></td>
</tr>
<%}%>
</table>
</center>
<%
con.close();
}
catch(SQLException e1){}
%>
</body>
</html>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在servlet里new UserBean;
UserBean() userbean=new UserBean();
ArrayLIst<UserBean> list=new ArrayList()<UserBean>;
再用userbean的get方法取出数据放到list里(userbean.getuid();)
然后跳转到jsp要带着list跳转
jsp中用el表达式输出(<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>)
<c:forEach items="${list}" var="aa">
<c:out value="${aa}"></c:out><br>
</c:forEach>
UserBean() userbean=new UserBean();
ArrayLIst<UserBean> list=new ArrayList()<UserBean>;
再用userbean的get方法取出数据放到list里(userbean.getuid();)
然后跳转到jsp要带着list跳转
jsp中用el表达式输出(<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>)
<c:forEach items="${list}" var="aa">
<c:out value="${aa}"></c:out><br>
</c:forEach>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在 servlet 里创建UserBean对象并且将查询的数据填充到该对象的属性,
UserBean user=new UserBean();
user.setUname="你好";//你好应该是从数据库获得的
session.setAttribute("userTemp",user);//userTemp随便写
这里是jsp页面
<input value="${user.uname}" type="text" />//${user.uname}" 这里的uname是UserBean 的getUname属性的Uname,记得要小写uname,
UserBean user=new UserBean();
user.setUname="你好";//你好应该是从数据库获得的
session.setAttribute("userTemp",user);//userTemp随便写
这里是jsp页面
<input value="${user.uname}" type="text" />//${user.uname}" 这里的uname是UserBean 的getUname属性的Uname,记得要小写uname,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
进入我们的网页设计群把,我们群讲这个的额
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |