我没有多好的书可以给你建议,不过我觉得高级java2大学教程 电子机械工业出版社 还可以
然后以我的个人建议对你上述的想法提点建议:
其中,MVC模式,jsp来实现V 视图 servlet来完成C 逻辑处理 javabean来实现M 模型
也就是javabean仅把连接数据库,分页的基本操作封装起来,然后用jsp 和servlet来调用处理。你能明白我得意思吗?
然后以我的个人建议对你上述的想法提点建议:
其中,MVC模式,jsp来实现V 视图 servlet来完成C 逻辑处理 javabean来实现M 模型
也就是javabean仅把连接数据库,分页的基本操作封装起来,然后用jsp 和servlet来调用处理。你能明白我得意思吗?
这是我毕业设计里用过的一段代码,也是跟别人学的
删除了一些代码但不知有没有删错,不过应该好用。希望帮得上忙。
在你的javabean里加一个方法(与数据库连接的bean)
public int rowCount=0; //记录总数
public int pageCount=0; //总页数
public int pageSize=2; //每个页面显示的记录数
public String pageSql = ""; //保留最近一次的分页查询SQL命令
public ResultSet queryPage(String sql,int curPage){
String countSql; //取得记录总数的SQL语句。 把传递参数sql中“select”“from”之间的部分换成“count(*)”
pageSql = sql;
try{
if (rs != null)
rs.close();
if (con == null || con.isClosed())
getConnection();
if (stmt == null) {
stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
} //先取出这次查询的总记录数
countSql = "select count(*) " + sql.substring(sql.indexOf("from"));
rs = stmt.executeQuery(countSql);
rs.next();
rowCount = rs.getInt(1);
//计算出总的页数
pageCount = rowCount/pageSize + 1;
rs.close();
//查询出当前页的记录数
sql = sql+" and rownum<="+(curPage+1)*pageSize+" minus "+sql+" and rownum<"+(curPage*pageSize+1);
rs = stmt.executeQuery(sql); }catch(SQLException e){
System.err.println("查询操作时出现意外");
e.printStackTrace();
}
return rs;
}
然后用jsp页面调用这个javabean中的这个方法
---------jsp1------------------
<%@ page contentType="text/html; charset=GBK" language="java" %>
<%
String qAccount="",qName="";
%>
<html>
<head>
<title>
查看帐号
</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#fef4d9">
<center>
<h1><font color="red" face="华文新魏">查看帐号</font> </h1>
</center>
<!-- 输入查询条件 -->
<form name="query" method="post" action="<%=response.encodeRedirectURL("viewaccountok.jsp")%>">
<input type="hidden" name="confirmQuery" value="query">
<center>
<table border=1 cellPadding=1 cellSpacing=1 bgcolor="#12FFFF">
<tr>
<td> 帐号:</td>
<td> <input type="text" name="qAccount" size=12></td>
</tr>
<tr>
<td> 姓名:</td>
<td> <input type="text" name="qName" size=12></td>
</tr>
</table>
</center><br>
<center>
<input type="submit" name="Submit" value="确认">
</center>
</form>
</body>
</html>
---------jsp2---------
<%@ page contentType="text/html; charset=GBK" language="java" %>
<%@page errorPage="error.jsp" %>
<%@page import="java.sql.*"%>
<%request.setCharacterEncoding("GBK");%>
<jsp:useBean id="beanid" scope="session" class="myproject.database"/>
<%
String qAccount="",qName=""; //查询用的字段
String confirmQuery = ""; //查询提交确认标志
StringBuffer sql ; //查询语句
ResultSet rs = null;
int prevPage = 0,nextPage = 0,curPage = 0; //上一页、下一页,当前页
String tempPage="";
int total = 0; //先判断是不是提交给自己的请求
confirmQuery = request.getParameter("confirmQuery");
if (confirmQuery == null) confirmQuery = ""; //提交查询请求
if (confirmQuery.equals("query"))
{
qAccount=request.getParameter("qAccount");
qName=request.getParameter("qName");
if(qAccount==null) qAccount="";
if(qName==null) qName="";
sql=new StringBuffer("select * from T1Account where status!=2 ");
if (!qAccount.equals("")) sql.append(" and account='" + qAccount + "' " );
if (!qName.equals("")) sql.append(" and name like '%" + qName + "%' " );
//新查询时,从第一页开始显示
curPage = 0;
rs = beanid.queryPage(sql.toString(),curPage);
}
//首次进入该页面,或从其他页面重定向过来,或者点击“上一页”或“下一页”时,查询出要显示的记录
else
{
tempPage = request.getParameter("wantPage"); if (tempPage == null || tempPage.equals(""))
{
curPage = 0;
sql = new StringBuffer("select * from t1account where status!=2 ");
rs = beanid.queryPage(sql.toString(),curPage);
}
else
{
curPage = Integer.parseInt(tempPage);
rs = beanid.queryPage(beanid.pageSql,curPage);
}
}
}%>
<table width=600 border=0 cellspacing=2 cellpadding=0>
<tr>
<font color="blue">共有记录<font color="red"><%=beanid.rowCount%></font>条,本页显示<font color="red"><%=curPage*beanid.pageSize + 1%>~<%=(curPage*beanid.pageSize +2)%></font>条</font>
</tr>
<tr>
<td align=right width="100%">第<%=curPage+1%>页 共<%=beanid.pageCount%>页
<%
//curPage起始编号为0
if (curPage > 0 && curPage <= (beanid.pageCount -1)){
prevPage = curPage -1;
out.print("<a href=viewaccountok.jsp?wantPage=" + prevPage + ">上一页</a> ");
}
if (curPage >= 0 && curPage < (beanid.pageCount - 1) ){
nextPage = curPage + 1;
out.print("<a href=viewaccountok.jsp?wantPage=" + nextPage + ">下一页</a>");
}
%>
</td>
</tr>
</table> <table border=1 cellPadding=1 cellSpacing=1 bgcolor="#12FFFF" width=750>
<tr align="center">
<td width=100><font color="red">用户帐号</font></td>
<td width=100><font color="red">帐号类型</font></td>
<td width=50><font color="red">状态</font></td>
<td width=150><font color="red">注册时间</font></td>
<td width=100><font color="red">管理员</font></td>
<td width=100><font color="red">姓名</font></td>
<td width=50><font color="red">性别</font></td>
<td width=50> </td>
<td width=50> </td>
</tr>
<%
String account,type,piror,status,operator,regDate,name,sex;
while (rs.next()){
account = rs.getString("Account");
type = rs.getString("Type");
piror = rs.getString("Piror");
status = rs.getString("Status");
regDate = rs.getDate("Regdate").toString();
operator=rs.getString("Operator");
name = rs.getString("Name");
sex = rs.getString("Sex");
if (piror.equals("0")) piror = "普通";
else piror = "特殊"; if (status.equals("0")) status = "有效";
else status = "停用"; viewmodaccount = response.encodeRedirectURL("viewmodaccountok.jsp?account=" + account + "&isfromview=true");
viewdelaccount = response.encodeRedirectURL("viewdelaccountok.jsp?account=" + account + "&isfromview=true");
total++;
%>
<tr bgcolor="#12FFFF" align="center">
<td height=20><font color="blue"><%=account%></font></td>
<td height=20><font color="blue"><%=type%></font></td>
<td height=20><font color="blue"><%=status%></font></td>
<td height=20><font color="blue"><%=regDate%></font></td>
<td height=20><font color="blue"><%=operator%></font></td>
<td height=20><font color="blue"><%=name%></font></td>
<td height=20><font color="blue"><%=sex%></font></td>
<td height=20><a href=<%=viewmodaccount%>>修改</a></td>
<td height=20><a href=<%=viewdelaccount%>>删除</a></td>
</tr>
<%
}
if (rs != null) rs.close();
%> </table>
<form name="query" method="post" action="<%=response.encodeRedirectURL("viewaccountok.jsp")%>">
<input type="hidden" name="confirmQuery" value="query">
<center>
<table border=1 cellPadding=1 cellSpacing=1 bgcolor="#12FFFF"><form>怎么在table的外面呢,那怎样提交数据啊。它应该是在table内的吧。
请指点.
action="<%=response.encodeRedirectURL("viewaccountok.jsp")%>">
不是提交到2.jsp吗,这里的viewaccoutnok.jsp是什么??
<%
int liPageHeight = 650;
int liPages = 1;
int liRowHeight = 24;
int liHeight = 54;
%>
<html>
<body oncontextmenu="return false;" ondragstart="return false;" onmousewheel="return false;">
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" height="<%=liPageHeight%>" width="760" id="Page_<%=liPages%>">
<table width="700" border="0" cellspacing="0" cellpadding="0" align="center" height=50>
<tr>
<td class="td" height="48">
<div align="center">Table Information</div>
</td>
</tr>
<tr>
<td>
<table width="500" border="1" align="center" cellpadding="0" cellspacing="0" class="table_print_style_small">
<tr align="center" valign="top" bgcolor="#EDF4E9">
<td width="80" height="24" valign="middle"><div align="center">ID</div></td>
<td height="24" valign="middle"> <div align="center">Name</div></td>
</tr>
<%
String lsTrID = "";
for(int i = 0;i<loGroupID.length;i++) {
String lsGroupId = (String)loGroupID[i];
String lsGroupName = m_oInfo.m_oGroupInfo.getGroupName(lsGroupId.trim()).trim();
liHeight += liRowHeight;
if (i % 24 ==0 && i!=0){
liPages++;
liHeight = 6;
lsTrID = "style=\"page-break-before : always;\" id=Page_" + liPages;
%>
<tr><td colspan="2" height="216" style="border-bottom-style :none;" class="table_print_bk_color"></td></tr>
<tr <%=lsTrID%>><td colspan="2" style="border-top-style :none;" height="72" class="table_print_bk_color"></td></tr>
<%
} else {
lsTrID = "";
}
%>
<tr>
<td width="80" height="24" align="center"><%=lsGroupId%></td>
<td height="24" align="left" ><%=Convert.convertHtmlStr(lsGroupName)%></td>
</tr>
<%
}
%>
</table>
</td>
</tr>
<%if(loGroupID.length % 24!=0){
for(int j=0;j<(27-loGroupID.length % 24);j++){%>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<%}
}%>
</table>
</td>
</tr>
</table>
<SCRIPT LANGUAGE="JavaScript">
<!--
parent.PageCount = <%=liPages%>;
parent.showPage(0);
//-->
</SCRIPT>
</body>
</html>
有介绍这方面的书推荐吗
<form>在外面是因为我在这个form里定义了一个隐藏域,在这里
//先判断是不是提交给自己的请求
confirmQuery = request.getParameter("confirmQuery");
if (confirmQuery == null) confirmQuery = ""; //提交查询请求
if (confirmQuery.equals("query"))
如果是form窗体query,它就执行查询。其实这两个页面可以为一个,我是为了别的问题才分开它们。你把它们放在一起就看明白了。这里我也一直没优化过。可以更简单一点。