软件工程日报——实现了换班的功能
今天我在课上终于实现了排班换班的功能
我一开始的思路是通过一个mysql语句来直接实现数据表中两行数据的调换,但是在经过两天的尝试下,这个方案被我放弃了,因为我的数据表和语句的问题,sql语句交换行数据的操作在我的数据表中并不能实现,于是今天我尝试换了另外一种思路,我试着利用两条sql语句先获取数据,再进行数据更近的方式来进行转换,在尝试修改完各种数据后,成功实现了这个功能
<%@ page import="java.sql.Connection" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.ResultSet" %> <%@ page import="java.sql.SQLException" %><%-- Created by IntelliJ IDEA. User: JC Date: 2023/5/15 Time: 17:14 To change this template use File | Settings | File Templates. --%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <jsp:useBean id="util" class="test.scheduling" scope="page" /> <% String id1=(String) session.getAttribute("id"); String id2=(String)request.getParameter("m"); if (id1.equals(id2)) { out.print("<script language='javaScript'> alert('数据错误');</script>"); response.setHeader("refresh", "0;url=schedule.jsp"); } else { Connection connection = util.getConnection(); String a = null; String b = null; PreparedStatement preparedStatement = null; PreparedStatement pr = null; ResultSet rs = null; try { String sql = "select * from schedule "; preparedStatement = connection.prepareStatement(sql); rs = preparedStatement.executeQuery(); while (rs.next()) { String s1 = rs.getObject(1).toString(); if (s1.equals(id1)) { a = rs.getObject(2).toString(); } if (s1.equals(id2)) { b = rs.getObject(2).toString(); } } String sql1 = "UPDATE schedule\n" + "set e_id = CASE\n" + " WHEN id = ? THEN ?\n" + " WHEN id = ? THEN ?\n" + " ELSE e_id\n" + "END\n" + "WHERE id IN (?, ?);"; pr = connection.prepareStatement(sql1); pr.setString(1, id1); pr.setString(2, b); pr.setString(3, id2); pr.setString(4, a); pr.setString(5, id1); pr.setString(6, id2); int count = pr.executeUpdate(); if (count > 0) { out.print("<script language='javaScript'> alert('换班成功');</script>"); response.setHeader("refresh", "0;url=schedule.jsp"); } else { out.print("<script language='javaScript'> alert('换班失败');</script>"); response.setHeader("refresh", "0;url=schedule.jsp"); } } catch (SQLException e) { e.printStackTrace(); } finally { util.close(rs); util.close(preparedStatement); util.close(connection); } out.print("<script language='javaScript'> alert('换班成功');</script>"); response.setHeader("refresh", "0;url=schedule.jsp"); } %> </body> </html>
实现效果如下
换班前
换班后