📄 topiccategoryserviceimpl.java
字号:
package com.ql.bbs.service.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import com.ql.bbs.model.TopicCategory;
import com.ql.bbs.model.User;
import com.ql.bbs.service.TopicCategoryService;
import com.ql.bbs.util.JdbcUtils;
public class TopicCategoryServiceImpl implements TopicCategoryService {
/**
* 增加类别
*/
public boolean addTopicCategory(TopicCategory category) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
//插入新的类别
String sql1 = "insert into t_category(name ,description ,parentId,isLeaf,grade) "+
"values(?,?,?,?,?)";
//查找父ID
String sql2 = "select name from t_category where id = ?";
//更新父类别的叶子标识
String sql3 = "update t_category set isLeaf = 0 where id = ?";
try {
con = JdbcUtils.getInstance().getConnection();
//设置自动提交事务为F
con.setAutoCommit(false);
//插入新的类别
pstmt = con.prepareStatement(sql1);
pstmt.setString(1, category.getName());
pstmt.setString(2, category.getDescrption());
pstmt.setInt(3, category.getPid());
pstmt.setInt(4, 1);
pstmt.setInt(5, category.getGrade());
pstmt.executeUpdate();
//查找父ID
pstmt = con.prepareStatement(sql2);
pstmt.setInt(1, category.getPid());
rs = pstmt.executeQuery();
if (rs.next()) {
//更新父类别的叶子标识
pstmt = con.prepareStatement(sql3);
pstmt.setInt(1, category.getPid());
pstmt.executeUpdate();
}
//提交事务
con.commit();
con.setAutoCommit(true);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
JdbcUtils.getInstance().close(con, pstmt, rs);
}
return true;
}
/**
* 删除类别
* 删除子类别的时候,父类别的isleaf是否要变----------->这点是否要完成???
*/
public boolean delTopicCategory(int id) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "delete from t_category where id = ?";
try {
con = JdbcUtils.getInstance().getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
JdbcUtils.getInstance().close(con, pstmt, rs);
}
return true;
}
/**
* 查找类别
*/
public List findTopicCategories() {
// TODO Auto-generated method stub
return null;
}
/**
* 按ID查找类别
*/
public TopicCategory findTopicCategory(int id) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
TopicCategory topicCategory = null;
String sql = "select name ,description ,parentId,isLeaf,grade " +
"from t_category where id=?";
try {
con = JdbcUtils.getInstance().getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
topicCategory = new TopicCategory();
topicCategory.setId(id);
topicCategory.setName(rs.getString("name"));
topicCategory.setDescrption(rs.getString("descrption"));
topicCategory.setPid(rs.getInt("parentId"));
topicCategory.setIsLeaf(rs.getInt("isLeaf"));
topicCategory.setGrade(rs.getInt("grade"));
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
JdbcUtils.getInstance().close(con, pstmt, rs);
}
return topicCategory;
}
/**
* 更新类别
*/
public boolean updateTopicCategory(TopicCategory category) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "update t_category set name = ?, description = ? where id = ? " ;
try {
con = JdbcUtils.getInstance().getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, category.getName());
pstmt.setString(2, category.getDescrption());
pstmt.setInt(3, category.getId());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
JdbcUtils.getInstance().close(con, pstmt, rs);
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -