当前位置: 首页 > news >正文

javaweb综合训练

1.创建表t_student,其中包含学号stuno,姓名stuname,性别stusex三个字段,在表中插入一些测试数据。

2.实现下述功能:

  1. 编写显示所有学生资料信息页面,其中包含一个表单用来根据学生姓名模糊查询匹配的学生信息。
  2. 在学生信息后面增加一个“删除学生信息”链接,单击链接可以将学生信息从数据库中删除,然后跳转到显示所有学生资料信息页面
  3. 部分代码:

package com.lab.model;

public class Student {

    private String stuno;

    private String stuname;

    private String stusex;

    public Student() {

    }

    public Student(String stuno, String stuname, String stusex) {

        this.stuno = stuno;

        this.stuname = stuname;

        this.stusex = stusex;

    }

    public String getStuno() {

        return stuno;

    }

    public void setStuno(String stuno) {

        this.stuno = stuno;

    }

    public String getStuname() {

        return stuname;

    }

    public void setStuname(String stuname) {

        this.stuname = stuname;

    }

    public String getStusex() {

        return stusex;

    }

    public void setStusex(String stusex) {

        this.stusex = stusex;

    }

    @Override

    public String toString() {

        return "Student [stuno=" + stuno + ", stuname=" + stuname + ", stusex=" + stusex + "]";

    }

}

package com.lab.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import com.lab.model.Student;

import com.lab.util.DatabaseBean;

public class StudentDao {

    public List<Student> queryAllStudents() {

        List<Student> students = new ArrayList<>();

        String sql = "select * from t_student";

        try (Connection conn = DatabaseBean.getConnection();

             PreparedStatement psmt = conn.prepareStatement(sql);

             ResultSet rs = psmt.executeQuery()) {

            while (rs.next()) {

                Student stu = new Student();

                stu.setStuno(rs.getString("stuno"));

                stu.setStuname(rs.getString("stuname"));

                stu.setStusex(rs.getString("stusex"));

                students.add(stu);

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }

        return students;

    }

    public List<Student> queryAllStudents(String stuname) {

        List<Student> students = new ArrayList<>();

        String sql = "select * from t_student where stuname like?";

        try (Connection conn = DatabaseBean.getConnection();

             PreparedStatement psmt = conn.prepareStatement(sql)) {

            psmt.setString(1, "%" + stuname + "%");

            try (ResultSet rs = psmt.executeQuery()) {

                while (rs.next()) {

                    Student stu = new Student();

                    stu.setStuno(rs.getString("stuno"));

                    stu.setStuname(rs.getString("stuname"));

                    stu.setStusex(rs.getString("stusex"));

                    students.add(stu);

                }

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }

        return students;

    }

    public boolean deleteStudent(String stuno) {

        String sql = "delete from t_student where stuno=?";

        try (Connection conn = DatabaseBean.getConnection();

             PreparedStatement psmt = conn.prepareStatement(sql)) {

            psmt.setString(1, stuno);

            int result = psmt.executeUpdate();

            return result > 0;

        } catch (SQLException e) {

            e.printStackTrace();

            return false;

        }

    }

    public boolean addStudent(Student stu) {

        String sql = "insert into t_student values(?,?,?)";

        try (Connection conn = DatabaseBean.getConnection();

             PreparedStatement psmt = conn.prepareStatement(sql)) {

            psmt.setString(1, stu.getStuno());

            psmt.setString(2, stu.getStuname());

            psmt.setString(3, stu.getStusex());

            int result = psmt.executeUpdate();

            return result > 0;

        } catch (SQLException e) {

            e.printStackTrace();

            return false;

        }

    }

    public boolean updateStudent(Student stu) {

        String sql = "update t_student set stuname=?,stusex=? where stuno=?";

        try (Connection conn = DatabaseBean.getConnection();

             PreparedStatement psmt = conn.prepareStatement(sql)) {

            psmt.setString(1, stu.getStuname());

            psmt.setString(2, stu.getStusex());

            psmt.setString(3, stu.getStuno());

            int result = psmt.executeUpdate();

            return result > 0;

        } catch (SQLException e) {

            e.printStackTrace();

            return false;

        }

    }

    public Student queryStudent(String stuno) {

        Student stu = null;

        String sql = "select * from t_student where stuno=?";

        try (Connection conn = DatabaseBean.getConnection();

             PreparedStatement psmt = conn.prepareStatement(sql)) {

            psmt.setString(1, stuno);

            try (ResultSet rs = psmt.executeQuery()) {

                if (rs.next()) {

                    stu = new Student();

                    stu.setStuno(rs.getString("stuno"));

                    stu.setStuname(rs.getString("stuname"));

                    stu.setStusex(rs.getString("stusex"));

                }

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }

        return stu;

    }

}

package com.lab.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class DatabaseBean {

    public static Connection getConnection() {

        Connection conn = null;

        try {

            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

            String path = "C:/Users/李心洁/Documents/Database6.accdb";

            String url = "jdbc:ucanaccess://" + path;

            conn = DriverManager.getConnection(url);

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        } catch (SQLException e) {

            e.printStackTrace();

        }

        return conn;

    }

    public static void close(ResultSet rs, PreparedStatement psmt, Connection conn) {

        try {

            if (rs!= null) {

                rs.close();

            }

            if (psmt!= null) {

                psmt.close();

            }

            if (conn!= null) {

                conn.close();

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }

}

package com.lab.action;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import org.apache.struts2.interceptor.ServletResponseAware;

import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class SuperAciton extends ActionSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware {

    private static final long serialVersionUID = 1L;

    protected HttpServletRequest request;

    protected HttpServletResponse response;

    protected HttpSession session;

    protected ServletContext application;

    @Override

    public void setServletContext(ServletContext application) {

        this.application = application;

    }

    @Override

    public void setServletResponse(HttpServletResponse response) {

        this.response = response;

    }

    @Override

    public void setServletRequest(HttpServletRequest request) {

        this.request = request;

        this.session = request.getSession();

    }

}

package com.lab.action;

import java.util.List;

import com.lab.dao.StudentDao;

import com.lab.model.Student;

public class StudentAction extends SuperAciton {

    private StudentDao studentDao = new StudentDao();

    public String find() {

        List<Student> students = studentDao.queryAllStudents();

        request.setAttribute("students", students);

        return "find_success";

    }

    public String query() {

        String stuname = request.getParameter("stuname");

        List<Student> students = studentDao.queryAllStudents(stuname);

        request.setAttribute("students", students);

        return "query_success";

    }

    public String delete() {

        String stuno = request.getParameter("stuno");

        studentDao.deleteStudent(stuno);

        return "delete_success";

    }

    public String add() {

        String stuno = request.getParameter("stuno");

        String stuname = request.getParameter("stuname");

        String stusex = request.getParameter("stusex");

        Student stu = new Student(stuno, stuname, stusex);

        boolean flag = studentDao.addStudent(stu);

        if (flag) {

            return "add_success";

        } else {

            return "add_failure";

        }

    }

    public String update() {

        String stuno = request.getParameter("stuno");

        Student student = studentDao.queryStudent(stuno);

        request.setAttribute("student", student);

        return "update_success";

    }

    public String save() {

        String stuno = request.getParameter("stuno");

        String stuname = request.getParameter("stuname");

        String stusex = request.getParameter("stusex");

        Student stu = new Student(stuno, stuname, stusex);

        boolean flag = studentDao.updateStudent(stu);

        if (flag) {

            return "save_success";

        } else {

            return "save_failure";

        }

    }

}

<%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>首页面</title>

</head>

<body>

    首页面

    <a href="find.action">查询所有学生数据</a>

</body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>查询所有学生数据</title>

</head>

<body>

    <form action="query.action" method="get">

        请输入要查询的学生姓名(支持模糊查询) <input type="text" name="stuname"

                                              placeholder="学生姓名"> <input type="submit" value="查询">

    </form>

    <table>

        <tr>

            <td>学号</td>

            <td>姓名</td>

            <td>性别</td>

            <td>操作</td>

        </tr>

        <c:forEach items="${students }" var="stu">

            <tr>

                <td>${stu.stuno }</td>

                <td>${stu.stuname }</td>

                <td>${stu.stusex }</td>

                <td><a href="delete.action?stuno=${stu.stuno }">删除</a> <a

                        href="update.action?stuno=${stu.stuno }">修改</a></td>

            </tr>

        </c:forEach>

    </table>

    <a href="student_add.jsp">添加学生数据</a>

</body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>添加学生数据</title>

</head>

<body>

    <form action="add.action" method="post">

        学号: <input type="text" name="stuno" required><br>

        姓名: <input type="text" name="stuname" required><br>

        性别: <input type="text" name="stusex" required><br>

        <input type="submit" value="添加">

    </form>

</body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>修改学生数据</title>

</head>

<body>

    <form action="save.action" method="post">

        <input type="hidden" name="stuno" value="${student.stuno }">

        姓名: <input type="text" name="stuname" value="${student.stuname }" required><br>

        性别: <input type="text" name="stusex" value="${student.stusex }" required><br>

        <input type="submit" value="保存">

    </form>

</body>

</html>

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <action name="index">

            <result>index.jsp</result>

        </action>

        <action name="*" class="com.lab.action.StudentAction" method="{1}">

            <result name="find_success">student_find.jsp</result>

            <result name="query_success">student_find.jsp</result>

            <result name="delete_success" type="redirectAction">find</result>

            <result name="add_success" type="redirectAction">find</result>

            <result name="add_failure" type="redirect">student_add.jsp</result>

            <result name="update_success">student_update.jsp</result>

            <result name="save_success" type="redirectAction">find</result>

            <result name="save_failure" type="chain">update</result>

        </action>

    </package>

</struts>

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

    <display-name>StudentManagement</display-name>

    <filter>

        <filter-name>struts2</filter-name>

        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>struts2</

 

 

相关文章:

  • CExercise_10_1动态数组Vector
  • scrapy爬虫框架采集完成后进行暂停延时关闭方法
  • 难度偏低,25西电人工智能学院821、833、834考研录取情况
  • Java常用工具算法-7--秘钥托管云服务2(阿里云 KMS)
  • python办公自动化------邮件发送
  • SAQ评级是什么,SAQ评级的意义?对企业发展好处
  • transformers 中的 input_ids 和 labels 是什么
  • 一个Linux/Java乱码问题的解决
  • Express中间件(Middleware)详解:从零开始掌握(2)
  • 使用Windows工具进行内存取证(不进行完全内存转储)
  • C语言:位段
  • 【后端分享】SpringBoot实现接口防刷的5种实现方案!
  • 微软Exchange管理中心全球范围宕机
  • centos-stream-9上安装nvidia驱动和cuda-toolkit
  • C++中std::move的高级应用示例
  • Robot---SPLITTER行星探测机器人
  • VS Code构建C/C++开发环境(Windows with MinGW and CMake)
  • Qt学习笔记——TableWidget的一些学习东西
  • 精品推荐-最新大模型MCP核心架构及最佳实践资料合集(18份).zip
  • Named Entity Recognition with Bidirectional LSTM-CNNs(于双向LSTM神经网络的命名实体识别)论文阅读
  • 网站怎样做的高大上/成人职业技术培训学校
  • 做外贸的网站有那些/怎么优化
  • 广州市网站建设 乾图信息科技/app注册推广
  • 北京医疗网站建设公司/西安seo排名外包
  • 廊坊网站优化/百度100%秒收录
  • 怎么做卖卷网站/如何进行网站推广