zoukankan      html  css  js  c++  java
  • web练习加读书体会

    12.8

    今天继续写学生管理系统,系统已经快些好了,大部分功能都已经实现,学生成绩的升序和降序都以完成,没有遇到问题,明天计划完成学生管理系统;

    下面是所有java代码层:界面部分还有些欠缺

    package com.pp;
    import java.sql.*;
    public class DBUtil{
    private DBUtil() {}
    static {
    try {
    Class.forName("com.mysql.jdbc.Driver");
    }catch(ClassNotFoundException e) {
    e.printStackTrace();
    }
    }
    public static Connection getConnection() throws SQLException{
    return DriverManager.getConnection("jdbc:mysql://localhost:3306/user?serverTimezone=UTC&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8","root","123456");
    }
    public static void close(Connection conn,Statement ps,ResultSet rs) {
    if(rs!=null) {
    try {
    rs.close();
    }catch(SQLException s) {
    s.printStackTrace();
    }
    }
    if(ps!=null) {
    try {
    ps.close();
    }catch(SQLException s) {
    s.printStackTrace();
    }
    }
    if(conn!=null) {
    try {
    conn.close();
    }catch(SQLException s) {
    s.printStackTrace();
    }
    }
    }
    }

    package com.pp;
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    public class dao {
    public void insert(student L) {
    String sql="insert into student(xuehao,name,banji,kecheng,teacher,score) values(?,?,?,?,?,?)";
    Connection con;
    try {
    con = DBUtil.getConnection();
    PreparedStatement pa=con.prepareStatement(sql);
    pa.setString(1,L.getXuehao());
    pa.setString(2,L.getName());
    pa.setString(3,L.getBanji());
    pa.setString(4,L.getKecheng());
    pa.setString(5,L.getTeacher());
    pa.setString(6,L.getScore());
    pa.execute();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public void delete(String xuehao) {
    String sql="delete from student where xuehao=?";
    Connection con;
    try {
    con = DBUtil.getConnection();
    PreparedStatement pa=con.prepareStatement(sql);
    pa.setString(1,xuehao);
    pa.execute();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public void update(String score,String kecheng,String xuehao,String teacher) {
    String sql="update student set score=? where kecheng=? and xuehao=? and teacher=?";
    Connection con;
    try {
    con = DBUtil.getConnection();
    PreparedStatement pa=con.prepareStatement(sql);
    pa.setString(1,score);
    pa.setString(2,kecheng);
    pa.setString(3,xuehao);
    pa.setString(4,teacher);
    pa.execute();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public List<student> select() {
    String sql="select * from student";
    student L=null;
    List<student> list=new ArrayList();
    Connection con;
    try {
    con = DBUtil.getConnection();
    PreparedStatement pa=con.prepareStatement(sql);
    ResultSet rs=pa.executeQuery();
    while(rs.next()) {
    String xuehao=rs.getString("xuehao");
    String name=rs.getString("name");
    String banji=rs.getString("banji");
    String kecheng=rs.getString("kecheng");
    String teacher=rs.getString("teacher");
    String score=rs.getString("score");
    L=new student(xuehao,name,banji,kecheng,teacher,score);
    list.add(L);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return list;
    }
    public boolean selectp(String num) {
    boolean panduan=false;
    String sql="select * from password";
    Connection con;
    try {
    con = DBUtil.getConnection();
    PreparedStatement pa=con.prepareStatement(sql);
    ResultSet rs=pa.executeQuery();
    while(rs.next()) {
    String numm=rs.getString("num");
    String password=rs.getString("password");
    if(num.equals(numm)&&num.equals(password)) {
    panduan=true;
    break;
    }
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return panduan;
    }
    }

    package com.pp;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Collections;
    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;

    /**
    * Servlet implementation class servlet
    */
    @WebServlet("/servlet")
    public class servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#HttpServlet()
    */
    public servlet() {
    super();
    // TODO Auto-generated constructor stub
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    String teacher=request.getParameter("num");
    if("123".equals(teacher)&&"123".equals(teacher)) {
    request.getSession().setAttribute("teacher",teacher);
    request.getRequestDispatcher("Course.jsp").forward(request, response);
    }
    else {
    response.sendRedirect("login.jsp?error=yes");
    }
    }
    private void course(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    String course=request.getParameter("course");
    if("math".equals(course)) {
    request.getSession().setAttribute("course","高数");
    request.getRequestDispatcher("gongneng.jsp").forward(request, response);
    }
    else if("java".equals(course)) {
    request.getSession().setAttribute("course","java");
    request.getRequestDispatcher("gongneng.jsp").forward(request, response);
    }
    else if("lisan".equals(course)) {
    request.getSession().setAttribute("course","离散");
    request.getRequestDispatcher("gongneng.jsp").forward(request, response);
    }
    }
    private void addstu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String xuehao=request.getParameter("xuehao");
    String name=request.getParameter("name");
    String banji=request.getParameter("banji");
    String kecheng=request.getSession().getAttribute("course").toString();
    String teacher=request.getSession().getAttribute("teacher").toString();
    String score="0";
    student stu=new student(xuehao,name,banji,kecheng,teacher,score);
    dao L=new dao();
    L.insert(stu);
    response.sendRedirect("addstu.jsp?addstu=yes");
    }
    private void deletestu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String xuehao=request.getParameter("xuehao");
    dao L=new dao();
    L.delete(xuehao);
    response.sendRedirect("deletestu.jsp?delete=yes");
    }
    private void dengji(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    dao L=new dao();
    String kecheng=request.getSession().getAttribute("course").toString();
    String teacher=request.getSession().getAttribute("teacher").toString();
    List<student> list=L.select();
    for(int i=0;i<list.size();i++) {
    if(!list.get(i).getTeacher().equals(teacher)||!list.get(i).getKecheng().equals(kecheng)) {
    list.remove(i);
    i--;
    }
    }
    request.setAttribute("list",list);
    request.getRequestDispatcher("dengji.jsp").forward(request, response);
    }
    private void dengji2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String xuehao=request.getParameter("xuehao");
    String teacher=request.getSession().getAttribute("teacher").toString();
    String kecheng=request.getSession().getAttribute("course").toString();
    String score=request.getParameter("score");
    dao L=new dao();
    L.update(score, kecheng, xuehao, teacher);
    response.sendRedirect("dengjier.jsp?dengji=yes");
    }
    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String xuehao=request.getParameter("xuehao");
    String teacher=request.getSession().getAttribute("teacher").toString();
    String kecheng=request.getSession().getAttribute("course").toString();
    String score=request.getParameter("score");
    dao L=new dao();
    L.update(score, kecheng, xuehao, teacher);
    response.sendRedirect("update.jsp?update=yes");
    }
    private void numselect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String xuehao=request.getParameter("xuehao");
    String kecheng=request.getSession().getAttribute("course").toString();
    String teacher=request.getSession().getAttribute("teacher").toString();
    dao L=new dao();
    List<student> list=L.select();
    for(int i=0;i<list.size();i++) {
    if(!list.get(i).getXuehao().equals(xuehao)||!list.get(i).getKecheng().equals(kecheng)||!list.get(i).getTeacher().equals(teacher)) {
    list.remove(i);
    i--;
    }
    }
    request.setAttribute("list", list);
    request.getRequestDispatcher("selectjieguo.jsp").forward(request, response);
    }
    private void nameselect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name=request.getParameter("name");
    String kecheng=request.getSession().getAttribute("course").toString();
    String teacher=request.getSession().getAttribute("teacher").toString();
    dao L=new dao();
    List<student> list=L.select();
    for(int i=0;i<list.size();i++) {
    if(!list.get(i).getName().equals(name)||!list.get(i).getKecheng().equals(kecheng)||!list.get(i).getTeacher().equals(teacher)) {
    list.remove(i);
    i--;
    }
    }
    request.setAttribute("list", list);
    request.getRequestDispatcher("selectjieguo.jsp").forward(request, response);
    }
    private void paixu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String kecheng=request.getSession().getAttribute("course").toString();
    String teacher=request.getSession().getAttribute("teacher").toString();
    String paixu=request.getParameter("paixu");
    dao L=new dao();
    int k;
    List<student> list=L.select();
    for(int i=0;i<list.size();i++) {
    if(!list.get(i).getTeacher().equals(teacher)||!list.get(i).getKecheng().equals(kecheng)) {
    list.remove(i);
    i--;
    }
    }
    if(list.size()>1)
    Collections.sort(list);
    if("sheng".equals(paixu)) {
    request.setAttribute("list", list);
    }
    else if("jiang".equals(paixu)) {
    List<student> list2=new ArrayList();
    for(int i=0;i<list.size();i++) {
    list2.add(list.get(list.size()-i-1));
    }
    request.setAttribute("list", list2);
    }
    request.getRequestDispatcher("paixu.jsp").forward(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("UTF-8");
    String method=request.getParameter("method");
    if("login".equals(method)) {
    login(request,response);
    }
    else if("course".equals(method)) {
    course(request,response);
    }
    else if("addstu".equals(method)) {
    addstu(request,response);
    }
    else if("deletestu".equals(method)) {
    deletestu(request,response);
    }
    else if("dengji".equals(method)) {
    dengji(request,response);
    }
    else if("update".equals(method)) {
    update(request,response);
    }
    else if("numselect".equals(method)) {
    numselect(request,response);
    }
    else if("nameselect".equals(method)) {
    nameselect(request,response);
    }
    else if("dengji2".equals(method)) {
    dengji2(request,response);
    }
    else if("paixu".equals(method)) {
    paixu(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 com.pp;

    public class student implements Comparable<student> {
    private String xuehao;
    private String name;
    private String banji;
    private String kecheng;
    private String teacher;
    private String score;
    public String getXuehao() {
    return xuehao;
    }
    public void setXuehao(String xuehao) {
    this.xuehao = xuehao;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getBanji() {
    return banji;
    }
    public void setBanji(String banji) {
    this.banji = banji;
    }
    public String getKecheng() {
    return kecheng;
    }
    public void setKecheng(String kecheng) {
    this.kecheng = kecheng;
    }
    public String getTeacher() {
    return teacher;
    }
    public void setTeacher(String teacher) {
    this.teacher = teacher;
    }
    public String getScore() {
    return score;
    }
    public void setScore(String score) {
    this.score = score;
    }
    public student(String xuehao, String name, String banji, String kecheng, String teacher, String score) {
    super();
    this.xuehao = xuehao;
    this.name = name;
    this.banji = banji;
    this.kecheng = kecheng;
    this.teacher = teacher;
    this.score = score;
    }
    @Override
    public int compareTo(student o) {
    int i = Integer.parseInt(this.getScore())-Integer.parseInt(o.getScore());
    return i;
    }
    }

  • 相关阅读:
    Qt Quick之QML与C++混合编程详解
    Qt QML与C++混合编程
    Qt QML和QtQuick简介以及QML实例
    Qt 渐变 QLinearGradient、 QConicalGradient、QRadialGradient
    Qt 加载HeightMap(高度图)构造3D地形图
    VisionPro内嵌脚本编译时显示:未定义类型“CogFindCircleTool”。未定义类型“CogToolResultConstants”
    VisionPro CogPMAlignTool
    VisionPro 卡尺原理
    VisionPro 卡尺测量长度的例子
    VisionPro 自学帮助
  • 原文地址:https://www.cnblogs.com/092e/p/14169124.html
Copyright © 2011-2022 走看看