zoukankan      html  css  js  c++  java
  • 10.22

    今天经过好长好长的时间努力,终于将整个添加课程的web小系统全部写完

    来,上全部代码!

    dao层的代码,这一部分主要是最底层的代码,是和数据库进行直接交互的代码

    package com.jaovo.msg.dao;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    import com.jaovo.msg.Util.DBUtil;
    //import com.jaovo.msg.Util.UserException;
    import com.jaovo.msg.model.User;
    public class UserDaoImpl {
    public void add(User user) {
            
            Connection connection = DBUtil.getConnection();
            System.out.println(1);
    
    
            PreparedStatement preparedStatement = null;
            System.out.println(2);
                try{
                    System.out.println(1);
                String sql = "insert into t_teacher(Name,Teacher,Place) values (?,?,?)";
                
                preparedStatement = connection.prepareStatement(sql);
                
                
                preparedStatement.setString(1, user.getName());
                preparedStatement.setString(2, user.getTeacher());
                preparedStatement.setString(3, user.getPlace());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                
                //DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
               
    }
    }

    user数据

    package com.jaovo.msg.model;
    
    public class User {
         private String  name;
            private String  teacher;
            private String  place;
            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 getPlace() {
                return place;
            }
            public void setPlace(String place) {
                this.place = place;
            }
    }

    dbutil代码,,就是连接mysql数据库的代码

    package com.jaovo.msg.Util;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    public class DBUtil {
         public  static  Connection getConnection() {
                try {
                
                    Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
                } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String user = "root";
                String password = "数据库的密码";
                String url = "jdbc:mysql://localhost:3306/jaovo_msg?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
                Connection connection = null;
                try {
                    
                     connection = DriverManager.getConnection(url,user,password);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return connection;
            }
            
    
            public static void close(Connection connection ) {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                    
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            public static void close(PreparedStatement preparedStatement ) {
                try {
                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                    
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            public static void close(ResultSet resultSet ) {
                try {
                    if (resultSet != null) {
                        resultSet.close();
                    }
                    
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    }

    下面就是前两天写的两个界面,重复一下

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
        /*<style type="text/css">
            body{
                background: #FFFF00;
                align-items: center;
                text-align: center;
            }
        </style>*/
        
    </head>
    
    <body>
    <body background="123.jpg"></body>
    <%
    String s=(String)request.getAttribute("Error");
    if("".equals(s)||s==null)
    {
        s="";
    }
    %>
    <%=s %>
    <form action="LoginInput.jsp" method="get">
            <table align="center" border="1">
    <tr>
    <td colspan="2">
    课程名称
     <input type="text" name="name"/>
    </td>
    </tr>
    <tr>
       <td colspan="2">
        任课教师
     <input type="text" name="teacher"/>
       </td>
      </tr>
      <tr>
       <td colspan="2">
        上课地点
     <input type="text" name="place"/>
       </td>
      </tr>
     
      <tr>
      <td align="center" colspan="2">
       <input type="submit" value="保存"/></td>
      </tr>
      </table>
      </form>
    </body>
    </html>
    <%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
    <%@page import="com.jaovo.msg.model.User"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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">
    <%
             int i=0;
             boolean flag=true;
             boolean flag1=true;
             String s[]={"王建民","刘丹","刘立嘉","杨子光","王辉"};
             String s1[]={"基教","一教","二教","三教"};
             String name=request.getParameter("name");
             String teacher=request.getParameter("teacher");
             String place=request.getParameter("place");
            while(i<s.length)
             {
            if(!s[i].equals(teacher))
         {
          flag=false;
         }
            else
                {
                flag=true;
                break;
                }
          i++;
         }%>
         <%
         if(!flag)
         {
             request.setAttribute("Error", "老师不对");
         
         %>
         <jsp:forward page="Login.jsp"></jsp:forward>
           <% 
         }
         i=0;
         String s3=place.substring(0,2);
     
         while(flag==true&&i<s1.length)
         {
            
        if(!s1[i].equals(s3))
             {
                 flag1=false;
              }
             else
            {
            flag1=true;
            break;
            }
              i++;
           }
         if(!flag1)
         {
             request.setAttribute("Error", "教室不对");
         
         %>
         <jsp:forward page="Login.jsp"></jsp:forward>
           <% 
         }
            UserDaoImpl userdao=new UserDaoImpl();
             User user=new User();
             user.setName(name);
             user.setTeacher(teacher);
             user.setPlace(place);
             userdao.add(user);
             System.out.println(teacher);
    %>
    
    <<jsp:forward page="Login.jsp"></jsp:forward>
  • 相关阅读:
    学习两个Python不常用的语法
    使用uwsgi在centos7上部署Flask在线Web服务
    redis server学习002
    上传至gitlab—— 本地git出现fatal: The current branch develop has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin develop 的问题
    数据库脏数据——数据库中的并发操作带来的一系列问题
    error: failed to push some refs to 'git@github.com:Sirxy/flask_jwt_demo.git'
    redis server学习001
    【解决方案】麒麟v10,更新yum源
    【解决方案】github图片加载不出来
    linux c程序高cpu,排查记录
  • 原文地址:https://www.cnblogs.com/cdl-sunshine/p/14159357.html
Copyright © 2011-2022 走看看