初學者java入門,小韓留言本管理系統
參考了一些材料,花了不到一週時間把基本功能都實現了下面讓分享我的設計作品。
1.系統概述(參考)
留言管理系統的目的比較明確,就是提供留言的增加、刪除、修改和顯示的功能。初學者通過《小韓簡易留言管理系統》這個小作品可以對項目開發有個全局的認識,並且通過這個作品可以對開發過程有個清晰的認識。
本系統運用了CSS+DIV技術佈局,用DREAMWEARE編寫代碼和FIREWORK輔助處理圖片,當然熟練的話也可以直接用文本方式寫代碼。
本系統採用JSP+ACCESS數據庫設計,基本可以實現增加、刪除、修改和顯示的功能,如果需要修改增加功能可以自行研究。
系統運行效果圖:
2.數據庫設計
留言本的數據設計如圖:
表1:message
表2:admin
具體的字段表示的含義可以根據英文翻譯。
3.主要頁面代碼的實現
index.jsp
login.jsp
edit.jsp
add.jsp
about.jsp
3.1 index.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>
其餘頁面代碼相似。
4.日誌處理分析
數據庫的連接:
- <%
- 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項目的開發流程。摘自航碩網絡。
歡迎多多給意見哈。