1 2 3 4

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>

 

posted @ 2020-10-22 22:40  小陈的太阳  阅读(63)  评论(0编辑  收藏  举报