You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
library_manage_system/src/servlet/manager/AnnouncementAdd.java

79 lines
3.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package servlet.manager;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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 javabean.Base;
import javabean.DateTime;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Servlet implementation class AnnouncementAdd
*/
@WebServlet("/manager/announcementAdd") // 定义Servlet的URL映射路径
public class AnnouncementAdd extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json; charset=utf8"); // 设置响应内容类型和字符编码
// 接收参数
String id = req.getParameter("id"); // 获取请求中的id参数
String tit = req.getParameter("title"); // 获取请求中的title参数
String det = req.getParameter("detail"); // 获取请求中的detail参数
DateTime date = new DateTime(); // 创建DateTime对象用于获取当前时间
String time = date.show(); // 获取当前时间的字符串表示
// 准备参数
Connection connection = null; // 数据库连接对象
PreparedStatement pstmt = null; // SQL预编译语句对象
ResultSet resultSet = null; // 结果集对象
int result = 0; // 执行更新操作的结果
int count = 0; // 记录影响的行数
// 返回参数
int code = 1; // 状态码默认为1表示失败
String msg = ""; // 消息提示
JSONArray jsonArray = new JSONArray(); // JSON数组用于存储所有记录的数据
JSONObject json = new JSONObject(); // JSON对象用于存储单条记录的数据
String sql = "insert into announcement(title, detail, publish_date) values(?,?,?)"; // SQL插入语句
System.out.println(sql); // 打印SQL语句到控制台
PrintWriter out = resp.getWriter(); // 获取响应输出流
try {
connection = (Connection) Base.getConnection(); // 获取数据库连接
pstmt = connection.prepareStatement(sql); // 创建预编译语句对象
pstmt.setString(1, tit); // 设置第一个参数为公告标题
pstmt.setString(2, det); // 设置第二个参数为公告详情
pstmt.setString(3, time); // 设置第三个参数为公告发布时间
result = pstmt.executeUpdate(); // 执行更新操作并获取影响的行数
} catch (SQLException e) { // 捕获SQL异常
} catch (ClassNotFoundException e) { // 捕获类未找到异常
e.printStackTrace(); // 打印堆栈跟踪信息
} finally {
try {
Base.closeResource(connection, pstmt, null); // 关闭数据库资源
} catch (SQLException e) { // 捕获关闭资源时的SQL异常
e.printStackTrace(); // 打印堆栈跟踪信息
}
}
if (result == 1) { // 如果插入操作成功
json.put("code", "0"); // 设置状态码为0表示成功
json.put("msg", "success"); // 设置消息提示为"success"
} else { // 如果插入操作失败
json.put("code", "1"); // 设置状态码为1表示失败
json.put("msg", "error"); // 设置消息提示为"error"
}
out.write(json.toString()); // 输出JSON响应
}
}