• 课堂作业


    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <style>
        .a{
            margin-top: 20px;
        }
        .b{
            font-size: 20px;
             160px;
            color: white;
            background-color: greenyellow;
        }
    </style>
    </head>
    <body>
        <%
             Object message = request.getAttribute("message");
             if(message!=null && !"".equals(message)){
         
        %>
             <script type="text/javascript">
                  alert("<%=request.getAttribute("message")%>");
             </script>
        <%} %>
        <div align="center">
            <h1 style="color: red;">公文录入</h1>
            <a href="index.jsp">返回主页</a>
            <form action="CourseServlet?method=add" method="post" onsubmit="return check()">
                <div class="a">
                    公文标题<input type="text" id="title" name="title"/>
                </div>
                <div class="a">
                    部门名称<input type="text" id="workname" name="workname" />
                </div>
                <div class="a">
                    起草人<input type="text" id="man" name="man" />
                </div>
                <div class="a">
                    <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>
                </div>
            </form>
        </div>
        <script type="text/javascript">
            function check() {
                var title = document.getElementById("title");;
                var workname = document.getElementById("workname");
                var man = document.getElementById("man");
                
                //非空
                if(title.value == '') {
                    alert('公文为空');
                    name.focus();
                    return false;
                }
                if(workname.value == '') {
                    alert('部门为空');
                    teacher.focus();
                    return false;
                }
                if(man.value == '') {
                    alert('起草人');
                    classroom.focus();
                    return false;
                }
                
            
            }
        </script>
    </body>
    </html>
    package dao;
    //对数据库的操作
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import entity.Course;
    import util.DBUtil;
    
    /**
     * 课程Dao
     * Dao层操作数据
     * @author Zheng
     *
     */
    public class CourseDao {
    
        /**
         * 添加 增
         * @param course
         * @return
         */
        public boolean add(Course course) {
            String sql = "insert into course(title, workname, man) values('" + course.getTitle() + "','" + course.getWorkname() + "','" + course.getMan() + "')";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            boolean f = false;
            int a = 0;
            
            try {
                state = conn.createStatement();
                state.executeUpdate(sql);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(state, conn);
            }
            
            if (a > 0) {
                f = true;
            }
            return f;
        }
        
        public boolean title(String title) {
            boolean flag = false;
            String sql = "select name from course where name = '" + title + "'";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            ResultSet rs = null;
            
            try {
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                while (rs.next()) {
                    flag = true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(rs, state, conn);
            }
            return flag;
        }
    }
    package service;
    
    import java.util.List;
    
    import dao.CourseDao;
    import entity.Course;
    
    /**
     * CourseService
     * 服务层
     * @author Zheng
     *
     */
    @SuppressWarnings("unused")
    public class CourseService {
    
        CourseDao cDao = new CourseDao();
        
        /**
         * 添加
         * @param course
         * @return
         */
        public boolean add(Course course) {
            boolean f = false;
            if(!cDao.title(course.getTitle())) {
                cDao.add(course);
                f = true;
            }
            return f;
        }}
    package entity;
    
    public class Course {
    
        
        private String title;
        private String workname;
        private String man;
        
    
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getWorkname() {
            return workname;
        }
        public void setWorkname(String workname) {
            this.workname = workname;
        }
        public String getMan() {
            return man;
        }
        public void setMan(String man) {
            this.man = man;
        }
        
        public Course() {}
        //*构造方法
    
        
        public Course(String title, String workname, String man) {
            this.title = title;
            this.workname = workname;
            this.man = man;
        }
    }
    package servlet;
    
    import java.io.IOException;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import entity.Course;
    import service.CourseService;
    
    @SuppressWarnings("unused")
    @WebServlet("/CourseServlet")
    public class CourseServlet extends HttpServlet {
        
        private static final long serialVersionUID = 1L;
    
        CourseService service = new CourseService();
        
        /**
         * 方法选择
         */
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            String method = req.getParameter("method");
            if ("add".equals(method)) {
                add(req, resp);
            }
        }
    
        /**
         * 添加
         * @param req
         * @param resp
         * @throws IOException 
         * @throws ServletException 
         */
        private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
            req.setCharacterEncoding("utf-8");
            String title = req.getParameter("title");
            String workname = req.getParameter("workname");
            String man = req.getParameter("man");
            Course course = new Course(title,workname,man);
            
            //添加后消息显示
            if(service.add(course)) {
                req.setAttribute("message", "添加成功");
                req.getRequestDispatcher("add.jsp").forward(req,resp);
            } else {
                req.setAttribute("message", "课程名称重复,请重新录入");
                req.getRequestDispatcher("add.jsp").forward(req,resp);
            }
        }}
    package util;
    //数据库连接
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /**
     * 数据库连接工具
     * @author Zheng
     *
     */
    public class DBUtil {
        
        public static String db_url = "jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true";
        public static String db_user = "root";
        public static String db_pass = "1234";
        
        public static Connection getConn () {
            Connection conn = null;
            
            try {
                Class.forName("com.mysql.jdbc.Driver");//加载驱动
                conn = DriverManager.getConnection(db_url, db_user, db_pass);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return conn;
        }
        
        /**
         * 关闭连接
         * @param state
         * @param conn
         */
        public static void close (Statement state, Connection conn) {
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void close (ResultSet rs, Statement state, Connection conn) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
  • 相关阅读:
    java网络编程基础——网络基础
    java并发编程基础——线程相关的类
    java并发编程基础——线程池
    java并发编程基础——线程通信
    java并发编程基础——线程同步
    java并发编程基础—生命周期与线程控制
    java并发编程基础——线程的创建
    StreamAPI
    Lambda表达式
    Java Annotation
  • 原文地址:https://www.cnblogs.com/xk1013/p/12012353.html
Copyright © 2020-2023  润新知