無聊弄了這個東西,還是發表出來吧 導入包 實體類 資料庫連接 資料庫操作 service層數據操作 網頁對service層可視化實現 model package com.ij34.model; public class article { private int id; private String ...
無聊弄了這個東西,還是發表出來吧
導入包---實體類------資料庫連接----資料庫操作----service層數據操作----網頁對service層可視化實現
model
package com.ij34.model; public class article { private int id; private String title; private String content; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }View Code
dao
package com.ij34.dao; import java.sql.DriverManager; import com.mysql.jdbc.Connection; public class DbConn { private static String username="root"; private static String password="123456"; private static String url="jdbc:mysql://localhost:3306/articles?useUnicode=true&characterEncoding=UTF-8"; private static Connection conn=null; public static Connection getConnection(){ if(conn==null){ try { Class.forName("com.mysql.jdbc.Driver"); conn=(Connection) DriverManager.getConnection(url, username, password); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } return conn; } //test connection public static void main(String[] args) { DbConn db=new DbConn(); System.out.println(db.toString()); } }View Code
package com.ij34.dao; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.ij34.model.article; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; public class ArticleDao { public List<article> getfindAll(){ //查All,返回list List<article> list=new ArrayList<article>(); String sql="select * from article"; Connection conn=DbConn.getConnection(); try { PreparedStatement pstm=(PreparedStatement) conn.prepareStatement(sql); ResultSet rs=pstm.executeQuery(); while(rs.next()){ article art=new article(); art.setId(rs.getInt("id")); art.setTitle(rs.getString("title")); art.setContent(rs.getString("content")); list.add(art); } rs.close(); pstm.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return list; } public boolean addArticle(article art){ //增 String sql="insert into article(title,content)values(?,?)"; Connection conn=DbConn.getConnection(); try { PreparedStatement pstm=(PreparedStatement) conn.prepareStatement(sql); pstm.setString(1, art.getTitle()); pstm.setString(2, art.getContent()); int count=pstm.executeUpdate(); pstm.close(); if(count>0) return true; else return false; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return false; } public boolean updateArticle(article art){ //修改 String sql="update article set title=?,content=? where id=?"; Connection conn=DbConn.getConnection(); try { PreparedStatement pstm=(PreparedStatement) conn.prepareStatement(sql); pstm.setString(1, art.getTitle()); pstm.setString(2, art.getContent()); pstm.setInt(3, art.getId()); int count=pstm.executeUpdate(); pstm.close(); if(count>0) return true; else return false; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return false; } public boolean deleteArticle(int id){ //刪除,根據id String sql="delete from article where id=?"; Connection conn=DbConn.getConnection(); try { PreparedStatement pstm=(PreparedStatement) conn.prepareStatement(sql); pstm.setInt(1, id); //id不可以寫在上面sql上 int count=pstm.executeUpdate(); pstm.close(); if(count>0) return true; else return false; } catch (Exception e) { e.printStackTrace(); } return false; } public article selectById(int id){//根據id查找 String sql="select * from article where id="+id; Connection conn=DbConn.getConnection(); article art=null; try { PreparedStatement pstm=(PreparedStatement) conn.prepareStatement(sql); ResultSet rs=pstm.executeQuery(); while(rs.next()){ art=new article(); art.setId(rs.getInt("id")); art.setTitle(rs.getString("title")); art.setContent(rs.getString("content")); } rs.close(); pstm.close(); } catch (Exception e) { e.printStackTrace(); } return art; } }View Code
service
add
package com.ij34.service; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ij34.dao.ArticleDao; import com.ij34.model.article; public class addArticle extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub article art=new article(); String title=req.getParameter("title"); String content=req.getParameter("content"); art.setTitle(new String(title.getBytes("ISO-8859-1"),"UTF-8")); art.setContent(new String(content.getBytes("ISO-8859-1"),"UTF-8")); ArticleDao ad=new ArticleDao(); ad.addArticle(art); req.getRequestDispatcher("showArticle").forward(req, resp); } }View Code
del
package com.ij34.service; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ij34.dao.ArticleDao; public class deleteArticle extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub String idStr=req.getParameter("id"); if(idStr!=null&&!idStr.equals("")){ int id=Integer.valueOf(idStr); ArticleDao ad=new ArticleDao(); ad.deleteArticle(id); } req.getRequestDispatcher("showArticle").forward(req, resp); } }View Code
find
package com.ij34.service; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ij34.dao.ArticleDao; import com.ij34.model.article; public class showArticle extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub ArticleDao ad=new ArticleDao(); List<article> list=ad.getfindAll(); req.setAttribute("list", list); req.getRequestDispatcher("show.jsp").forward(req, resp); } }View Code
update
package com.ij34.service; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ij34.dao.ArticleDao; import com.ij34.model.article; public class updateArticle extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub String idStr=req.getParameter("id"); if(idStr!=null&&!idStr.equals("")){ ArticleDao ad=new ArticleDao(); int id=Integer.valueOf(idStr); article art=ad.selectById(id); req.setAttribute("article", art); } req.getRequestDispatcher("update.jsp").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub article art=new article(); String idStr=req.getParameter("id"); String title=req.getParameter("title"); String content=req.getParameter("content"); art.setId(Integer.valueOf(idStr)); art.setTitle(new String(title.getBytes("ISO-8859-1"),"UTF-8")); art.setContent(new String(content.getBytes("ISO-8859-1"),"UTF-8")); ArticleDao ad=new ArticleDao(); ad.updateArticle(art); req.getRequestDispatcher("showArticle").forward(req, resp); } }View Code
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Articles</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>addArticle</servlet-name> <servlet-class>com.ij34.service.addArticle</servlet-class> </servlet> <servlet-mapping> <servlet-name>addArticle</servlet-name> <url-pattern>/addArticle</url-pattern> </servlet-mapping> <servlet> <servlet-name>deleteArticle</servlet-name> <servlet-class>com.ij34.service.deleteArticle</servlet-class> </servlet> <servlet-mapping> <servlet-name>deleteArticle</servlet-name> <url-pattern>/deleteArticle</url-pattern> </servlet-mapping> <servlet> <servlet-name>showArticle</servlet-name> <servlet-class>com.ij34.service.showArticle</servlet-class> </servlet> <servlet-mapping> <servlet-name>showArticle</servlet-name> <url-pattern>/showArticle</url-pattern> </servlet-mapping> <servlet> <servlet-name>updateArticle</servlet-name> <servlet-class>com.ij34.service.updateArticle</servlet-class> </servlet> <servlet-mapping> <servlet-name>updateArticle</servlet-name> <url-pattern>/updateArticle</url-pattern> </servlet-mapping> </web-app>View Code
網頁
index
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <jsp:forward page="showArticle"/> //要寫dogetView Code
add
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添 加</title> </head> <body> <form action="addArticle" method="post"> <table align="center"> <tr> <td colspan="2"><center><h4>添加</h4></center></td> </tr> <tr> <td>標 題:</td><td><input type="text" name="title"/></td> </tr> <tr> <td>內 容:</td><td><input type="text" name="content"/></td> </tr> <tr> <td><input type="submit" value="提 交"/></td><td><input type="reset" value="重 置"/></td> </tr> </table> </form> </body> </html>View Code
update
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>修 改</title> </head> <body> <form action="updateArticle" method="post"> <table align="center"> <tr> <td colspan="2"><center><h4>修 改</h4></center></td> </tr> <tr> <td>編 號:</td><td><input type="text" name="id" value="${article.id }" readonly="readonly"/></td> </tr> <tr> <td>標 題:</td><td><input type="text" name="title" value="${article.title }"/></td> </tr> <tr> <td>內 容:</td><td><input type="text" name="content" value="${article.content }"></td> </tr> <tr> <td><input type="submit" value="提 交"/></td><td><input type="button" value="返回" onclick="history.go(-1)"/></td> </tr> </table> </form> </body> </html>View Code
show
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%