|
|
package servlet.admin;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.sql.PreparedStatement;
|
|
|
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 java.sql.Connection;
|
|
|
|
|
|
import javabean.Base;
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
|
|
/**
|
|
|
* Servlet 实现类,用于管理员添加书籍
|
|
|
*
|
|
|
* 该 Servlet 处理管理员提交的添加书籍请求,将书籍信息(书名、作者、馆藏ID、类别ID、位置、状态、描述)插入到数据库中。
|
|
|
* 插入成功后,返回 JSON 格式的成功信息;失败则返回错误信息。
|
|
|
*/
|
|
|
@WebServlet("/admin/bookAdd")
|
|
|
public class BookAdd extends HttpServlet {
|
|
|
|
|
|
/**
|
|
|
* 处理 POST 请求,执行书籍的添加操作
|
|
|
*
|
|
|
* 该方法接收客户端提交的书籍信息(通过 HTTP 请求),然后将这些信息插入到数据库中。
|
|
|
* 插入操作通过 JDBC 进行,如果插入成功,返回一个包含成功状态的 JSON 响应;如果失败,则返回错误信息。
|
|
|
*
|
|
|
* @param req 请求对象
|
|
|
* @param resp 响应对象
|
|
|
* @throws ServletException 如果发生 Servlet 错误
|
|
|
* @throws IOException 如果发生 I/O 错误
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置响应内容类型为 JSON,字符编码为 UTF-8
|
|
|
resp.setContentType("application/json; charset=utf8");
|
|
|
|
|
|
// 获取请求参数(书籍信息)
|
|
|
String name = req.getParameter("name"); // 书名
|
|
|
String author = req.getParameter("author"); // 作者
|
|
|
String library_id = req.getParameter("library_id"); // 所属馆ID
|
|
|
String sort_id = req.getParameter("sort_id"); // 书籍类别ID
|
|
|
String position = req.getParameter("position"); // 书籍位置
|
|
|
String status = req.getParameter("status"); // 书籍状态
|
|
|
String description = req.getParameter("description"); // 书籍描述
|
|
|
|
|
|
// 用于调试输出描述字段的内容
|
|
|
System.out.println(description + "-------------"); // debug
|
|
|
|
|
|
// 创建 JSON 对象用于存储响应数据
|
|
|
JSONObject json = new JSONObject();
|
|
|
Connection connection = null; // 数据库连接
|
|
|
PreparedStatement pstmt = null; // SQL 执行对象
|
|
|
int result = 0; // 记录 SQL 执行结果(影响的行数)
|
|
|
|
|
|
// SQL 插入语句
|
|
|
String sql = "insert into books(name, author, library_id, sort_id, position, status, description) values(?,?,?,?,?,?,?)";
|
|
|
System.out.println(sql); // debug,输出 SQL 语句
|
|
|
|
|
|
// 获取响应输出流
|
|
|
PrintWriter out = resp.getWriter();
|
|
|
|
|
|
try {
|
|
|
// 获取数据库连接
|
|
|
connection = (Connection) Base.getConnection();
|
|
|
|
|
|
// 创建 PreparedStatement 对象
|
|
|
pstmt = connection.prepareStatement(sql);
|
|
|
|
|
|
// 设置 SQL 语句中的参数
|
|
|
pstmt.setString(1, name);
|
|
|
pstmt.setString(2, author);
|
|
|
pstmt.setString(3, library_id);
|
|
|
pstmt.setString(4, sort_id);
|
|
|
pstmt.setString(5, position);
|
|
|
pstmt.setString(6, status);
|
|
|
pstmt.setString(7, description);
|
|
|
|
|
|
// 执行 SQL 插入操作,返回影响的行数
|
|
|
result = pstmt.executeUpdate();
|
|
|
} catch (SQLException e) {
|
|
|
// 捕获 SQL 异常(可以在这里做日志记录或处理)
|
|
|
e.printStackTrace();
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
// 捕获 ClassNotFoundException 异常(数据库驱动类未找到)
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
// 关闭数据库资源
|
|
|
try {
|
|
|
Base.closeResource(connection, pstmt, null);
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace(); // 关闭资源时发生异常
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 判断插入操作是否成功,返回相应的 JSON 数据
|
|
|
if (result == 1) {
|
|
|
// 插入成功,返回成功的 JSON 响应
|
|
|
json.put("code", "0"); // code = 0 表示成功
|
|
|
json.put("msg", "success"); // 返回成功消息
|
|
|
} else {
|
|
|
// 插入失败,返回错误的 JSON 响应
|
|
|
json.put("code", "1"); // code = 1 表示失败
|
|
|
json.put("msg", "error"); // 返回失败消息
|
|
|
}
|
|
|
|
|
|
// 将 JSON 响应写入输出流
|
|
|
out.write(json.toString());
|
|
|
}
|
|
|
}
|