初学者java入门,小韩留言本管理系统
参考了一些材料,花了不到一周时间把基本功能都实现了下面让分享我的设计作品。
留言管理系统的目的比较明确,就是提供留言的增加、删除、修改和显示的功能。初学者通过《小韩简易留言管理系统》这个小作品可以对项目开发有个全局的认识,并且通过这个作品可以对开发过程有个清晰的认识。
本系统运用了CSS+DIV技术布局,用DREAMWEARE编写代码和FIREWORK辅助处理图片,当然熟练的话也可以直接用文本方式写代码。
本系统采用JSP+ACCESS数据库设计,基本可以实现增加、删除、修改和显示的功能,如果需要修改增加功能可以自行研究。
系统运行效果图:
留言本的数据设计如图:
表1:message
表2:admin
具体的字段表示的含义可以根据英文翻译。
index.jsp
login.jsp
edit.jsp
add.jsp
about.jsp
- <%@ page contentType="text/html; charset=gbk" language="java" import="java.sql.*" %>
- <%
- String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+application.getRealPath("data/guestbook.mdb");
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection conn=DriverManager.getConnection(url);
- Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- Boolean a=(Boolean)session.getAttribute("a");
- %>
- <html><head>
- <title>简单留言本--航硕网络</title>
- <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
- <link href="css/classid.css" rel="stylesheet" type="text/css">
- <link href="css/classcss.css" rel="stylesheet" type="text/css">
- </head>
- <body>
- <div>
- <jsp:include page="head.jsp" />
- </div>
- <%
- String sql="select * from message order by messageid DESC";
- ResultSet rs=stmt.executeQuery(sql);
- //rs.absolute(1);
- int intPageSize; //一页显示的记录数
- int intRowCount; //记录总数
- int intPageCount; //总页数
- int intPage; //待显示页码
- String strPage;
- int i;
- //设置一页显示的记录数
- intPageSize = 5;
- strPage=request.getParameter("page");
- if(strPage==null){
- //表明QueryString中没有page这个参数,此时显示第一页数据
- intPage=1;
- }
- else{
- //将字符串转换成整型
- intPage=Integer.parseInt(strPage);
- if(intPage<1){
- intPage=1;
- }
- }
- //获取记录总数
- rs.last();
- intRowCount = rs.getRow();
- //out.print(intRowCount);
- //记算总页数
- intPageCount = (intRowCount+intPageSize-1) / intPageSize;
- //out.print(intPageCount);
- //调整待显示的页码
- if(intPage>intPageCount){
- intPage=intPageCount;
- }
- if(intPageCount>0){
- rs.absolute((intPage-1)*intPageSize+1);
- i = 0;
- while(i<intPageSize && !rs.isAfterLast()){ // intPageSize && !rs.isAfterLast()
- //读留言信息
- i++;
- int messageid=rs.getInt("messageid");
- String username=rs.getString("username");
- String content=rs.getString("content");
- String title=rs.getString("title");
- String sex=rs.getString("sex");
- String reptime=rs.getString("reptime");
- String userip=rs.getString("userip");
- String repcontext=rs.getString("repcontext");
- %>
- <div id="center">
- <div id="left" class="left" >
- <div style="margin-top:25px"><img src="image/sex<%=sex%>.jpg" height="120" width="120"></div>
- <div><%=username%></div>
- </div>
- <div id="right">
- <div id="right01"> </div>
- <div id="right02">
- <div align="right" style="margin:0px 18px 5px 18px;"><div class="title" style=" float:left;margin-left:8px; text-align:center;"><%=title%></div> 留言时间:<%=reptime%> IP:<%=userip%> <hr/></div>
- <div style="margin:0px 20px 50px 20px;float:left"><%=content%></div>
- <div style=" width:720px; text-align:left">
- <%if(repcontext!=null){%>
- <hr/>
- <strong>管理员回复:</strong><%=repcontext%>
- <%}
- else{
- }
- %>
- </div>
- <%
- if(a==null||a.booleanValue()==true){
- %><div style=" width:720px; "><hr/>
- <div style="float:right; width:120px;"><a href="del.jsp?id=<%=messageid%>" class="del">删除</a> <a href="repadd.jsp?id=<%=messageid%>" class="del">回复</a> <a href="alter.jsp?id=<%=messageid%>" class="del">编辑</a></div></div>
- <%
- }
- else{
- }
- %>
- </div>
- <div id="right03"> </div>
- </div>
- <%
- rs.next();
- //读流言信息结束
- }
- }%>
- <div id="yema" class="yema"><STRONG>
- 第<%=intPage%>页 共<%=intRowCount%>条记录/共<%=intPageCount%>页
- <%
- if(intPage<2){
- out.print("首页 | 上一页");
- }else{%>
- <a href="index.jsp?page=1">首页</a> | <a href="index.jsp?page=<%=intPage-1%>">上一页</a>
- <%}%>|
- <%
- if((intPageCount-intPage)<1)
- {
- out.print("下一页 | 尾页");
- }else{%>
- <a href="index.jsp?page=<%=intPage+1%>">下一页</a> | <a href="index.jsp?page=<%=intPageCount%>">尾页</a>
- <% }
- %>
- </STRONG></div>
- </div>
- <div>
- <jsp:include page="foot.jsp"></jsp:include></div>
- </body>
- </html><%
- if(conn!=null)
- {
- conn.close();
- }
- if(stmt!=null)
- {
- stmt.close();
- }
- %>
3.2 login.jsp
- <%@ page contentType="text/html; charset=gbk" language="java" import="java.sql.*" %>
- <html>
- <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
- <link href="css/classid.css" rel="stylesheet" type="text/css">
- <link href="css/classcss.css" rel="stylesheet" type="text/css">
- <script language="javascript">
- function checks(){
- if(loginpost.loginname.value==""){
- alert("请输入管理员帐号!");
- return false;
- }
- if(loginpost.loginname.value.length>20){
- alert("管理员帐号不超过20位!");
- return false
- }
- if(loginpost.loginpass.value==""){
- alert("请输入管理员密码!");
- return false;
- }
- if(loginpost.loginpass.value.length>20){
- alert("管理员密码不超过20位!");
- return false;
- }
- return true;
- }
- </script>
- <head>
- <title></title></head>
- <body style="text-align:center">
- <form name="loginpost" method="post" action="loginpost.jsp" onSubmit="return checks();">
- <div><jsp:include page="head.jsp"/></div>
- <div style="margin: auto;width:950px;">
- <div style="float:left; width:500px; height:300px; margin-top:10px; background:url(image/login.jpg)"> </div>
- <%
- Boolean a=(Boolean)session.getAttribute("a");
- if(a==null||a.booleanValue()==false)
- {
- %>
- <div style="float:left; width:340px; margin-left:40px; margin-top:10px; height:340px; background:url(image/loginbg.jpg)">
- <div style="margin:100px 40px 20px 50px;"><div id="login">用户名</div><input type="text" name="loginname" size="25" ></div>
- <div style="margin:35px 40px 20px 51px;"><div id="login2">密 码</div><input type="password" name="loginpass" size="27" ></div>
- <div style="margin-top:40px; margin-left:50px;">
- <div style="float:left; margin-right:1px;"><input type="submit" name="dl" value=" 登陆 "></div>
- <div style="float:left; "><input type="reset" name="cz" value=" 注册 "></div></div>
- </div>
- <%}else{
- %>
- <div style="float:left; width:340px; margin-left:40px; margin-top:10px; height:340px; background:url(image/loginbg.jpg)">
- <div style="margin-top:40px; margin-left:50px;">
- <div style="float:left; margin-top:120px; margin-left:42px;" class="loginin">您已经登陆了</div>
- </div>
- </div>
- <%}%>
- </div>
- <div><jsp:include page="foot.jsp"/></div>
- </body>
- </html>
其余页面代码相似。
数据库的连接:
- <%
- String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+application.getRealPath("data/guestbook.mdb");
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection conn=DriverManager.getConnection(url);
- Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- Boolean a=(Boolean)session.getAttribute("a");
- %>
数据处理:判断每页显示的条数
- <%
- String sql="select * from message order by messageid DESC";
- ResultSet rs=stmt.executeQuery(sql);
- //rs.absolute(1);
- int intPageSize; //一页显示的记录数
- int intRowCount; //记录总数
- int intPageCount; //总页数
- int intPage; //待显示页码
- String strPage;
- int i;
- //设置一页显示的记录数
- intPageSize = 5;
- strPage=request.getParameter("page");
- if(strPage==null){
- //表明QueryString中没有page这个参数,此时显示第一页数据
- intPage=1;
- }
- else{
- //将字符串转换成整型
- intPage=Integer.parseInt(strPage);
- if(intPage<1){
- intPage=1;
- }
- }
- //获取记录总数
- rs.last();
- intRowCount = rs.getRow();
- //out.print(intRowCount);
- //记算总页数
- intPageCount = (intRowCount+intPageSize-1) / intPageSize;
- //out.print(intPageCount);
- //调整待显示的页码
- if(intPage>intPageCount){
- intPage=intPageCount;
- }
- if(intPageCount>0){
- rs.absolute((intPage-1)*intPageSize+1);
- i = 0;
- while(i<intPageSize && !rs.isAfterLast()){ // intPageSize && !rs.isAfterLast()
- //读留言信息
- i++;
- int messageid=rs.getInt("messageid");
- String username=rs.getString("username");
- String content=rs.getString("content");
- String title=rs.getString("title");
- String sex=rs.getString("sex");
- String reptime=rs.getString("reptime");
- String userip=rs.getString("userip");
- String repcontext=rs.getString("repcontext");
- %>
页码的显示:
- <div id="yema" class="yema"><STRONG>
- 第<%=intPage%>页 共<%=intRowCount%>条记录/共<%=intPageCount%>页
- <%
- if(intPage<2){
- out.print("首页 | 上一页");
- }else{%>
- <a href="index.jsp?page=1">首页</a> | <a href="index.jsp?page=<%=intPage-1%>">上一页</a>
- <%}%>|
- <%
- if((intPageCount-intPage)<1)
- {
- out.print("下一页 | 尾页");
- }else{%>
- <a href="index.jsp?page=<%=intPage+1%>">下一页</a> | <a href="index.jsp?page=<%=intPageCount%>">尾页</a>
- <% }
- %>
- </STRONG></div>
- </div>
还有一些JAVA 函数大家可一查阅网上材料。
可能结构粗糙,但是对于初学者来说,这个可以很清楚的熟悉WEB开发和JSP项目的开发流程。摘自航硕网络。
欢迎多多给意见哈。