• Java自学


     今天完成了Javaweb和数据库连接,课程录入的代码

          aaa.jsp:

    复制代码
    复制代码
    <%@ 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="${pageContext.request.contextPath }/addServlet" method="post" onsubmit="return check()">
                <div class="a">
                    课程名称<input type="text" id="name" name="name"/>
                </div>
                <div class="a">
                    任课教师<input type="text" id="teacher" name="teacher" />
                </div>
                <div class="a">
                    上课地点<input type="text" id="classroom" name="classroom" />
                </div>
                <div class="a">
                    <button type="submit" class="b">保   存<tton>
                </div>
               <tr>
                    <td>学历:</td>
                    <td>
                        <input type="radio"name="degree"value="专科">专科
                        <input type="radio"name="degree"value="本科"checked>本科
                        <input type="radio"name="degree"value="硕士研究生">硕士研究生
                        <input type="radio"name="degree"value="博士研究生">博士研究生
                        <input type="radio"name="degree"value="其他">其他
                    </td>
                </tr>
            </form>
        </div>
        <script type="text/javascript">
            function check() {
                var name = document.getElementById("name");;
                var teacher = document.getElementById("teacher");
                var classroom = document.getElementById("classroom");
                 
                //非空
                if(name.value == '') {
                    alert('课程名称为空');
                    name.focus();
                    return false;
                }
                if(teacher.value == '') {
                    alert('教师为空');
                    teacher.focus();
                    return false;
                }
                if(classroom.value == '') {
                    alert('上课地点为空');
                    classroom.focus();
                    return false;
                }
                 
                //教师
                if(teacher.value != '王建民' && teacher.value != '王辉' && teacher.value != '刘丹' && teacher.value != '刘立嘉' && teacher.value != '杨子光'){
                    alert('教师名称错误');
                    return false;
                }
                 
                //教室
                if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) {
                    alert('上课地点错误');
                    return false;
                }
            }
                </script type="text/javascript">
    复制代码
    复制代码

         DBUtil:

    复制代码
    复制代码
    package DBUtil;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    public class DBUtil {
    
        public static String db_url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
        public static String db_user = "root";
        public static String db_pass = "chenhan";
    
        public static Connection getConn () {
            Connection conn = null;
    
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                conn = DriverManager.getConnection(db_url, db_user, db_pass);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return conn;
        }//end getConn
    
        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();
                }
            }
        }
    
        public static void main(String[] args) throws SQLException {
            Connection conn = getConn();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql ="select * from mmm";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if(rs.next()){
                System.out.println("连接成功");
            }else{
                System.out.println("连接失败");
            }
        }
    }
    复制代码
    复制代码

        Dao:

    复制代码
    复制代码
    package Dao;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    import DBUtil.DBUtil;
    import domain.Course;
     
    
    public class CourseDao {
         
        /**
         * 添加
         * @param course
         * @return
         */
        public boolean add(Course course) {
            String sql = "insert into mmm(name, teacher, classroom) values('" + course.getName() + "','" + course.getTeacher() + "','" + course.getClassroom() + "')";
            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;
        }
    }
    复制代码
    复制代码

       domain:

    复制代码
    复制代码
    package domain;
    
    public class Course {
    
        private String name;
        private String teacher;
        private String classroom;
         
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getTeacher() {
            return teacher;
        }
        public void setTeacher(String teacher) {
            this.teacher = teacher;
        }
        public String getClassroom() {
            return classroom;
        }
        public void setClassroom(String classroom) {
            this.classroom = classroom;
        }
         
        public Course() {}
         
        public Course(String name, String teacher, String classroom) {
            
            this.name = name;
            this.teacher = teacher;
            this.classroom = classroom;
        }
    }
    复制代码
    复制代码

        Servlet:

    复制代码
    复制代码
    package servlet;
    
    import java.io.IOException;
    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 Dao.CourseDao;
    import domain.Course;
    
    /**
     * Servlet implementation class AddServlet
     */
    @WebServlet("/addServlet")
    public class AddServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public AddServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            String name =request.getParameter("name");
            String teacher = request.getParameter("teacher");
            String classroom =request.getParameter("classroom");
            Course course = new Course(name, teacher, classroom);
            CourseDao cd = new CourseDao(); 
            //添加后消息显示
            if(cd.add(course)) {
                request.getRequestDispatcher("aaa.jsp").forward(request,response);
            } else {
                request.getRequestDispatcher("aaa.jsp").forward(request,response);
            }
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    复制代码
    复制代码

    2

        对于代码中各个类的含义还不清楚,jsp中各部分的含义也不理解。

    3 继续学习jsp

  • 相关阅读:
    .NET Core 首例 Office 开源跨平台组件(NPOI Core)
    ASP.NET Core 导入导出Excel xlsx 文件
    python练习七—P2P下载
    VisualVM远程连接Tomcat
    一次Linux自动化部署尝试
    python练习六—简单的论坛
    shiro实现APP、web统一登录认证和权限管理
    python练习五—简单web应用
    python练习四—简单的聊天软件
    python练习三—解析xml
  • 原文地址:https://www.cnblogs.com/20193898liufa/p/14161822.html
Copyright © 2020-2023  润新知