Java自学

1今天写了web的课程信息录入

复制代码
<%@ 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;
        width: 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>
        <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>
            
        </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">
复制代码
复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
    <title>登录界面</title>
    <style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
        width: 160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>
<body>
<%
    Object msg = request.getAttribute("msg");
    if(msg!=null && !"".equals(msg)){
%>
<script type="text/javascript">
    alert("<%=request.getAttribute("msg")%>");
</script>
<%} %>

<div align="center">
    <h1 style="color: #ff0000;">登录页面</h1>
 <form action="${pageContext.request.contextPath}/denglu" method="post" >
        <div class="a">
            学号<input type="text" id="xuehao" name="xuehao"/>
        </div>
        <div>
        </div>
        <div class="a">
            密码<input type="password" id="mima" name="mima"/>
        </div>
        <div class="a">
            <button type="submit" class="b">登录</button>
        </div>
    </form>
</div>


</body>
</html>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 text1(xuehao,mima,name, teacher, classroom) values('"+ course.getXuehao() + "','"+ course.getMima() + "','" + course.getName() + "','" + course.getTeacher() + "','" + course.getClassroom() + "')";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 1;
          
        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
            a=0;
        } finally {
            DBUtil.close(state, conn);
        }
          
        if (a > 0) {
            f = true;
        }
        return f;
    }
}

  

复制代码
package DBUtil;


import java.sql.*;


public class DBUtil {

    public static String db_url = "jdbc:mysql://localhost:3306/text111?serverTimezone=GMT%2B8&useSSL=false";
    public static String db_user = "root";
    public static String db_pass = "chenhan";

    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;
    }//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 text1";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        if(rs.next()){
            System.out.println("连接成功");
        }else{
            System.out.println("连接失败");
        }
    }
}
复制代码
复制代码
package domain;

public class Course {

    private String name;
    private String teacher;
    private String classroom;
    private String xuehao;
    private String mima;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTeacher() {
        return teacher;
    }
    public String getXuehao() {
        return xuehao;
    }
    public String getMima() {
        return mima;
    }
    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }
    public String getClassroom() {
        return classroom;
    }
    public void setClassroom(String classroom) {
        this.classroom = classroom;
    }
    public void setXuehao(String xuehao) {
        this.xuehao = xuehao;
    }
    public void setMima(String mima) {
        this.mima = mima;
    }
    public Course() {}
     
    public Course(String xuehao,String mima,String name, String teacher, String classroom) {
        this.xuehao = xuehao;
        this.mima = mima;
        this.name = name;
        this.teacher = teacher;
        this.classroom = classroom;
        
    }
}
复制代码
复制代码
package domain;

public class course2 {
     private String xuehao;
     private String mima;
     public String getXuehao() {
            return xuehao;
        }
     public String getMima() {
            return mima;
        }
     public void setXuehao(String xuehao) {
            this.xuehao = xuehao;
        }
     public void setMima(String mima) {
            this.mima = mima;
        }
     public course2() {}
         
     public course2(String xuehao,String mima) {
            this.xuehao = xuehao;
            this.mima = mima;        
        }
}
复制代码
复制代码
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 javax.servlet.http.HttpSession;

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");
        HttpSession session = request.getSession();
        String xuehao =   (String) session.getAttribute("xuehao");
        String mima =   (String) session.getAttribute("mima");
        String name =request.getParameter("name");
        String teacher = request.getParameter("teacher");
        String classroom =request.getParameter("classroom");
        Course course = new Course(xuehao,mima,name, teacher, classroom);
        CourseDao cd = new CourseDao(); 
        //添加后消息显示
        if(cd.add(course)) {
            request.getRequestDispatcher("add.jsp").forward(request,response);
        } else {
            request.getRequestDispatcher("add.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);
    }

}
复制代码
复制代码
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 javax.servlet.http.HttpSession;

import dao.CourseDao;
import domain.*;

/**
 * Servlet implementation class AddServlet    
 */
@WebServlet("/denglu")
public class denglu extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public denglu() {
        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");
        HttpSession session = request.getSession();
        
        String xuehao =request.getParameter("xuehao");
        String mima = request.getParameter("mima");

        session.setAttribute("xuehao",request.getParameter("xuehao"));
        session.setAttribute("mima",request.getParameter("mima"));
        
        course2 course2 = new course2(xuehao,mima);
        CourseDao cd = new CourseDao(); 
        //添加后消息显示
        request.setAttribute("msg","登录成功");
        request.getRequestDispatcher("add.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);
    }

}
复制代码
posted @ 2020-11-08 22:21  平安喜乐v  阅读(55)  评论(0编辑  收藏  举报