📄 albumtypedao.java
字号:
package cn.myvideosite.data.model.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import cn.myvideosite.data.model.bean.AlbumType;
public class AlbumTypeDAO extends BaseDAO{
/**
* 专辑分类表 保存方法
* @param albumtype
* @return
*/
public static AlbumType save(AlbumType albumtype){
PreparedStatement pstmt=null;
String sql="insert into albumtype (typeName) values (?)";
try {
pstmt=connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, albumtype.getTypeName());
if(pstmt.executeUpdate()==1){
ResultSet rs=pstmt.getGeneratedKeys();
if(rs!=null){
rs.next();
albumtype.setTypeId(rs.getInt(1));
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(pstmt!=null){
try {
pstmt.close();
pstmt=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return albumtype;
}
/**
* 专辑分类表 删除方法
* @param id
*/
public static void delete(int typeId){
PreparedStatement pstmt=null;
String sql="delete from albumtype where typeId=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1,typeId);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 专辑分类表 更新方法
* @param albumtype
*/
public static void update(AlbumType albumtype){
PreparedStatement pstmt=null;
String sql="update albumtype set typeName=? where typeId=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setString(1,albumtype.getTypeName());
pstmt.setInt(2,albumtype.getTypeId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 专辑分类表 查找方法 单条记录
* @param id
* @return
*/
public static AlbumType findById(int typeId){
PreparedStatement pstmt=null;
ResultSet rs=null;
AlbumType albumtype=null;
String sql="select * from albumtype where typeId=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1,typeId);
rs=pstmt.executeQuery();
albumtype=new AlbumType();
while(rs.next()){
albumtype.setTypeId(rs.getInt(1));
albumtype.setTypeName(rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
try {
if(rs!=null){
rs.close();
rs=null;
}
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return albumtype;
}
/**
* 专辑分类表 查询全部 无参数方法
* @return
*/
public static List<AlbumType> findAll(){
PreparedStatement pstmt=null;
ResultSet rs=null;
List<AlbumType> rtnList=null;
String sql="select * from albumtype";
try {
pstmt=connection.prepareStatement(sql);
rs=pstmt.executeQuery();
rtnList=new ArrayList<AlbumType>();
while(rs.next()){
AlbumType albumtype=new AlbumType();
albumtype.setTypeId(rs.getInt(1));
albumtype.setTypeName(rs.getString(2));
rtnList.add(albumtype);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
rs=null;
}
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return rtnList;
}
/**
* 专辑分类表 查询全部 有参数方法
* @param first
* @param max
* @return
*/
public static List<AlbumType> findAll(int first,int max){
PreparedStatement pstmt=null;
ResultSet rs=null;
List<AlbumType> rtnList=null;
String sql="select * from albumtype limit ?,?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1, first);
pstmt.setInt(2,max);
rs=pstmt.executeQuery();
rtnList=new ArrayList<AlbumType>();
while(rs.next()){
AlbumType albumtype=new AlbumType();
albumtype.setTypeId(rs.getInt(1));
albumtype.setTypeName(rs.getString(2));
rtnList.add(albumtype);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
rs=null;
}
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return rtnList;
}
public static AlbumType findByAlbumName(String typeName){
PreparedStatement pstmt=null;
ResultSet rs=null;
AlbumType albumtype=null;
String sql="select * from albumtype where typeName=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setString(1, typeName);
rs=pstmt.executeQuery();
if(rs.next()){
albumtype=new AlbumType();
albumtype.setTypeId(rs.getInt(1));
albumtype.setTypeName(rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}
return albumtype;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -