package com.moder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Random;
public class userBeanCl {
//添加数据库组建
Connection ct=null;
Statement sm=null;
ResultSet rs=null;
//验证用户名
public boolean CheckUser(String name,String passwd){
boolean b=false;
try{
ct=new ConnDB().getConnet();
sm=ct.createStatement();
rs=sm.executeQuery("select passwd from userTable where name='"+name+"'");
if(rs.next()){
if(rs.getString(1).equals(passwd)){
b=true;
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try{
ct.close();
sm.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return b;
}
//接受用户级别
public boolean gradeCheck(String name){
boolean b=false;
try{
ct=new ConnDB().getConnet();
sm=ct.createStatement();
rs=sm.executeQuery("select grade from userTable where name='"+name+"'");
if(rs.next()){
if(rs.getString(1).equals("1")){
b=true;
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try{
ct.close();
sm.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return b;
}
//管理员添加用户加班信息
//对Mid进行随机编号范围在1~100000之间方便对信息表管理
public boolean addMessage(String Mname,String Mdata,String MStime,String MOtime,String Mtime,String Reasons){
boolean b=false;
//处理Mid,赋值
int i=(int)(Math.random()*10000);
String Mid=String.valueOf(i);
//对信息表添加信息
try{
String sql="insert into MessageTable values" +
"('"+Mid+"','"+Mname+"','"+Mdata+"','"+MStime+"','"+MOtime+"','"+Mtime+"','"+Reasons+"')";
ct= new ConnDB().getConnet();
sm=ct.createStatement();
int m=sm.executeUpdate(sql);
if(m==1){
b=true;
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try{
ct.close();
sm.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return b;
}
//管理员添加人员信息
public boolean addEmployee(String id,String name,String passwd,String email,String grade){
boolean b=false;
try{
String sql="insert into userTable values('"+id+"','"+name+"','"+passwd+"','"+email+"','"+grade+"')";
ct= new ConnDB().getConnet();
sm=ct.createStatement();
int m=sm.executeUpdate(sql);
if(m==1){
b=true;
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try{
ct.close();
sm.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return b;
}
//查看加班信息
public ArrayList getUserByPage(String name){
ArrayList al=new ArrayList();
try {
ct = new ConnDB().getConnet();
//创建statement
sm=ct.createStatement();
//查询
rs=sm.executeQuery
("select * from MessageTable where Mname= '"+name+"'");
while (rs.next()) {
messageBean ub=new messageBean();
ub.setMid(rs.getInt(1));
ub.setMname(rs.getString(2));
ub.setMdata(rs.getString(3));
ub.setMStime(rs.getString(4));
ub.setMOtime(rs.getString(5));
ub.setMtime(rs.getString(6));
ub.setReasons(rs.getString(7));
al.add(ub);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}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) {
e.printStackTrace();
}
}
//删除加班信息
public boolean delMessage(String Mid){
boolean b=false;
try{
ct=new ConnDB().getConnet();
sm=ct.createStatement();
int id=Integer.valueOf(Mid);
//执行
int a=sm.executeUpdate("delete from MessageTable where Mid='"+id+"'");
if(a==1){
//删除成功
b=true;
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
this.close();
}
return b;
}
//删除人员信息
public boolean delEmployee(String id)
{
boolean b=false;
try{
ct=new ConnDB().getConnet();
sm=ct.createStatement();
int num=Integer.valueOf(id);
//执行
int a=sm.executeUpdate("delete from userTable where id='"+num+"'");
if(a==1){
//删除成功
b=true;
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
this.close();
}
return b;
}
//用户自己修改自己的信息
public boolean alteruser(String name,String passwd,String email){
boolean b=false;
try{
ct=new ConnDB().getConnet();
sm=ct.createStatement();
//执行
int a=sm.executeUpdate("update userTable set passwd='"+passwd+"',email='"+email+"' where name='"+name+"'");
if(a==1){
//修改成功
b=true;
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
this.close();
}
return b;
}
//修改加班信息
public boolean alterMessage(String Mid,String Mname,String Mdata,String MStime,String MOtime,String Mtime,String Reasons){
boolean b=false;
try{
ct=new ConnDB().getConnet();
sm=ct.createStatement();
//执行
int a=sm.executeUpdate("update MessageTable set Mdata='"+Mdata+"',MStime='"+MStime+"',MOtime='"+MOtime+"',Mtime='"+Mtime+"',Reasons='"+Reasons+"'where Mid='"+Mid+"'");
if(a==1){
//修改成功
b=true;
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
this.close();
}
return b;
}
//修改人员信息
public boolean alterEmployee(String id,String name,String passwd,String email,String grade){
boolean b=false;
try{
ct=new ConnDB().getConnet();
sm=ct.createStatement();
//执行
int a=sm.executeUpdate("update userTable set name='"+name+"',passwd='"+passwd+"',email='"+email+"',grade='"+grade+"' where id='"+id+"'");
if(a==1){
//修改成功
b=true;
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
this.close();
}
return b;
}
//查看人员信息
public ArrayList getEmployee(){
ArrayList al=new ArrayList();
try {
ct = new ConnDB().getConnet();
//创建statement
sm=ct.createStatement();
//查询
rs=sm.executeQuery
("select * from userTable");
while (rs.next()) {
userBean ub=new userBean();
ub.setId(rs.getInt(1));
ub.setName(rs.getString(2));
ub.setPasswd(rs.getString(3));
ub.setEmail(rs.getString(4));
ub.setGrade(rs.getInt(5));
al.add(ub);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
this.close();
}
return al;
}
}