首页 > 编程语言 >12.20 java实战2019年错题本系统(2)

12.20 java实战2019年错题本系统(2)

时间:2024-12-27 13:54:09浏览次数:3  
标签:preparedStatement java String 12.20 request 错题 sql import

今天继续2019年错题本系统的练习,昨天已经完成了除增删改查和页面设计的其他部分。
1.新增请假申请
Servlet(AddLeaveServlet)
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 java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

@WebServlet("/addLeave")
public class AddLeaveServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 读取数据库连接配置
Properties properties = new Properties();
try {
properties.load(getServletContext().getResourceAsStream("/WEB-INF/db.properties"));
String url = properties.getProperty("db.url");
String username = properties.getProperty("db.username");
String password = properties.getProperty("db.password");

        // 1. 建立数据库连接
        Connection connection = DriverManager.getConnection(url, username, password);

        // 2. 从请求中获取请假申请信息
        String studentId = request.getParameter("student_id");
        String studentName = request.getParameter("student_name");
        String gender = request.getParameter("gender");
        String grade = request.getParameter("grade");
        String college = request.getParameter("college");
        String major = request.getParameter("major");
        String className = request.getParameter("class_name");
        String leaveReason = request.getParameter("leave_reason");
        String leaveDateStr = request.getParameter("leave_date");
        java.sql.Date leaveDate = java.sql.Date.valueOf(leaveDateStr);

        // 3. 编写 SQL 插入语句
        String sql = "INSERT INTO leave_application_info (student_id, student_name, gender, grade, college, major, class_name, leave_reason, leave_date) VALUES (?,?,?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, studentId);
        preparedStatement.setString(2, studentName);
        preparedStatement.setString(3, gender);
        preparedStatement.setString(4, grade);
        preparedStatement.setString(5, college);
        preparedStatement.setString(6, major);
        preparedStatement.setString(7, className);
        preparedStatement.setString(8, leaveReason);
        preparedStatement.setDate(9, leaveDate);

        // 4. 执行插入操作
        int rowsAffected = preparedStatement.executeUpdate();
        if (rowsAffected > 0) {
            request.setAttribute("message", "请假申请新增成功");
        } else {
            request.setAttribute("message", "请假申请新增失败");
        }

        // 5. 关闭资源
        preparedStatement.close();
        connection.close();

        // 转发到结果页面展示消息
        request.getRequestDispatcher("/result.jsp").forward(request, response);
    } catch (SQLException | java.io.IOException e) {
        e.printStackTrace();
        request.setAttribute("message", "系统出现异常,请稍后再试");
        request.getRequestDispatcher("/result.jsp").forward(request, response);
    }
}

2.修改请假申请 Servlet(UpdateLeaveServlet):
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 java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

@WebServlet("/updateLeave")
public class UpdateLeaveServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Properties properties = new Properties();
try {
properties.load(getServletContext().getResourceAsStream("/WEB-INF/db.properties"));
String url = properties.getProperty("db.url");
String username = properties.getProperty("db.username");
String password = properties.getProperty("db.password");

        Connection connection = DriverManager.getConnection(url, username, password);

        int id = Integer.parseInt(request.getParameter("id"));
        String leaveReason = request.getParameter("leave_reason");
        String leaveDateStr = request.getParameter("leave_date");
        java.sql.Date leaveDate = java.sql.Date.valueOf(leaveDateStr);

        String sql = "UPDATE leave_application_info SET leave_reason =?, leave_date =? WHERE id =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, leaveReason);
        preparedStatement.setDate(2, leaveDate);
        preparedStatement.setInt(3, id);

        int rowsAffected = preparedStatement.executeUpdate();
        if (rowsAffected > 0) {
            request.setAttribute("message", "请假申请修改成功");
        } else {
            request.setAttribute("message", "请假申请修改失败");
        }

        preparedStatement.close();
        connection.close();

        request.getRequestDispatcher("/result.jsp").forward(request, response);
    } catch (SQLException | java.io.IOException e) {
        e.printStackTrace();
        request.setAttribute("message", "系统出现异常,请稍后再试");
        request.getRequestDispatcher("/result.jsp").forward(request, response);
    }
}

}
3.删除请假申请
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 java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

@WebServlet("/deleteLeave")
public class DeleteLeaveServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Properties properties = new Properties();
try {
properties.load(getServletContext().getResourceAsStream("/WEB-INF/db.properties"));
String url = properties.getProperty("db.url");
String username = properties.getProperty("db.username");
String password = properties.getProperty("db.password");

        Connection connection = DriverManager.getConnection(url, username, password);

        int id = Integer.parseInt(request.getParameter("id"));

        String sql = "DELETE FROM leave_application_info WHERE id =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id);

        int rowsAffected = preparedStatement.executeUpdate();
        if (rowsAffected > 0) {
            request.setAttribute("message", "请假申请删除成功");
        } else {
            request.setAttribute("message", "请假申请删除失败");
        }

        preparedStatement.close();
        connection.close();

        request.getRequestDispatcher("/result.jsp").forward(request, response);
    } catch (SQLException | java.io.IOException e) {
        e.printStackTrace();
        request.setAttribute("message", "系统出现异常,请稍后再试");
        request.getRequestDispatcher("/result.jsp").forward(request, response);
    }
}

}
4.查询请假申请
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 java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@WebServlet("/queryLeave")
public class QueryLeaveServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Properties properties = new Properties();
try {
properties.load(getServletContext().getResourceAsStream("/WEB-INF/db.properties"));
String url = properties.getProperty("db.url");
String username = properties.getProperty("db.username");
String password = properties.getProperty("db.password");

        Connection connection = DriverManager.getConnection(url, username, password);

        String keyword = request.getParameter("keyword");
        String sql = "SELECT student_name, college, major, class_name, leave_date, leave_reason FROM leave_application_info WHERE student_id LIKE? OR leave_reason LIKE? OR leave_date LIKE?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "%" + keyword + "%");
        preparedStatement.setString(2, "%" + keyword + "%");
        preparedStatement.setString(3, "%" + keyword + "%");

        ResultSet resultSet = preparedStatement.executeQuery();
        List<LeaveApplication> resultList = new ArrayList<>();
        while (resultSet.next()) {
            LeaveApplication application = new LeaveApplication();
            application.setStudentName(resultSet.getString("student_name"));
            application.setCollege(resultSet.getString("college"));
            application.setMajor(resultSet.getString("major"));
            application.setClassName(resultSet.getString("class_name"));
            application.setLeaveDate(resultSet.getDate("leave_date"));
            application.setLeaveReason(resultSet.getString("leave_reason"));
            resultList.add(application);
        }

        request.setAttribute("resultList", resultList);
        resultSet.close();
        preparedStatement.close();
        connection.close();

        request.getRequestDispatcher("/query_result.jsp").forward(request, response);
    } catch (SQLException | java.io.IOException e) {
        e.printStackTrace();
        request.setAttribute("message", "系统出现异常,请稍后再试");
        request.getRequestDispatcher("/result.jsp").forward(request, response);
    }
}

}
之后就是页面的设计
主页面

石家庄铁道大学请假条管理系统

石家庄铁道大学请假条管理系统

新增请假页面 新增请假申请

新增请假申请










修改申请页面 修改请假申请

修改请假申请

<%-- 这里需要先从数据库获取要修改的记录信息填充到表单中,以下是简单示意,实际需要结合 Servlet 等处理 --%>


完成后用tomcat在浏览器访问即可

标签:preparedStatement,java,String,12.20,request,错题,sql,import
From: https://www.cnblogs.com/bixi/p/18635550

相关文章

  • 12.16博客 备战java14
    今天学习增删改查中的修改功能,这部分是期末考试必须拿到的分数建立数据库连接。构建UPDATE语句的PreparedStatement对象,其中有两个占位符,第一个用于设置要更新的年龄值,第二个用于指定要修改的记录的ID(通过WHERE子句来限定条件)。执行executeUpdate方法进行修改操作,同样......
  • 12.13博客 备战java11
    今天学习单表的增删改查中的增,这是在期末考试中一定要拿到的分数首先通过DriverManager.getConnection方法根据配置信息建立与数据库的连接。构建INSERT语句的PreparedStatement对象,其中?是占位符,后续通过setString、setInt等方法按照顺序设置对应占位符的值,这样可以......
  • 12.14博客 备战java12
    今天学习增删改查单表中的查询操作同样先建立数据库连接。编写SELECT语句的PreparedStatement对象,这里查询所有字段(*),如果需要特定字段,可以修改查询语句,比如SELECTname,ageFROMstudents。通过executeQuery方法执行查询,得到ResultSet结果集对象,使用while(resultSe......
  • 12.15博客 备战java13
    今天学习单表的增删改查中的删除操作建立数据库连接。构建DELETE语句的PreparedStatement对象,通过占位符指定要删除记录的条件(这里以ID作为条件)。执行executeUpdate方法来执行删除操作,依据返回的受影响行数判断删除是否成功。关闭相关资源,完成整个删除操作流程。我写......
  • java 使用HttpClient发送@RequestBody类型的请求(解决中文乱码)
    如何使用java发送参数以@RequestBody类型接收到的请求。如下使用java的HttpClient写一个样例依赖<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.10</version></dependency>......
  • JAVA-Day 01:Hello,World!
    Hello,World!publicclassHello{publicstaticvoidmain(String[]args){System.out.print("Hello,world!");}}在idea中JAVA代码如下图片所示:运行结果如下图片所示:注意事项:"class"后的类名要和创建的JAVA文件名字相同;"main"方法中的"String"首字母&q......
  • 12.10博客 备战java8
    今天学习了JSP基础,为web的期末考试做准备JSP概念与语法:学习JSP(JavaServerPages)的概念,它是一种将Java代码嵌入到HTML页面中的技术,使得页面可以动态生成内容。了解JSP的基本语法,包括脚本元素(<%%>、<%=%>、<%@%>)、指令(page、include、taglib)、表达式语言(EL)等,能够在JS......
  • 12.11博客 备战java9
    今天学习了JSP与Servlet整合的内容Model-View-Controller(MVC)模式介绍:了解MVC设计模式在JavaWeb开发中的应用,Servlet作为控制器(Controller)负责接收和处理请求,JSP作为视图(View)负责展示数据,JavaBean或其他数据访问对象作为模型(Model)负责处理业务逻辑和数据存储。理解这种......
  • 12.12博客 备战java10
    今天学习了mysql数据库连接与操作以及数据表的建立,这一部分在期末考试中占比五分JDBC基础:了解JDBC(JavaDatabaseConnectivity)的概念和作用,它是Java语言连接数据库的标准API。学习使用JDBC连接数据库的基本步骤,包括加载数据库驱动(如MySQL的com.mysql.cj.jdbc.Driver)、......
  • 12.3博客 备战java3
    今天我学习了HTML、CSS基础,以便于更好地进行页面设计HTML基础:学习HTML(超文本标记语言)的基本结构,包括声明、、、等标签的作用和用法。掌握常用的HTML标签,如标题标签(-)、段落标签()、链接标签()、图片标签()、列表标签(、、)等,能够使用这些标签构建简单的静态网页结......