狂神說java超市管理系統service層
阿新 • • 發佈:2021-10-05
介面
package service.LogService.RoleService; import po.role; import java.sql.Connection; import java.sql.SQLException; import java.util.List; public interface RoleList { public List<role> RoleListService() throws SQLException, ClassNotFoundException; }
package service.LogService; importpo.user; import java.sql.Connection; import java.sql.SQLException; import java.util.Date; import java.util.List; public interface LoginService { //登入介面 public user Login(String User,String pwd) throws SQLException, ClassNotFoundException; //修改密碼介面 public boolean UppwdService(int id,String pwd) throwsSQLException, ClassNotFoundException; //獲取人員數量 public int CountService(String Username,int id) throws SQLException, ClassNotFoundException; //獲取人員資訊 public List <user> UserList(String UserName, int id,int a,int b) throws SQLException, ClassNotFoundException; //新增使用者 public booleanadduser(user user) throws SQLException, ClassNotFoundException; //查詢使用者是否存在 public boolean exuser(String user); //刪除使用者 public boolean deluser(int id); //根據ID查詢使用者 public user selectuser(int id); //更新使用者資訊 public Boolean UPuser(user user); }
實現類
package service.LogService.RoleService; import dao.BaseDao; import dao.Role.RoleListimpl; import po.role; import service.LogService.RoleService.RoleList; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class RoleListServiceimpl implements RoleList { public List<role> RoleListService() { Connection conn = null; try { conn = BaseDao.GetConn(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } RoleListimpl roleListimpl = new RoleListimpl(); List<role> roles = null; try { roles = roleListimpl.SelectRole(conn); } catch (SQLException e) { e.printStackTrace(); } BaseDao.ConnClose((com.mysql.jdbc.Connection) conn,null,null); return roles; } }
package service.LogService; import com.mysql.jdbc.StringUtils; import dao.BaseDao; import dao.LoginDao.LoginDao; import dao.LoginDao.LoginDaoImpl; import po.user; import java.sql.Connection; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; //登入service public class LoginServiceImpl implements LoginService { LoginDao loginDao; public LoginServiceImpl() { loginDao = new LoginDaoImpl(); } //登入 public user Login(String User, String pwd) throws SQLException, ClassNotFoundException { Connection conn = BaseDao.GetConn(); user user1 = null; user1 = loginDao.LoginUser(conn, User); BaseDao.ConnClose((com.mysql.jdbc.Connection) conn, null, null); return user1; } //修改service public boolean UppwdService(int id, String pwd) throws SQLException, ClassNotFoundException { Connection conn = null; boolean flag; int i = 0; if (pwd != null) { conn = BaseDao.GetConn(); i = loginDao.UpPwd(conn, id, pwd); } if (i != 0) { flag = true; } else { flag = false; } return flag; } //通過組或使用者名稱獲取數量 public int CountService(String Username, int id) throws SQLException, ClassNotFoundException { Connection conn = null; conn = BaseDao.GetConn(); int count = loginDao.CountName(conn, Username, id); BaseDao.ConnClose((com.mysql.jdbc.Connection) conn, null, null); return count; } //獲取使用者列表 public List<user> UserList(String UserName, int id, int a, int b) throws SQLException, ClassNotFoundException { Connection conn = null; conn = BaseDao.GetConn(); ArrayList<user> UserList = new ArrayList<user>(); UserList = (ArrayList<user>) loginDao.UerList(conn, UserName, id, a, b); BaseDao.ConnClose((com.mysql.jdbc.Connection) conn, null, null); return UserList; } //新增使用者 public boolean adduser(user user) throws SQLException { boolean flag = false; Connection conn = null; if (user != null) { if (StringUtils.isNullOrEmpty(user.getUserPassword())) { flag = false; } if (StringUtils.isNullOrEmpty(user.getUserCode())) { flag = false; } if (StringUtils.isNullOrEmpty(user.getUserRole())) { flag = false; }; try { conn = BaseDao.GetConn(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } try { conn.setAutoCommit(false); flag= loginDao.adduser(conn,user); conn.commit(); conn.setAutoCommit(true); } catch (SQLException e) { conn.rollback(); e.printStackTrace(); flag=false; }finally { BaseDao.ConnClose((com.mysql.jdbc.Connection) conn,null,null); } } return flag; } //查詢使用者是否存在 public boolean exuser(String user) { boolean flag=false; int a=0; Connection conn=null; try { conn = BaseDao.GetConn(); a= loginDao.ecuser(conn,user); if(a>0){ flag=false; }else{ flag=true; } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return flag; } //刪除使用者 public boolean deluser(int id) { boolean flag=false; Connection conn=null; int a=0; try { conn=BaseDao.GetConn(); a = loginDao.deluser(conn,id); if(a>0){ flag = true; }else{ flag=false; } } catch (ClassNotFoundException e) { e.printStackTrace(); flag=false; } catch (SQLException e) { e.printStackTrace(); }finally { BaseDao.ConnClose((com.mysql.jdbc.Connection) conn,null,null); } return flag; } //根據使用者查詢ID; public user selectuser(int id) { user user = new user(); user =null; Connection conn=null; try { conn = BaseDao.GetConn(); user= loginDao.selectuser(conn,id); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return user; } //更新使用者資訊 public Boolean UPuser(user user) { boolean flag = false; Connection conn=null; int updatauser = 0; try { conn =BaseDao.GetConn(); conn.setAutoCommit(false); updatauser = loginDao.updatauser(conn, user); conn.commit(); conn.setAutoCommit(true); if(updatauser!=0){ flag = true; }else{ flag = false; } } catch (ClassNotFoundException e) { try { conn.rollback(); } catch (SQLException ex) { ex.printStackTrace(); } e.printStackTrace(); flag = false; } catch (SQLException e) { try { conn.rollback(); flag = false; } catch (SQLException ex) { ex.printStackTrace(); flag = false; } e.printStackTrace(); flag = false; }finally { BaseDao.ConnClose((com.mysql.jdbc.Connection) conn,null,null); } return flag; } }