📄 channelclassdao.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.ChannelClass;
public class ChannelClassDAO extends BaseDAO{
/**
* 频道分类的 写入方法
* @param channelclass
* @return
*/
public static ChannelClass save(ChannelClass channelclass){
PreparedStatement pstmt=null;
String sql="insert into channelclass (fchannelId,channelName) values (?,?)";
try {
pstmt=connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
pstmt.setInt(1, channelclass.getFchannelId());
pstmt.setString(2,channelclass.getChannelName());
if(pstmt.executeUpdate()==1){
ResultSet rs=pstmt.getGeneratedKeys();
if(rs!=null){
rs.next();
channelclass.setChannelId(rs.getInt(1));
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(pstmt!=null){
try {
pstmt.close();
pstmt=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return channelclass;
}
/**
* 频道分类的 删除方法
* @param id
*/
public static void delete(int channelId){
PreparedStatement pstmt=null;
String sql="delete from channelclass where channelId=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1, channelId);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 频道分类的 更新方法
* @param channelclass
*/
public static void update(ChannelClass channelclass){
PreparedStatement pstmt=null;
String sql="update channelclass set fchannelId=?,channelName=? where channelId=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1,channelclass.getFchannelId());
pstmt.setString(2,channelclass.getChannelName());
pstmt.setInt(3,channelclass.getChannelId());
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 ChannelClass findById(int id){
PreparedStatement pstmt=null;
ResultSet rs=null;
ChannelClass channelclass=null;
String sql="select * from channelclass where channelId=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1, id);
rs=pstmt.executeQuery();
channelclass=new ChannelClass();
while(rs.next()){
channelclass.setChannelId(rs.getInt(1));
channelclass.setFchannelId(rs.getInt(2));
channelclass.setChannelName(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return channelclass;
}
/**
* 频道分类的 查询全部 有参数方法
* @return
*/
public static List<ChannelClass> findAll(){
PreparedStatement pstmt=null;
ResultSet rs=null;
List<ChannelClass> rtnList=null;
String sql="select * from channelclass";
try {
pstmt=connection.prepareStatement(sql);
rs=pstmt.executeQuery();
rtnList=new ArrayList<ChannelClass>();
while(rs.next()){
ChannelClass channelclass=new ChannelClass();
channelclass.setChannelId(rs.getInt(1));
channelclass.setFchannelId(rs.getInt(2));
channelclass.setChannelName(rs.getString(3));
rtnList.add(channelclass);
}
} 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<ChannelClass> findAll(int first,int max){
PreparedStatement pstmt=null;
ResultSet rs=null;
List<ChannelClass> rtnList=null;
String sql="select * from channelclass limit ?,?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setInt(1, first);
pstmt.setInt(2,max);
rs=pstmt.executeQuery();
rtnList=new ArrayList<ChannelClass>();
while(rs.next()){
ChannelClass channelclass=new ChannelClass();
channelclass.setChannelId(rs.getInt(1));
channelclass.setFchannelId(rs.getInt(2));
channelclass.setChannelName(rs.getString(3));
rtnList.add(channelclass);
}
} 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 ChannelClass findByChannelName(String channelName){
PreparedStatement pstmt=null;
ResultSet rs=null;
ChannelClass channelclass=null;
String sql="select * from channelclass where channelName=?";
try {
pstmt=connection.prepareStatement(sql);
pstmt.setString(1, channelName);
rs=pstmt.executeQuery();
if(rs.next()){
channelclass=new ChannelClass();
channelclass.setChannelId(rs.getInt(1));
channelclass.setFchannelId(rs.getInt(2));
channelclass.setChannelName(rs.getString(3));
}
} 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 channelclass;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -