2019獨角獸企業重金招聘Python工程師標準>>>
案例-jsp+DAO實現留言管理程序
----------------Note.java------------------------
package?org.sky.darkness.note.vo?;public?class?Note?{private?int?id?;private?String?title?;private?String?author?;private?String?content?;public?void?setId(int?id)?{this.id?=?id?;}public?void?setTitle(String?title)?{this.title?=?title?;}public?void?setAuthor(String?author)?{this.author?=?author?;}public?void?setContent(String?content)?{this.content?=?content?;}public?int?getId()?{return?this.id?;}public?String?getTitle()?{return?this.title?;}public?String?getAuthor()?{return?this.author?;}public?String?getContent()?{return?this.content?;}
};
----------------------Person.java--------------------------
package?org.sky.darkness.note.vo?;public?class?Person
{private?String?id?;private?String?name?;private?String?password?;public?void?setId(String?id)?{this.id?=?id?;}public?void?setName(String?name)?{this.name?=?name?;}public?void?setPassword(String?password)?{this.password?=?password?;}public?String?getId()?{return?this.id?;}public?String?getName()?{return?this.name?;}public?String?getPassword()?{return?this.password?;}
};
----------------NoteDAO.java-------------
package?org.sky.darkness.note.dao?;import?java.util.*?;
import?org.sky.darkness.note.vo.*?;public?interface?NoteDAO
{//?增加操作public?void?insert(Note?note)?throws?Exception?;//?修改操作public?void?update(Note?note)?throws?Exception?;//?刪除操作public?void?delete(int?id)?throws?Exception?;//?按ID查詢,主要為更新使用public?Note?queryById(int?id)?throws?Exception?;//?查詢全部public?List?queryAll()?throws?Exception?;//?模糊查詢public?List?queryByLike(String?cond)?throws?Exception?;
};
----------------------------PersonDAO.java-----------------------------------
package?org.sky.darkness.note.dao?;import?org.sky.darkness.note.vo.*?;public?interface?PersonDAO?{//?做登陸驗證public?boolean?login(Person?person)?throws?Exception?;
};
----------------?PersonDAOImpl.java--------------
package?org.sky.darkness.note.dao.impl?;import?java.sql.*?;
import?org.sky.darkness.note.vo.*?;
import?org.sky.darkness.note.dbc.*?;
import?org.sky.darkness.note.dao.*?;public?class?PersonDAOImpl?implements?PersonDAO
{/*功能:??判斷是否是正確的用戶名或密碼??從數據庫中取出用戶的真實姓名*/public?boolean?login(Person?person)?throws?Exception?{boolean?flag?=?false?;String?sql?=?"SELECT?name?FROM?person?WHERE?id=??and?password=?"?;PreparedStatement?pstmt?=?null?;DataBaseConnection?dbc?=?null?;dbc?=?new?DataBaseConnection()?;try?{pstmt?=?dbc.getConnection().prepareStatement(sql)?;pstmt.setString(1,person.getId())?;pstmt.setString(2,person.getPassword())?;ResultSet?rs?=?pstmt.executeQuery()?;if?(rs.next())?{flag?=?true?;person.setName(rs.getString(1))?;}rs.close()?;pstmt.close()?;}?catch?(Exception?e)?{throw?new?Exception("操作出現錯誤!!!")?;}?finally?{dbc.close()?;}return?flag?;}
};
------------- NoteDAOImpl.java-------------------------------------
package?org.sky.darkness.note.dao.impl?;import?java.sql.*?;
import?java.util.*?;
import?org.sky.darkness.note.vo.*?;
import?org.sky.darkness.note.dao.*?;
import?org.sky.darkness.note.dbc.*?;public?class?NoteDAOImpl?implements?NoteDAO?{//?增加操作public?void?insert(Note?note)?throws?Exception?{String?sql?=?"INSERT?INTO?note(id,title,author,content)?VALUES(note_sequ.nextVal,?,?,?)"?;PreparedStatement?pstmt?=?null?;DataBaseConnection?dbc?=?null?;dbc?=?new?DataBaseConnection()?;try?{pstmt?=?dbc.getConnection().prepareStatement(sql)?;pstmt.setString(1,note.getTitle())?;pstmt.setString(2,note.getAuthor())?;pstmt.setString(3,note.getContent())?;pstmt.executeUpdate()?;pstmt.close()?;}?catch?(Exception?e)?{//?System.out.println(e)?;throw?new?Exception("操作中出現錯誤!!!")?;}?finally?{dbc.close()?;}}//?修改操作public?void?update(Note?note)?throws?Exception?{String?sql?=?"UPDATE?note?SET?title=?,author=?,content=??WHERE?id=?"?;PreparedStatement?pstmt?=?null?;DataBaseConnection?dbc?=?null?;dbc?=?new?DataBaseConnection()?;try?{pstmt?=?dbc.getConnection().prepareStatement(sql)?;pstmt.setString(1,note.getTitle())?;pstmt.setString(2,note.getAuthor())?;pstmt.setString(3,note.getContent())?;pstmt.setInt(4,note.getId())?;pstmt.executeUpdate()?;pstmt.close()?;}?catch?(Exception?e)?{throw?new?Exception("操作中出現錯誤!!!")?;}?finally?{dbc.close()?;}}//?刪除操作public?void?delete(int?id)?throws?Exception?{String?sql?=?"DELETE?FROM?note?WHERE?id=?"?;PreparedStatement?pstmt?=?null?;DataBaseConnection?dbc?=?null?;dbc?=?new?DataBaseConnection()?;try?{pstmt?=?dbc.getConnection().prepareStatement(sql)?;pstmt.setInt(1,id)?;pstmt.executeUpdate()?;pstmt.close()?;}?catch?(Exception?e)?{throw?new?Exception("操作中出現錯誤!!!")?;}?finally?{dbc.close()?;}}//?按ID查詢,主要為更新使用public?Note?queryById(int?id)?throws?Exception?{Note?note?=?null?;String?sql?=?"SELECT?id,title,author,content?FROM?note?WHERE?id=?"?;PreparedStatement?pstmt?=?null?;DataBaseConnection?dbc?=?null?;dbc?=?new?DataBaseConnection()?;try?{pstmt?=?dbc.getConnection().prepareStatement(sql)?;pstmt.setInt(1,id)?;ResultSet?rs?=?pstmt.executeQuery()?;if(rs.next())?{note?=?new?Note()?;note.setId(rs.getInt(1))?;note.setTitle(rs.getString(2))?;note.setAuthor(rs.getString(3))?;note.setContent(rs.getString(4))?;}rs.close()?;pstmt.close()?;}?catch?(Exception?e)?{throw?new?Exception("操作中出現錯誤!!!")?;}?finally?{dbc.close()?;}return?note?;}//?查詢全部public?List?queryAll()?throws?Exception?{List?all?=?new?ArrayList()?;String?sql?=?"SELECT?id,title,author,content?FROM?note"?;PreparedStatement?pstmt?=?null?;DataBaseConnection?dbc?=?null?;dbc?=?new?DataBaseConnection()?;try?{pstmt?=?dbc.getConnection().prepareStatement(sql)?;ResultSet?rs?=?pstmt.executeQuery()?;while(rs.next())?{Note?note?=?new?Note()?;note.setId(rs.getInt(1))?;note.setTitle(rs.getString(2))?;note.setAuthor(rs.getString(3))?;note.setContent(rs.getString(4))?;all.add(note)?;}rs.close()?;pstmt.close()?;}?catch?(Exception?e)?{System.out.println(e)?;throw?new?Exception("操作中出現錯誤!!!")?;}?finally?{dbc.close()?;}return?all?;}//?模糊查詢public?List?queryByLike(String?cond)?throws?Exception?{List?all?=?new?ArrayList()?;String?sql?=?"SELECT?id,title,author,content?FROM?note?WHERE?title?LIKE???or?AUTHOR?LIKE???or?CONTENT?LIKE??"?;PreparedStatement?pstmt?=?null?;DataBaseConnection?dbc?=?null?;dbc?=?new?DataBaseConnection()?;try?{pstmt?=?dbc.getConnection().prepareStatement(sql)?;pstmt.setString(1,"%"+cond+"%")?;pstmt.setString(2,"%"+cond+"%")?;pstmt.setString(3,"%"+cond+"%")?;ResultSet?rs?=?pstmt.executeQuery()?;while(rs.next())?{Note?note?=?new?Note()?;note.setId(rs.getInt(1))?;note.setTitle(rs.getString(2))?;note.setAuthor(rs.getString(3))?;note.setContent(rs.getString(4))?;all.add(note)?;}rs.close()?;pstmt.close()?;}?catch?(Exception?e)?{System.out.println(e)?;throw?new?Exception("操作中出現錯誤!!!")?;}?finally?{dbc.close()?;}return?all?;}
};
----------- DAOFactory .java------------------------------------------------
package?org.sky.darkness.note.factory?;import?org.sky.darkness.note.dao.*?;
import?org.sky.darkness.note.dao.impl.*?;public?class?DAOFactory?{public?static?PersonDAO?getPersonDAOInstance()?{return?new?PersonDAOImpl()?;}public?static?NoteDAO?getNoteDAOInstance()?{return?new?NoteDAOImpl()?;}
};
----------- DataBaseConnection .java-----------------------------------------------
package?org.sky.darkness.note.dbc?;import?java.sql.*?;public?class?DataBaseConnection?{private?String?DBDRIVER =?"oracle.jdbc.driver.OracleDriver"?;private?String?DBURL =?"jdbc:oracle:thin:@localhost:1521:sky"?;private?String?DBUSER =?"scott"?;private?String?DBPASSWORD =?"darkness"?;private?Connection?conn =?null?;public?DataBaseConnection()?{try?{Class.forName(DBDRIVER)?;this.conn?=?DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD)?;}?catch?(Exception?e)?{}}public?Connection?getConnection()?{return?this.conn?;}public?void?close()?{try?{this.conn.close()?;}catch?(Exception?e)?{}}
};
------------腳本.sql-------------------------------------------------
--?創建表
--?用戶表(登陸)、留言表--?刪除表
DROP?TABLE?person?;
DROP?TABLE?note?;--?刪除序列
DROP?SEQUENCE?note_sequ?;--?創建序列
CREATE?SEQUENCE?note_sequ?;--?創建person表
CREATE?TABLE?person
(id?varchar(20)?not?null?primary?key?,name?varchar(20)?,password?varchar(20)
)?;--?創建留言表
CREATE?TABLE?note
(id?int?not?null?primary?key?,?--?sequencetitle?varchar(20)?not?null?,author?varchar(20)?not?null?,content?varchar(50)?not?null?
)?;--?插入測試數據
INSERT?INTO?person?VALUES?('darkness','wind','zzzzzz')?;
INSERT?INTO?person?VALUES?('sky','cloud','mmmmmm')?;--?事務提交
commit?;
-------------------------login.jsp--------------------------------------------<%@?page?contentType="text/html;charset=gb2312"%>
<html>
<head><title>JSP+DAO?留言管理程序——登陸</title>
</head>
<body>
<center><h1>留言管理范例?——?JSP?+?DAO實現</h1><hr><br><%//?判斷是否有錯誤信息,如果有則打印//?如果沒有此段代碼,則顯示時會直接打印nullif(request.getAttribute("err")!=null){%><h2><%=request.getAttribute("err")%></h2><%}%><form?action="login_conf.jsp"?method="post"><table?width="80%"><tr><td?colspan="2">用戶登陸</td></tr><tr><td>用戶名:</td><td><input?type="text"?name="id"></td></tr><tr><td>密 碼:</td><td><input?type="password"?name="password"></td></tr><tr><td?colspan="2"><input?type="submit"?value="登陸"><input?type="reset"?value="重置"></td></tr></table></form>
</center>
</body>
</html>
---------------------------?login_conf.jsp------------------------------------
<%@?page?contentType="text/html;charset=gb2312"%>
<%@?page?import="org.sky.darkness.note.factory.*"%>
<html>
<head><title>JSP+DAO?留言管理程序——登陸</title>
</head>
<body>
<center><h1>留言管理范例?——?JSP?+?DAO實現</h1><hr><br><jsp:useBean?id="person"?scope="page"?class="org.sky.darkness.note.vo.Person"/><jsp:setProperty?name="person"?property="*"/><%try{//?跳轉if(DAOFactory.getPersonDAOInstance().login(person)){//?設置用戶姓名到session范圍之中session.setAttribute("uname",person.getName())?;//?用戶合法%><jsp:forward?page="login_success.jsp"/><%}else{//?用戶非法%><jsp:forward?page="login.jsp"/><%}}catch(Exception?e){}%>
</center>
</body>
</html>
------------------login_success.jsp------------------------------------------
<%@?page?contentType="text/html;charset=gb2312"%>
<html>
<head><title>JSP+DAO?留言管理程序——登陸</title>
</head>
<body>
<center><h1>留言管理范例?——?JSP?+?DAO實現</h1><hr><br><%if(session.getAttribute("uname")!=null){//?用戶已登陸%><h2>登陸成功</h2><h2>歡迎<font?color="red"?size="12"><%=session.getAttribute("uname")%></font>光臨留言程序</h2><h3><a?href="list_notes.jsp">進入留言管理頁面</a></h3><%}else{//?用戶未登陸,提示用戶登陸,并跳轉response.setHeader("refresh","2;URL=login.jsp")?;%>您還未登陸,請先登陸!!!<br>兩秒后自動跳轉到登陸窗口!!!<br>如果沒有跳轉,請按<a?href="login.jsp">這里</a>!!!<br><%}%>
</center>
</body>
</html>
------------------------insert.jsp------------------------------
<%@?page?contentType="text/html;charset=gb2312"%>
<html>
<head><title>JSP+DAO?留言管理程序——登陸</title>
</head>
<body>
<center><h1>留言管理范例?——?JSP?+?DAO實現</h1><hr><br><%if(session.getAttribute("uname")!=null){//?用戶已登陸%><form?action="insert_do.jsp"?method="post"><table><tr><td?colspan="2">添加新留言</td></tr><tr><td>標題:</td><td><input?type="text"?name="title"></td></tr><tr><td>作者:</td><td><input?type="text"?name="author"></td></tr><tr><td>內容:</td><td><textarea?name="content"?cols="30"?rows="6"></textarea></td></tr><tr><td?colspan="2"><input?type="submit"?value="添加"><input?type="reset"?value="重置"></td></tr></table></form><h3><a?href="list_notes.jsp">回到留言列表頁</a></h3><%}else{//?用戶未登陸,提示用戶登陸,并跳轉response.setHeader("refresh","2;URL=login.jsp")?;%>您還未登陸,請先登陸!!!<br>兩秒后自動跳轉到登陸窗口!!!<br>如果沒有跳轉,請按<a?href="login.jsp">這里</a>!!!<br><%}%>
</center>
</body>
</html>
--------------------insert_do.jsp---------------------------------------------------------
<%@?page?contentType="text/html;charset=gb2312"%>
<%@?page?import="org.sky.darkness.note.factory.*"%>
<html>
<head><title>JSP+DAO?留言管理程序——登陸</title>
</head>
<body>
<center><h1>留言管理范例?——?JSP?+?DAO實現</h1><hr><br><%//?進行亂碼處理request.setCharacterEncoding("GB2312")?;%><%if(session.getAttribute("uname")!=null){//?用戶已登陸%><jsp:useBean?id="note"?scope="page"?class="org.sky.darkrness.note.vo.Note"/><jsp:setProperty?name="note"?property="*"/><%response.setHeader("refresh","2;URL=list_notes.jsp")?;try{DAOFactory.getNoteDAOInstance().insert(note)?;%>留言添加成功,兩秒后跳轉到留言列表頁!!!<br>如果沒有跳轉,請按<a?href="list_notes.jsp">這里</a>!!!<%}catch(Exception?e){%>留言添加失敗,兩秒后跳轉到留言列表頁!!!<br>如果沒有跳轉,請按<a?href="list_notes.jsp">這里</a>!!!<%}%><%}else{//?用戶未登陸,提示用戶登陸,并跳轉response.setHeader("refresh","2;URL=login.jsp")?;%>您還未登陸,請先登陸!!!<br>兩秒后自動跳轉到登陸窗口!!!<br>如果沒有跳轉,請按<a?href="login.jsp">這里</a>!!!<br><%}%>
</center>
</body>
</html>
-------------------update.jsp-------------------------------------
<%@?page?contentType="text/html;charset=gb2312"%>
<%@?page?import="org.sky.darkness.note.factory.*"%>
<%@?page?import="org.sky.darkness.note.vo.*"%>
<html>
<head><title>JSP+DAO?留言管理程序——登陸</title>
</head>
<body>
<center><h1>留言管理范例?——?JSP?+?DAO實現</h1><hr><br><%//?進行亂碼處理request.setCharacterEncoding("GB2312")?;%><%if(session.getAttribute("uname")!=null){//?用戶已登陸%> <%//?接收參數int?id?=?0?;try{id?=?Integer.parseInt(request.getParameter("id"))?;}catch(Exception?e){}%><%Note?note?=?null?;try{note?=?DAOFactory.getNoteDAOInstance().queryById(id)?;}catch(Exception?e){}%><%if(note!=null){id?=?note.getId()?;String?title?=?note.getTitle()?;String?author?=?note.getAuthor()?;String?content?=?note.getContent()?;%><form?action="update_do.jsp"?method="post"><table><tr><td?colspan="2">添加新留言</td></tr><tr><td>標題:</td><td><input?type="text"?name="title"?value="<%=title%>"></td></tr><tr><td>作者:</td><td><input?type="text"?name="author"?value="<%=author%>"></td></tr><tr><td>內容:</td><td><textarea?name="content"?cols="30"?rows="6"><%=content%></textarea></td></tr><tr><td?colspan="2"><input?type="hidden"?name="id"?value="<%=id%>"><input?type="submit"?value="更新"><input?type="reset"?value="重置"></td></tr></table></form><%}else{%>沒有發現,要更新的內容!!<br>請確認要更新的留言是否存在!!<br><%}%><h3><a?href="list_notes.jsp">回到留言列表頁</a></h3><%}else{//?用戶未登陸,提示用戶登陸,并跳轉response.setHeader("refresh","2;URL=login.jsp")?;%>您還未登陸,請先登陸!!!<br>兩秒后自動跳轉到登陸窗口!!!<br>如果沒有跳轉,請按<a?href="login.jsp">這里</a>!!!<br><%}%>
</center>
</body>
</html>
-----------------------------update_do.jsp--------------------------------
<%@?page?contentType="text/html;charset=gb2312"%>
<%@?page?import="org.sky.darkness.note.factory.*"%>
<html>
<head><title>JSP+DAO?留言管理程序——登陸</title>
</head>
<body>
<center><h1>留言管理范例?——?JSP?+?DAO實現</h1><hr><br><%//?進行亂碼處理request.setCharacterEncoding("GB2312")?;%><jsp:useBean?id="note"?scope="page"?class="org.sky.darkness.note.vo.Note"/><jsp:setProperty?name="note"?property="*"/><%if(session.getAttribute("uname")!=null){//?用戶已登陸%><%response.setHeader("refresh","2;URL=list_notes.jsp")?;try{DAOFactory.getNoteDAOInstance().update(note)?;%>留言修改成功,兩秒后跳轉到留言列表頁!!!<br>如果沒有跳轉,請按<a?href="list_notes.jsp">這里</a>!!!<%}catch(Exception?e){%>留言修改失敗,兩秒后跳轉到留言列表頁!!!<br>如果沒有跳轉,請按<a?href="list_notes.jsp">這里</a>!!!<%}%><%}else{//?用戶未登陸,提示用戶登陸,并跳轉response.setHeader("refresh","2;URL=login.jsp")?;%>您還未登陸,請先登陸!!!<br>兩秒后自動跳轉到登陸窗口!!!<br>如果沒有跳轉,請按<a?href="login.jsp">這里</a>!!!<br><%}%>
</center>
</body>
</html>
-------------------------list_notes.jsp--------------------------
<%@?page?contentType="text/html;charset=gb2312"%>
<%@?page?import="java.util.*"%>
<%@?page?import="org.sky.darkness.note.factory.*"%>
<%@?page?import="org.sky.darkness.note.vo.*"%>
<html>
<head><title>JSP+DAO?留言管理程序——登陸</title>
</head>
<body>
<center><h1>留言管理范例?——?JSP?+?DAO實現</h1><hr><br><%//?編碼轉換request.setCharacterEncoding("GB2312")?;if(session.getAttribute("uname")!=null){//?用戶已登陸%><%//?如果有內容,則修改變量i,如果沒有,則根據i的值進行無內容提示int?i?=?0?;String?sql?=?null;?String?keyword?=?request.getParameter("keyword")?;List?all?=?null?;try{if(keyword==null){all?=?DAOFactory.getNoteDAOInstance().queryAll()?;}else{//?有查詢條件all?=?DAOFactory.getNoteDAOInstance().queryByLike(keyword)?;}}catch(Exception?e){System.out.println(e)?;}%>
<form?action="list_notes.jsp"?method="POST">請輸入查詢內容:<input?type="text"?name="keyword"><input?type="submit"?value="查詢">
</form>
</h3><a?href="insert.jsp">添加新留言</a></h3>
<table?width="80%"?border="1"><tr><td>留言ID</td><td>標題</td><td>作者</td><td>內容</td><td>刪除</td></tr><%Iterator?iter?=?all.iterator()?;while(iter.hasNext()){Note?note?=?(Note)iter.next()?;i++?;//?進行循環打印,打印出所有的內容,以表格形式//?從數據庫中取出內容int?id?=?note.getId()?;String?title?=?note.getTitle()?;String?author?=?note.getAuthor()?;String?content?=?note.getContent()?;if(keyword!=null){//?需要將數據返紅title?=?title.replaceAll(keyword,"<font?color=\"red\">"+keyword+"</font>")?;author?=?author.replaceAll(keyword,"<font?color=\"red\">"+keyword+"</font>")?;content?=?content.replaceAll(keyword,"<font?color=\"red\">"+keyword+"</font>")?;}%><tr><td><%=id%></td><td><a?href="update.jsp?id=<%=id%>"><%=title%></a></td><td><%=author%></td><td><%=content%></td><td><a?href="delete_do.jsp?id=<%=id%>">刪除</a></td></tr><%}//?判斷i的值是否改變,如果改變,則表示有內容,反之,無內容if(i==0){//?進行提示%><tr><td?colspan="5">沒有任何內容!!!</td></tr><%}%>
</table><%}else{//?用戶未登陸,提示用戶登陸,并跳轉response.setHeader("refresh","2;URL=login.jsp")?;%>您還未登陸,請先登陸!!!<br>兩秒后自動跳轉到登陸窗口!!!<br>如果沒有跳轉,請按<a?href="login.jsp">這里</a>!!!<br><%}%>
</center>
</body>
</html>
------------------------delete_do.jsp-----------------------------------------
<%@?page?contentType="text/html;charset=gb2312"%>
<%@?page?import="org.sky.darkness.note.factory.*"%><html>
<head><title>JSP+DAO?留言管理程序——登陸</title>
</head>
<body>
<center><h1>留言管理范例?——?JSP?+?DAO實現</h1><hr><br><%if(session.getAttribute("uname")!=null){//?用戶已登陸%> <%//?接收參數int?id?=?0?;try{id?=?Integer.parseInt(request.getParameter("id"))?;}catch(Exception?e){}%><%response.setHeader("refresh","2;URL=list_notes.jsp")?;try{DAOFactory.getNoteDAOInstance().delete(id)?;%>留言刪除成功,兩秒后跳轉到留言列表頁!!!<br>如果沒有跳轉,請按<a?href="list_notes.jsp">這里</a>!!!<%}catch(Exception?e){%>留言刪除失敗,兩秒后跳轉到留言列表頁!!!<br>如果沒有跳轉,請按<a?href="list_notes.jsp">這里</a>!!!<%}%><%}else{//?用戶未登陸,提示用戶登陸,并跳轉response.setHeader("refresh","2;URL=login.jsp")?;%>您還未登陸,請先登陸!!!<br>兩秒后自動跳轉到登陸窗口!!!<br>如果沒有跳轉,請按<a?href="login.jsp">這里</a>!!!<br><%}%>
</center>
</body>
</html>
轉載于:https://my.oschina.net/darkness/blog/357466
總結
以上是生活随笔為你收集整理的JSP复习笔记——第10章 连接数据库 之 jsp+DAO实现留言管理程序的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。