📄 bookcategorydao.java
字号:
/**
* 图书类别的操作类
*/
package fuguo.yy1.dao;
/**
* @author cankongyun
*
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import fuguo.yy1.base.BaseDAO;
import fuguo.yy1.db.DBConn;
import fuguo.yy1.dto.BookCategory;
import fuguo.yy1.util.SQLConstants;
public class BookCategoryDAO implements BaseDAO {
private static Connection con=DBConn.getConnection();
private static PreparedStatement pstmt=null;
private static ResultSet rs=null;
/**
* 显示所有的图书类别
* @return ArrayList
*/
public static ArrayList bookCategorys(){
ArrayList al=new ArrayList();
BookCategory bookCategory=null;
try{
pstmt=con.prepareStatement(SQLConstants.SELECT_BOOKCATEGORYS);
rs=pstmt.executeQuery();
while(rs.next()){
bookCategory=new BookCategory();
bookCategory.setCategoryId(rs.getInt("bookCategoryId"));
bookCategory.setCategoryName(rs.getString("categoryName"));
//将bookCategory对象加入al容器中
al.add(bookCategory);
}
DBConn.releaseConnection(rs,pstmt,null);
}catch(SQLException se){
System.err.println("显示所有图书类别失败!出现异常:"+se.getMessage());
}
return al;
}
/**
* 检测图书类别是否存在方法
* @param _categoryName
* @return boolean
*/
public static boolean checkCategoryName(String _categoryName)
{
boolean exist = false;
try
{
pstmt = con.prepareStatement(SQLConstants.SELECT_CHECK_CATEGORYNAME);
pstmt.setString(1, _categoryName);
rs = pstmt.executeQuery();
exist = rs.next();
DBConn.releaseConnection(rs,pstmt,null);
}catch(SQLException se){
System.err.println("检测图书类别是否存在失败!出现异常:"+se.getMessage());
}
return exist;
}
/**
* 增加图书类别的方法
* @param _baseDTO
* @return int
*/
public static int insertCategory(BookCategory _bookCategory){
int result=0;
try{
StringBuffer INSERT_BOOKCATEGORY = new StringBuffer();
INSERT_BOOKCATEGORY.append("insert into bookCategory(categoryName)");
INSERT_BOOKCATEGORY.append(" values(?)");
pstmt = con.prepareStatement(INSERT_BOOKCATEGORY.toString());
pstmt.setString(1, _bookCategory.getCategoryName());
result = pstmt.executeUpdate();
DBConn.releaseConnection(null,pstmt,null);
}catch(SQLException se) {
System.out.println("添加图书类别失败!出现异常:"+se.getMessage());
}
return result;
}
/**
* 根据图书类别Id查询类别信息
* @param _categoryId
* @return BookCategory
*/
public static BookCategory selectBookCategoryById(int _bookCategoryId){
BookCategory bookCategory=null;
try{
pstmt=con.prepareStatement(SQLConstants.SELECT_BOOKCATEGORY_ID);
pstmt.setInt(1,_bookCategoryId);
rs=pstmt.executeQuery();
while(rs.next()){
bookCategory=new BookCategory();
bookCategory.setCategoryId(rs.getInt("bookCategoryId"));
bookCategory.setCategoryName(rs.getString("categoryName"));
return bookCategory;
}
DBConn.releaseConnection(rs,pstmt,null);
}catch(SQLException se){
System.err.println("根据Id查询用户信息失败!出现异常:"+se.getMessage());
}
return null;
}
/**
* 修改图书类别
* @param _bookCategoryId
* @param _baseDTO
* @return int
*/
public static int updateBookCategory(int _bookCategoryId,BookCategory _bookCategory){
int result=0;
try
{
pstmt = con.prepareStatement(SQLConstants.UPDATE_BOOKCATEGORY);
pstmt.setString(1, _bookCategory.getCategoryName());
pstmt.setInt(2,_bookCategoryId);
result = pstmt.executeUpdate();
DBConn.releaseConnection(null,pstmt,null);
}
catch(SQLException se){
System.err.println("修改图书类别信息失败!出现异常:"+se.getMessage());
}
return result;
}
/**
* 删除图书类别
* @param _bookCategoryId
* @return int
*/
public static int deleteCategory(int _bookCategoryId){
int result=0;
try{
pstmt=con.prepareStatement(SQLConstants.DELETE_BOOKCATEGORY);
pstmt.setInt(1,_bookCategoryId);
result=pstmt.executeUpdate();
DBConn.releaseConnection(null,pstmt,null);
}catch(SQLException se){
System.err.println("删除图书类别信息失败,出现异常:"+se.getMessage());
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -