查看: 2418|回复: 0

[HTML代码] 基本的CRUD操作

发表于 2018-1-2 08:00:00

无聊弄了这个东西,还是发表出来吧

导入包---实体类------数据库连接----数据库操作----service层数据操作----网页对service层可视化实现


model
  1. package com.ij34.model;
  2. public class article {
  3. private int id;
  4. private String title;
  5. private String content;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getTitle() {
  13. return title;
  14. }
  15. public void setTitle(String title) {
  16. this.title = title;
  17. }
  18. public String getContent() {
  19. return content;
  20. }
  21. public void setContent(String content) {
  22. this.content = content;
  23. }
  24. }
复制代码
View Code

dao
  1. package com.ij34.dao;
  2. import java.sql.DriverManager;
  3. import com.mysql.jdbc.Connection;
  4. public class DbConn {
  5. private static String username="root";
  6. private static String password="123456";
  7. private static String url="jdbc:mysql://localhost:3306/articles?useUnicode=true&characterEncoding=UTF-8";
  8. private static Connection conn=null;
  9. public static Connection getConnection(){
  10. if(conn==null){
  11. try {
  12. Class.forName("com.mysql.jdbc.Driver");
  13. conn=(Connection) DriverManager.getConnection(url, username, password);
  14. } catch (Exception e) {
  15. // TODO: handle exception
  16. e.printStackTrace();
  17. }
  18. }
  19. return conn;
  20. }
  21. //test connection
  22. public static void main(String[] args) {
  23. DbConn db=new DbConn();
  24. System.out.println(db.toString());
  25. }
  26. }
复制代码
View Code

  1. package com.ij34.dao;
  2. import java.sql.ResultSet;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import com.ij34.model.article;
  6. import com.mysql.jdbc.Connection;
  7. import com.mysql.jdbc.PreparedStatement;
  8. public class ArticleDao {
  9. public List<article> getfindAll(){ //查All,返回list
  10. List<article> list=new ArrayList<article>();
  11. String sql="select * from article";
  12. Connection conn=DbConn.getConnection();
  13. try {
  14. PreparedStatement pstm=(PreparedStatement) conn.prepareStatement(sql);
  15. ResultSet rs=pstm.executeQuery();
  16. while(rs.next()){
  17. article art=new article();
  18. art.setId(rs.getInt("id"));
  19. art.setTitle(rs.getString("title"));
  20. art.setContent(rs.getString("content"));
  21. list.add(art);
  22. }
  23. rs.close();
  24. pstm.close();
  25. } catch (Exception e) {
  26. // TODO: handle exception
  27. e.printStackTrace();
  28. }
  29. return list;
  30. }
  31. public boolean addArticle(article art){ //增
  32. String sql="insert into article(title,content)values(?,?)";
  33. Connection conn=DbConn.getConnection();
  34. try {
  35. PreparedStatement pstm=(PreparedStatement) conn.prepareStatement(sql);
  36. pstm.setString(1, art.getTitle());
  37. pstm.setString(2, art.getContent());
  38. int count=pstm.executeUpdate();
  39. pstm.close();
  40. if(count>0) return true;
  41. else return false;
  42. } catch (Exception e) {
  43. // TODO: handle exception
  44. e.printStackTrace();
  45. }
  46. return false;
  47. }
  48. public boolean updateArticle(article art){ //修改
  49. String sql="update article set title=?,content=? where id=?";
  50. Connection conn=DbConn.getConnection();
  51. try {
  52. PreparedStatement pstm=(PreparedStatement) conn.prepareStatement(sql);
  53. pstm.setString(1, art.getTitle());
  54. pstm.setString(2, art.getContent());
  55. pstm.setInt(3, art.getId());
  56. int count=pstm.executeUpdate();
  57. pstm.close();
  58. if(count>0) return true;
  59. else return false;
  60. } catch (Exception e) {
  61. // TODO: handle exception
  62. e.printStackTrace();
  63. }
  64. return false;
  65. }
  66. public boolean deleteArticle(int id){ //删除,根据id
  67. String sql="delete from article where id=?";
  68. Connection conn=DbConn.getConnection();
  69. try {
  70. PreparedStatement pstm=(PreparedStatement) conn.prepareStatement(sql);
  71. pstm.setInt(1, id); //id不可以写在上面sql上
  72. int count=pstm.executeUpdate();
  73. pstm.close();
  74. if(count>0) return true;
  75. else return false;
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. return false;
  80. }
  81. public article selectById(int id){//根据id查找
  82. String sql="select * from article where id="+id;
  83. Connection conn=DbConn.getConnection();
  84. article art=null;
  85. try {
  86. PreparedStatement pstm=(PreparedStatement) conn.prepareStatement(sql);
  87. ResultSet rs=pstm.executeQuery();
  88. while(rs.next()){
  89. art=new article();
  90. art.setId(rs.getInt("id"));
  91. art.setTitle(rs.getString("title"));
  92. art.setContent(rs.getString("content"));
  93. }
  94. rs.close();
  95. pstm.close();
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. }
  99. return art;
  100. }
  101. }
复制代码
View Code

service

add

  1. package com.ij34.service;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import com.ij34.dao.ArticleDao;
  8. import com.ij34.model.article;
  9. public class addArticle extends HttpServlet{
  10. /**
  11. *
  12. */
  13. private static final long serialVersionUID = 1L;
  14. @Override
  15. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  16. // TODO Auto-generated method stub
  17. this.doPost(req, resp);
  18. }
  19. @Override
  20. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  21. // TODO Auto-generated method stub
  22. article art=new article();
  23. String title=req.getParameter("title");
  24. String content=req.getParameter("content");
  25. art.setTitle(new String(title.getBytes("ISO-8859-1"),"UTF-8"));
  26. art.setContent(new String(content.getBytes("ISO-8859-1"),"UTF-8"));
  27. ArticleDao ad=new ArticleDao();
  28. ad.addArticle(art);
  29. req.getRequestDispatcher("showArticle").forward(req, resp);
  30. }
  31. }
复制代码
View Code

del

  1. package com.ij34.service;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import com.ij34.dao.ArticleDao;
  8. public class deleteArticle extends HttpServlet{
  9. /**
  10. *
  11. */
  12. private static final long serialVersionUID = 1L;
  13. @Override
  14. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  15. // TODO Auto-generated method stub
  16. this.doPost(req, resp);
  17. }
  18. @Override
  19. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  20. // TODO Auto-generated method stub
  21. String idStr=req.getParameter("id");
  22. if(idStr!=null&&!idStr.equals("")){
  23. int id=Integer.valueOf(idStr);
  24. ArticleDao ad=new ArticleDao();
  25. ad.deleteArticle(id);
  26. }
  27. req.getRequestDispatcher("showArticle").forward(req, resp);
  28. }
  29. }
复制代码
View Code

find

  1. package com.ij34.service;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import com.ij34.dao.ArticleDao;
  9. import com.ij34.model.article;
  10. public class showArticle extends HttpServlet{
  11. /**
  12. *
  13. */
  14. private static final long serialVersionUID = 1L;
  15. @Override
  16. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  17. // TODO Auto-generated method stub
  18. this.doPost(req, resp);
  19. }
  20. @Override
  21. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  22. // TODO Auto-generated method stub
  23. ArticleDao ad=new ArticleDao();
  24. List<article> list=ad.getfindAll();
  25. req.setAttribute("list", list);
  26. req.getRequestDispatcher("show.jsp").forward(req, resp);
  27. }
  28. }
复制代码
View Code

update

  1. package com.ij34.service;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import com.ij34.dao.ArticleDao;
  8. import com.ij34.model.article;
  9. public class updateArticle extends HttpServlet{
  10. /**
  11. *
  12. */
  13. private static final long serialVersionUID = 1L;
  14. @Override
  15. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  16. // TODO Auto-generated method stub
  17. String idStr=req.getParameter("id");
  18. if(idStr!=null&&!idStr.equals("")){
  19. ArticleDao ad=new ArticleDao();
  20. int id=Integer.valueOf(idStr);
  21. article art=ad.selectById(id);
  22. req.setAttribute("article", art);
  23. }
  24. req.getRequestDispatcher("update.jsp").forward(req, resp);
  25. }
  26. @Override
  27. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  28. // TODO Auto-generated method stub
  29. article art=new article();
  30. String idStr=req.getParameter("id");
  31. String title=req.getParameter("title");
  32. String content=req.getParameter("content");
  33. art.setId(Integer.valueOf(idStr));
  34. art.setTitle(new String(title.getBytes("ISO-8859-1"),"UTF-8"));
  35. art.setContent(new String(content.getBytes("ISO-8859-1"),"UTF-8"));
  36. ArticleDao ad=new ArticleDao();
  37. ad.updateArticle(art);
  38. req.getRequestDispatcher("showArticle").forward(req, resp);
  39. }
  40. }
复制代码
View Code

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>Articles</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. </welcome-file-list>
  7. <servlet>
  8. <servlet-name>addArticle</servlet-name>
  9. <servlet-class>com.ij34.service.addArticle</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>addArticle</servlet-name>
  13. <url-pattern>/addArticle</url-pattern>
  14. </servlet-mapping>
  15. <servlet>
  16. <servlet-name>deleteArticle</servlet-name>
  17. <servlet-class>com.ij34.service.deleteArticle</servlet-class>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>deleteArticle</servlet-name>
  21. <url-pattern>/deleteArticle</url-pattern>
  22. </servlet-mapping>
  23. <servlet>
  24. <servlet-name>showArticle</servlet-name>
  25. <servlet-class>com.ij34.service.showArticle</servlet-class>
  26. </servlet>
  27. <servlet-mapping>
  28. <servlet-name>showArticle</servlet-name>
  29. <url-pattern>/showArticle</url-pattern>
  30. </servlet-mapping>
  31. <servlet>
  32. <servlet-name>updateArticle</servlet-name>
  33. <servlet-class>com.ij34.service.updateArticle</servlet-class>
  34. </servlet>
  35. <servlet-mapping>
  36. <servlet-name>updateArticle</servlet-name>
  37. <url-pattern>/updateArticle</url-pattern>
  38. </servlet-mapping>
  39. </web-app>
复制代码
View Code

网页

index

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <jsp:forward page="showArticle"/> //要写doget
复制代码
View Code

add

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>添 加</title>
  9. </head>
  10. <body>
  11. <form action="addArticle" method="post">
  12. <table align="center">
  13. <tr>
  14. <td colspan="2"><center><h4>添加</h4></center></td>
  15. </tr>
  16. <tr>
  17. <td>标 题:</td><td><input type="text" name="title"/></td>
  18. </tr>
  19. <tr>
  20. <td>内 容:</td><td><input type="text" name="content"/></td>
  21. </tr>
  22. <tr>
  23. <td><input type="submit" value="提 交"/></td><td><input type="reset" value="重 置"/></td>
  24. </tr>
  25. </table>
  26. </form>
  27. </body>
  28. </html>
复制代码
View Code

update

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>修 改</title>
  8. </head>
  9. <body>
  10. <form action="updateArticle" method="post">
  11. <table align="center">
  12. <tr>
  13. <td colspan="2"><center><h4>修 改</h4></center></td>
  14. </tr>
  15. <tr>
  16. <td>编 号:</td><td><input type="text" name="id" value="${article.id }" readonly="readonly"/></td>
  17. </tr>
  18. <tr>
  19. <td>标 题:</td><td><input type="text" name="title" value="${article.title }"/></td>
  20. </tr>
  21. <tr>
  22. <td>内 容:</td><td><input type="text" name="content" value="${article.content }"></td>
  23. </tr>
  24. <tr>
  25. <td><input type="submit" value="提 交"/></td><td><input type="button" value="返回" onclick="history.go(-1)"/></td>
  26. </tr>
  27. </table>
  28. </form>
  29. </body>
  30. </html>
复制代码
View Code

show

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>显示</title>
  9. </head>
  10. <body>
  11. <form action="showArticle" method="post">
  12. <table align="center">
  13. <tr>
  14. <th>编 号 </th>
  15. <th>标 题 </th>
  16. <th> 内 容 </th>
  17. <th> 操 作 </th>
  18. </tr>
  19. <c:forEach var="li" items="${list}">
  20. <tr>
  21. <td>${li.id} </td>
  22. <td> ${li.title} </td>
  23. <td> ${li.content} </td>
  24. <td><a href="deleteArticle?id=${li.id}">删除</a>|<a href="updateArticle?id=${li.id}">修改</a></td>
  25. </tr>
  26. </c:forEach>
  27. <tr>
  28. <td align="center"><a href="add.jsp"> 添 加 </a></td>
  29. </tr>
  30. </table>
  31. </form>
  32. </body>
  33. </html>
复制代码
View Code
结果



回复

使用道具 举报