📄 actorbeancl.java
字号:
//这是一个处理类,有些人把它叫做bo,主要是封装对users表的各种
//操作,(主要是增,删,修,查...)
package com.djj.model;
import java.sql.*;
import java.util.*;
public class ActorBeanCl {
private Statement sm=null;//PreparedStatement
private ResultSet rs=null;
private Connection ct=null;
private int pageSize=3;
private int rowCount=0;//该值从数据库查询
private int pageCount=0;//该值是通过pageSize和rowCount
//添加角色
/**
* @author 小明(我的工位a89)
* @param actorName: 系统管理员...
* @param about: 角色描述
* @param grade: 权限
* @return boolean: 如果true:说明添加成功,false:添加不成功
*
*/
public boolean addActor(String actorName,String about,int grade){
boolean b=false;
try {
//得到连接
ct=new ConnDB().getConn();
sm=ct.createStatement();
//执行
int a=sm.executeUpdate("insert into actor values('"+actorName+"','"+about+"','"+grade+"')");
if(a==1){
//添加成功
b=true;
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
this.close();
}
return b;
}
//删除用户
public boolean delActorByGrade(String grade){
boolean b=false;
try {
//得到连接
ct=new ConnDB().getConn();
sm=ct.createStatement();
//grade = Tools.getNewString(grade);
//System.out.println("要删除的grade-------"+grade);
//执行
int a=sm.executeUpdate(
"delete from users where exists(select * from actor where users.grade=actor.grade and grade='"+grade+"')delete from actor where grade='"+grade+"'");
//System.out.println("welcome----a-----"+a);
if(a==2){
//删除成功
b=true;
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
this.close();
}
return b;
}
//更新用户
public boolean updateActor(ActorBean actor){
boolean b=false;
try {
//得到连接
ct=new ConnDB().getConn();
sm=ct.createStatement();
//执行
String grade=actor.getGrade();
String about=actor.getAbout();
int lever=actor.getLever();
about=Tools.getNewString(about);//将乱码转成 gb2312 , gbk ,utf-8
int a=sm.executeUpdate("update actor set about='"+about+"',lever='"+lever+"' where grade='"+grade+"'");
if(a==1){
//删除成功
b=true;
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
this.close();
}
return b;
}
//返回,分页的总页数
public int getPageCount(){
try {
//得到连接
ct=new ConnDB().getConn();
sm=ct.createStatement();
// 4. 查询
rs=sm.executeQuery("select count(*) from actor ");
//请注意,一定要rs.next()
if(rs.next()){
rowCount=rs.getInt(1);//得出一共有多少条数据
//System.out.println("----------------"+rowCount);
}
//计算pageCount,这里算法很多,可以自己设计
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount=rowCount/pageSize+1;
}
//pageCount为分多少条页面
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
this.close();
}
return pageCount;
}
//得到用户需要显示的用户信息(分页)
public ArrayList getActorByPage(int pageNow){
ArrayList al=new ArrayList();
try {
//创建连接
ct=new ConnDB().getConn();
// 3.创建Statement
sm=ct.createStatement();
//查询出需要显示的记录
rs=sm.executeQuery("select top "+pageSize
+" * from actor where grade not in (select top "
+pageSize*(pageNow-1)+" grade from actor) ");
//开始将rs封装到ArrayList
while(rs.next()){
ActorBean ab = new ActorBean();
ab.setGrade(rs.getString(1));
ab.setAbout(rs.getString(2));
ab.setLever(rs.getInt(3));
//将ab放入到arryalist中
al.add(ab);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
//关闭资源
this.close();
}
return al;
}
//根据用户名搜索用户信息
// public ArrayList selectUser(String u){
//
// ArrayList al=new ArrayList();
// try {
//
//// 到数据库中去验证用户
// ct=new ConnDB().getConn();
//
// //3.创建Statement
// sm=ct.createStatement();
//
// //4. 查询
// rs=sm.executeQuery("select * from users where username='"+u+"'");
//
// //根据结果判断
// if(rs.next()){
// UserBean ub=new UserBean();
// ub.setUserId(rs.getInt(1));
// ub.setUsername(rs.getString(2));
// ub.setPasswd(rs.getString(3));
// ub.setRepasswd(rs.getString(4));
// ub.setGrade(rs.getString(5));
// al.add(ub);
// }
//
// } catch (Exception e) {
//
// e.printStackTrace(); // TODO: handle exception
//
// }finally{
//
// this.close();
// }
// return al;
// }
//关闭资源
public void close(){
try {
if(rs!=null){
rs.close();
rs=null;
}
if(sm!=null){
sm.close();
sm=null;
}
if(ct!=null){
ct.close();
ct=null;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//验证用户是否存在
// public boolean checkUser(String u,String p ){
//
// boolean b=false;
//
// //...
// try {
//
//// 到数据库中去验证用户
// ct=new ConnDB().getConn();
//
// //3.创建Statement
// sm=ct.createStatement();
//
// //4. 查询
// rs=sm.executeQuery("select passwd from users where username='"+u+"'");
//
// //根据结果判断
// if(rs.next()){
//
// //说明用户名存在
// if(rs.getString(1).equals(p)){
//
// //一定合法
// b=true;
// }
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// // TODO: handle exception
// }finally{
//
// //关闭打开的各种资源,这个很重要的!!!!
//
// //函数 !!
// this.close();
// }
//
// return b;
//
// }
//public static void main(String args[]){
// ActorBeanCl ub = new ActorBeanCl();
// ub.delActorByGrade("大师傅");
// //ub.addActor("栏目管理员", "主要管理网站的栏目", 2);
//}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -