package com.nic.dao;
import com.nic.model.Category;
import com.nic.model.MainCategory;
import com.nic.model.SubCategory;
import com.nic.util.JdbcUtil;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class CategoryDao {
/**
* 获取所有分类的信息
* @return 分类查询结果的列表
*/
public List<Category> getCategory(Category searchModel){
List<Category> result = new ArrayList<Category>();
// 存放查询参数
List<Object> paramList = new ArrayList<Object>();
int main_id = searchModel.getMain_id();
int sub_id = searchModel.getSub_id();
StringBuilder sql = new StringBuilder("select m.*,s.id sub_id,s.name sub_name," +
"(select count(id) from article where main_id = s.main_id and sub_id = s.id) sub_count " +
"from maincategory m,subcategory s where m.id = s.main_id");
if (main_id != 0 ) {
sql.append(" and s.main_id = ?");
paramList.add(main_id);
}
if (sub_id != 0 ) {
sql.append(" and s.sub_id = ?");
paramList.add(sub_id);
}
JdbcUtil jdbcUtil = null;
try {
jdbcUtil = new JdbcUtil();
jdbcUtil.getConnection(); // 获取数据库链接
List<Map<String, Object>> categoryResult = jdbcUtil.findResult(sql.toString(), paramList);
if (categoryResult != null) {
for (Map<String, Object> map : categoryResult) {
Category s = new Category(map);
result.add(s);
}
}
} catch (SQLException e) {
throw new RuntimeException("查询所有数据异常!", e);
} finally {
if (jdbcUtil != null) {
jdbcUtil.releaseConn(); // 释放资源
}
}
return result;
}
/**
* 获取所有主分类的列表
* @return 主分类查询结果的列表
*/
public List<MainCategory> getMaincategory(){
JdbcUtil jdbcUtil = new JdbcUtil();
jdbcUtil.getConnection(); // 获取数据库链接
StringBuilder sql = new StringBuilder("select *,(select count(*) from article where main_id = m.id) main_count " +
"from maincategory m;");
List<Map<String, Object>> CatetoryList = null;
List<MainCategory> result = new ArrayList<MainCategory>();
List<SubCategory> sublist = null;
try {
CatetoryList = jdbcUtil.findResult(sql.toString(),new ArrayList<>());
if (CatetoryList!=null){
for (Map<String, Object> map : CatetoryList) {
MainCategory catetory = new MainCategory(map);
sublist = getSubcategory(Integer.parseInt(map.get("id").toString()));
catetory.setSublist(sublist);
result.add(catetory);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 获取选中主分类对应的二级分类
* @return 二级分类查询结果的列表
*/
public List<SubCategory> getSubcategory(int main_id){
JdbcUtil jdbcUtil = new JdbcUtil();
jdbcUtil.getConnection(); // 获取数据库链接
StringBuilder sql = new StringBuilder("select *,(select count(*) from article where sub_id = s.id) sub_count" +
" from subcategory s where 1=1 ");
List<Object> paramList = new ArrayList<Object>();
List<Map<String, Object>> subCategoryList = null;
List<SubCategory> result = new ArrayList<SubCategory>();
if (main_id != 0 ) {
sql.append(" and main_id = ?");
paramList.add(main_id);
}
try {
subCategoryList = jdbcUtil.findResult(sql.toString(),paramList);
if (subCategoryList!=null){
for (Map<String, Object> map : subCategoryList) {
result.add(new SubCategory(map));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 添加主分类*/
public boolean addMainCategory(MainCategory mainCategory){
boolean result = false;
StringBuilder sql =
new StringBuilder("insert into maincategory(name) values(?);");
JdbcUtil jdbcUtil = null;
List<Object> paramList = new ArrayList<Object>();
paramList.add(mainCategory.getName());
try {
jdbcUtil = new JdbcUtil();
jdbcUtil.getConnection(); // 获取数据库连接
result = jdbcUtil.updateByPreparedStatement(sql.toString(), paramList);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 添加二级分类*/
public boolean addSubCategory(SubCategory subCategory){
boolean result = false;
StringBuilder sql =
new StringBuilder("insert into subcategory(name,main_id) values(?,?);");
JdbcUtil jdbcUtil = null;
try {
jdbcUtil = new JdbcUtil();
jdbcUtil.getConnection(); // 获取数据库连接
result = jdbcUtil.updateByPreparedStatement(sql.toString(),subCategory.toList());
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 修改主分类*/
public boolean updateMainCategory(MainCategory mainCategory){
boolean result = false;
StringBuilder sql =
new StringBuilder("update maincategory set name = ? where id = ? ");
List<Object> paramlist = new ArrayList<Object>();
paramlist.add(mainCategory.getName());
paramlist.add(mainCategory.getId());
JdbcUtil jdbcUtil = null;
try {
jdbcUtil = new JdbcUtil();
jdbcUtil.getConnection(); // 获取数据库连接
result = jdbcUtil.updateByPreparedStatement(sql.toString(),paramlist);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 修改二级分类*/
public boolean updateSubCategory(SubCategory subCategory){
boolean result = false;
StringBuilder sql =
new StringBuilder("update subcategory set name = ?,main_id = ? where id = ? ");
List paramlist = new ArrayList();
paramlist.add(subCategory.getName());
paramlist.add(subCategory.getMain_id());
paramlist.add(subCategory.getId());
JdbcUtil jdbcUtil = null;
try {
jdbcUtil = new JdbcUtil();
jdbcUtil.getConnection(); // 获取数据库连接
result = jdbcUtil.updateByPreparedStatement(sql.toString(),paramlist);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 删除主分类*/
public boolean deleteMainCategory(int id){
boolean result = false;
StringBuilder sql =
new StringBuilder("delete from maincategory where id = ?;");
List<Object> paramlist = new ArrayList<Object>();
paramlist.add(id);
JdbcUtil jdbcUtil = null;
try {
jdbcUtil = new JdbcUtil();
jdbcUtil.getConnection(); // 获取数据库连接
result = jdbcUtil.updateByPreparedStatement(sql.toString(),paramlist);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 删除二级分类*/
public boolean deleteSubCategory(int id){
boolean result = false;
StringBuilder sql =
new StringBuilder("delete from subcategory where id = ?;");
List paramlist = new ArrayList();
paramlist.add(id);
JdbcUtil jdbcUtil = null;
没有合适的资源?快使用搜索试试~ 我知道了~
jsp+servlet实现的个人博客系统jsp-blog.zip
共456个文件
js:197个
html:103个
css:58个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 196 浏览量
2024-04-29
23:38:29
上传
评论
收藏 3.25MB ZIP 举报
温馨提示
【博客个人资源】 包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。 【技术】 Java、Python、Node.js、Spring Boot、Django、Express、MySQL、PostgreSQL、MongoDB、React、Angular、Vue、Bootstrap、Material-UI、Redis、Docker、Kubernetes
资源推荐
资源详情
资源评论
收起资源包目录
jsp+servlet实现的个人博客系统jsp-blog.zip (456个子文件)
AUTHORS 6KB
CategoryDao.class 7KB
ArticleDao.class 6KB
JdbcUtil.class 5KB
CategoryServlet.class 5KB
Article.class 5KB
PosteditServlet.class 3KB
PostlistServlet.class 3KB
CategoryEditServlet.class 3KB
AdminFilter.class 3KB
ImgServlet.class 3KB
SubCategory.class 2KB
MainCategory.class 2KB
LoginServlet.class 2KB
Category.class 2KB
Pager.class 2KB
CategoryService.class 2KB
UserDao.class 2KB
ArticleService.class 1KB
User.class 877B
UserService.class 614B
Test.class 456B
bootstrap.min.css 136KB
editormd.min.css 60KB
font-awesome.min.css 30KB
ambiance.css 26KB
codemirror.css 8KB
codemirror.min.css 5KB
mdn-like.css 5KB
solarized.css 5KB
blog.css 4KB
merge.css 3KB
lint.css 3KB
xq-dark.css 3KB
lesser-dark.css 2KB
prism.css 2KB
pastel-on-dark.css 2KB
xq-light.css 2KB
tomorrow-night-eighties.css 2KB
erlang-dark.css 2KB
zenburn.css 2KB
twilight.css 2KB
midnight.css 2KB
vibrant-ink.css 2KB
mbo.css 2KB
base16-dark.css 2KB
base16-light.css 2KB
tern.css 2KB
3024-night.css 2KB
paraiso-dark.css 2KB
paraiso-light.css 2KB
tomorrow-night-bright.css 2KB
3024-day.css 2KB
blackboard.css 2KB
colorforth.css 2KB
the-matrix.css 2KB
night.css 2KB
rubyblue.css 2KB
monokai.css 1KB
cobalt.css 1KB
simplescrollbars.css 1KB
project.css 1KB
eclipse.css 1KB
manage.css 989B
neo.css 932B
about.css 869B
elegant.css 768B
neat.css 693B
pagination.css 664B
show-hint.css 662B
page.css 515B
dialog.css 502B
index.css 485B
tiki.css 440B
foldgutter.css 435B
login.css 336B
tiddlywiki.css 220B
matchesonscrollbar.css 188B
fullscreen.css 116B
ambiance-mobile.css 103B
fontawesome-webfont.eot 162KB
glyphicons-halflings-regular.eot 20KB
Glyphter.eot 11KB
.gitignore 36B
scala.html 28KB
index.html 22KB
index.html 17KB
index.html 13KB
index.html 13KB
index.html 11KB
index.html 10KB
index.html 9KB
index.html 8KB
index.html 8KB
index.html 7KB
index.html 7KB
contact.html 6KB
index.html 6KB
index.html 6KB
index.html 6KB
共 456 条
- 1
- 2
- 3
- 4
- 5
资源评论
枫蜜柚子茶
- 粉丝: 7333
- 资源: 5102
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功