增加insertcategory接口

This commit is contained in:
cyk
2023-07-11 13:35:05 +08:00
parent ccbee742fd
commit 21101cc287
3 changed files with 60 additions and 0 deletions

View File

@@ -120,4 +120,18 @@ public class categoryDao {
}
}
public int insertCategory(String name , String url , int isValid )
{
int num ;
try {
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql="insert into category (category,categoryName,isValid) VALUES(?,?,?)";
num = runner.update(conn,sql,name,url,isValid);
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return num;
}
}

View File

@@ -37,4 +37,7 @@ public class categoryService {
}
return JSONObject.toJSONString(projectCategoryDaoList);
}
public int insertCategory(String name , String url , String isValid ){
return categoryDao.insertCategory(name,url,Integer.parseInt(isValid));
}
}

View File

@@ -0,0 +1,43 @@
package com.hellogithub.servlet.systemAdmin;
import com.hellogithub.service.categoryService;
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.io.PrintWriter;
@WebServlet("/insertCategory")
public class insertCategoryServlet extends HttpServlet {
private categoryService categoryService = new categoryService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=utf-8");
// 设置响应头允许ajax跨域访问
String curOrigin = req.getHeader("Origin");
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
resp.setHeader("Access-Control-Allow-Credentials", "true");
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
resp.setHeader("Access-Control-Max-Age", "3600");
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
resp.setContentType("application/json;charset=UTF-8");
String name = req.getParameter("name");
String url = req.getParameter("url");
String isValid = req.getHeader("isValid");
int num = categoryService.insertCategory(name,url,isValid);
PrintWriter writer = resp.getWriter();
if(num == 0)
{
writer.println("wrong");
}
}
}