public interface UserDao {
//得到要登录的用户
public User getLoginUser(Connection connection, String userCode) throws SQLException;
//修改当前用户密码
public int updatePwd(Connection connection,int id,int password) throws SQLException;
}
4.UserDao接口实现类
public int updatePwd(Connection connection, int id, String password) throws SQLException {
PreparedStatement pstm = null;
int excute = 0;
if(connection!=null){
String sql = "update smbms_user set userPassword = ? where id = ?";
Object[] params = {password,id};
excute = BaseDao.execute(connection,pstm,sql,params);
BaseDao.closeResource(null,pstm,null);
}
return excute;
}
5.UserService层
public interface UserService {
//用户登录
public User login(String userCode, String password);
//根据用户ID修改密码
public boolean updatePwd(int id,String pwd);
}
6.UserService实现类
public class UserServiceImpl implements UserService{
//业务层都会调用dao层,所以我们要引入Dao层;
private UserDao userDao;
public UserServiceImpl(){
userDao = new UserDaoImpl();
}
public User login(String userCode, String password) {
Connection connection = null;
User user = null;
try {
connection = BaseDao.getConnection();
//通过业务层调用对应的具体的数据库操作
user = userDao.getLoginUser(connection,userCode);
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return user;
}
public boolean updatePwd(int id, int pwd) {
Connection connection = null;
boolean flag = false;
//修改密码
try {
connection = BaseDao.getConnection();
if(userDao.updatePwd(connection,id,pwd)>0){
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null)
}
return flag;
}
}
public interface UserDao {
//得到要登录的用户
public User getLoginUser(Connection connection, String userCode) throws SQLException;
//修改当前用户密码
public int updatePwd(Connection connection,int id,String password) throws SQLException;
//查询用户名或者角色查询用户总数
public int getUserCount(Connection connection,String username,int userRole) throws SQLException;
}
2.UserDaoImpl
public class UserDaoImpl implements UserDao{
public User getLoginUser(Connection connection, String userCode) throws SQLException {
PreparedStatement pstm = null;
ResultSet rs = null;
User user = null;
if(connection!=null){
String sql = "select * from smbms_user where userCode=?";
Object[] params = {userCode};
rs = BaseDao.execute(connection,pstm,rs,sql,params);
if(rs.next()){
user = new User();
user.setId(rs.getInt("id"));
user.setUserCode(rs.getString("userCode"));
user.setUserName(rs.getString("userName"));
user.setUserPassword(rs.getString("userPassword"));
user.setGender(rs.getInt("gender"));
user.setBirthday(rs.getDate("birthday"));
user.setPhone(rs.getString("phone"));
user.setAddress(rs.getString("address"));
user.setUserRole(rs.getInt("userRole"));
user.setCreatedBy(rs.getInt("createdBy"));
user.setCreationDate(rs.getTimestamp("creationDate"));
user.setModifyBy(rs.getInt("modifyBy"));
user.setModifyDate(rs.getTimestamp("modifyDate"));
}
BaseDao.closeResource(null,pstm,rs);
}
return user;
}
public int updatePwd(Connection connection, int id, String password) throws SQLException {
PreparedStatement pstm = null;
int excute = 0;
if(connection!=null){
String sql = "update smbms_user set userPassword = ? where id = ?";
Object[] params = {password,id};
excute = BaseDao.execute(connection,pstm,sql,params);
BaseDao.closeResource(null,pstm,null);
}
return excute;
}
//根据用户名或者角色查询用户总数【最难理解的SQL】
public int getUserCount(Connection connection, String username, int userRole) throws SQLException {
PreparedStatement pstm = null;
ResultSet rs = null;
int count= 0;
if(connection!=null){
StringBuffer sql = new StringBuffer();
sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id");
ArrayList
3.UserService
public interface UserService {
//用户登录
public User login(String userCode, String password);
//根据用户ID修改密码
public boolean updatePwd(int id,String pwd) ;
}
4.UserServiceImpl
public class UserServiceImpl implements UserService{
//业务层都会调用dao层,所以我们要引入Dao层;
private UserDao userDao;
public UserServiceImpl(){
userDao = new UserDaoImpl();
}
public User login(String userCode, String password) {
Connection connection = null;
User user = null;
try {
connection = BaseDao.getConnection();
//通过业务层调用对应的具体的数据库操作
user = userDao.getLoginUser(connection,userCode);
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return user;
}
public boolean updatePwd(int id, String pwd) {
Connection connection = null;
boolean flag = false;
//修改密码
try {
connection = BaseDao.getConnection();
if(userDao.updatePwd(connection,id,pwd)>0){
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return flag;
}
//查询记录数
public int getUserCount(String username, int userRole) {
Connection connection = null;
int count = 0;
try {
connection = BaseDao.getConnection();
count = userDao.getUserCount(connection,username,userRole);
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return count;
}
@Test
public void Test(){
UserServiceImpl userService = new UserServiceImpl();
int userCount = userService.getUserCount(null,0);
System.out.println(userCount);
}
}
2.获取用户列表
1.userdao
public interface UserDao {
//得到要登录的用户
public User getLoginUser(Connection connection, String userCode) throws SQLException;
//修改当前用户密码
public int updatePwd(Connection connection,int id,String password) throws SQLException;
//查询用户名或者角色查询用户总数
public int getUserCount(Connection connection,String username,int userRole) throws SQLException;
//通过条件查询userList
public List getUserList(Connection connection, String username, int userRole, int currentPageNo, int pageSize) throws SQLException;
}
2.userdaolmpl
public List getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws SQLException {
PreparedStatement pstm = null;
ResultSet rs = null;
List userList = new ArrayList();
if (connection != null) {
StringBuffer sql = new StringBuffer();
sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id");
List list = new ArrayList();
if (!StringUtils.isNullOrEmpty(userName)) {
sql.append(" and u.userName like ?");
list.add("%"+userName+"%");
}
if (userRole > 0) {
sql.append(" and u.userRole = ?");
list.add(userRole);
}
//在mysql数据库中,分页使用 limit startIndex,pageSize ; 总数
//当前页 (当前页-1)*页面大小
sql.append(" order by creationDate DESC limit ?,?");
currentPageNo = (currentPageNo - 1) * pageSize;
list.add(currentPageNo);
list.add(pageSize);
Object[] params = list.toArray();
System.out.println("sql ----> " + sql.toString());
rs = BaseDao.execute(connection, pstm, rs, sql.toString(), params);
while (rs.next()) {
User _user = new User();
_user.setId(rs.getInt("id"));
_user.setUserCode(rs.getString("userCode"));
_user.setUserName(rs.getString("userName"));
_user.setGender(rs.getInt("gender"));
_user.setBirthday(rs.getDate("birthday"));
_user.setPhone(rs.getString("phone"));
_user.setUserRole(rs.getInt("userRole"));
_user.setUserRoleName(rs.getString("userRoleName"));
userList.add(_user);
}
BaseDao.closeResource(null, pstm, rs);
}
return userList;
}
3.userService
public interface UserService {
//用户登录
public User login(String userCode, String password);
//根据用户ID修改密码
public boolean updatePwd(int id,String pwd) ;
//查询记录数
public int getUserCount(String username,int userRole);
//根据条件查询用户列表
public List getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize);
}
4.userServicelmpl
public List getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
Connection connection = null;
List userList = null;
System.out.println("queryUserName ---- >" + queryUserName);
System.out.println("queryUserRole ---- >" + queryUserRole);
System.out.println("currentPageNo ---- >" + currentPageNo);
System.out.println("pageSize ---- >" + pageSize);
try {
connection = BaseDao.getConnection();
userList = userDao.getUserList(connection,queryUserName,queryUserRole,currentPageNo,pageSize);
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return userList;
}
3.获取角色操作
为了我们职责统一,可以把角色的操作单独放在一个包中,和Pojo类对应
RoleDao
public interface RoleDao {
//获取角色列表
public List getRoleList(Connection connection) throws SQLException;
}
RoleDaoImpl
public class RoleDaoImpl implements RoleDao {
//获取角色列表
public List getRoleList(Connection connection) throws SQLException {
PreparedStatement pstm = null;
ResultSet rs = null;
List roleList = new ArrayList();
if(connection!=null){
String sql = "select * from smbms_role";
pstm = connection.prepareStatement(sql);
Object[] params = {};
rs = BaseDao.execute(connection,pstm,rs,sql,params);
while(rs.next()){
Role _role = new Role();
_role.setId(rs.getInt("id"));
_role.setRoleCode(rs.getString("roleCode"));
_role.setRoleName(rs.getString("roleName"));
roleList.add(_role);
}
BaseDao.closeResource(null,pstm,rs);
}
return roleList;
}
}
RoleService
public interface RoleService {
//获取角色列表
public List getRoleList() ;
}
RoleServiceImpl
public class RoleServiceImpl implements RoleService{
//引入Dao
private RoleDao roleDao;
public RoleServiceImpl(){
roleDao = new RoleDaoImpl();
}
public List getRoleList(){
Connection connection = null;
List roleList = null;
try {
connection = BaseDao.getConnection();
roleList = roleDao.getRoleList(connection);
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return roleList;
}
}
声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/
public class Singleton {
}
/*
* 懒汉模式。注意,getInstance如果在多线程环境中调用,需要加上synchronized,否则存在线程不安全问题
*/
class LazySingleton
这个月公司安排我一个人做iOS客户端开发,由于急着用,我先发布一个版本,由于第一次发布iOS应用,期间出了不少问题,记录于此。
1、使用Application Loader 发布时报错:Communication error.please use diagnostic mode to check connectivity.you need to have outbound acc
/*
2013年3月15日15:16:24
malloc 就memory(内存) allocate(分配)的缩写
本程序没有实际含义,只是理解使用
*/
# include <stdio.h>
# include <malloc.h>
int main(void)
{
int i = 5; //分配了4个字节 静态分配
int * p
http://wiki.sdn.sap.com/wiki/display/BOBJ/Optimize+query+with+Query+Stripping+in+Web+Intelligence
and a very straightfoward video
http://www.sdn.sap.com/irj/scn/events?rid=/library/uuid/40ec3a0c-936