⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userbiz.java

📁 这段代码是用struts实现的客户管理系统
💻 JAVA
字号:
package com.tarena.biz;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tarena.entity.User;
import com.tarena.util.ConnectionFactory;
import com.tarena.util.JdbcUtil;

/**
 * 用户信息处理对象,主要的功能包括: 增加用户信息 查询用户信息 线程安全的实现
 * 
 * @Author Allan
 * @Version 1.0
 */
public class UserBiz {
    public void add(User user) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = ConnectionFactory.getConnection();
            StringBuffer sql = new StringBuffer("insert into user_tbl");
            sql.append("(id,uname,pwd,rname,gender,birthday,degree,hobbies,remark)");
            sql.append(" values(null,?,?,?,?,?,?,?,?)");
            ps = con.prepareStatement(sql.toString());
            int index = 1;
            ps.setString(index++, user.getUserName());
            ps.setString(index++, user.getPassword());
            ps.setString(index++, user.getRealName());
            ps.setString(index++, user.getGender());
            ps.setDate(index++, user.getBirthday());
            ps.setString(index++, user.getDegree());
            ps.setString(index++, user.getHobbies());
            ps.setString(index++, user.getRemark());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JdbcUtil.close(con, ps);
        }
    }

    public boolean isUserNameExist(String userName) {
        User user = new User();
        user.setUserName(userName);
        List<User> users = find(user);
        if (users.size() == 0) {
            return false;
        }

        return true;
    }

    public User find(String userName, String password) {
        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);
        List<User> users = find(user);
        if (users.size() == 0) {
            return null;
        }

        return users.iterator().next();
    }

    public User findById(int id) {
        User user = new User();
        user.setId(id);
        List<User> users = find(user);
        if (users.size() == 0) {
            return null;
        }

        return users.iterator().next();
    }

    /**
     * 根据条件查询用户信息,可能的查询条件包括: 用户的标识 - 如果不为0 用户名 - 如果不为null 密码 - 如果不为null
     * 
     * @param user
     *            查询条件
     * @return 用户信息列表,如过没有返回一个空的List
     * @throws RuntimeException
     *             执行操作出错时。
     */
    public List<User> find(User user) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> users = null;
        try {
            con = ConnectionFactory.getConnection();
            StringBuffer sql = new StringBuffer("select ");
            sql.append("id,uname,pwd,rname,gender,birthday,degree,hobbies,remark");
            sql.append(" from user_tbl");
            sql.append(" where 1=1 ");
            if (user.getId() != 0) {
                sql.append(" and id = ?");
            }

            if (user.getUserName() != null) {
                sql.append(" and uname = ?");
            }

            if (user.getPassword() != null) {
                sql.append(" and pwd = ?");
            }

            ps = con.prepareStatement(sql.toString());
            int index = 1;
            if (user.getId() != 0) {
                ps.setInt(index++, user.getId());
            }

            if (user.getUserName() != null) {
                ps.setString(index++, user.getUserName());
            }

            if (user.getPassword() != null) {
                ps.setString(index++, user.getPassword());
            }
            rs = ps.executeQuery();
            users = getUsers(rs);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JdbcUtil.close(con, ps, rs);
        }

        return users;
    }

    private List<User> getUsers(ResultSet rs) throws SQLException {
        List<User> users = new ArrayList<User>();
        while (rs.next()) {
            User user = new User();
            user.setId(rs.getInt(1));
            user.setUserName(rs.getString(2));
            user.setPassword(rs.getString(3));
            user.setRealName(rs.getString(4));
            user.setGender(rs.getString(5));
            user.setBirthday(rs.getDate(6));
            user.setDegree(rs.getString(7));
            user.setHobbies(rs.getString(8));
            user.setRemark(rs.getString(9));
            users.add(user);
        }

        return users;
    }

    public void modify(User user) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = ConnectionFactory.getConnection();
            String sql = "update user_tbl set uname=?,pwd=?,rname=?,gender=?,birthday=?,degree=?,hobbies=?,remark=? where id=?";
            ps = con.prepareStatement(sql);
            int index = 1;
            ps.setString(index++, user.getUserName());
            ps.setString(index++, user.getPassword());
            ps.setString(index++, user.getRealName());
            ps.setString(index++, user.getGender());
            ps.setDate(index++, user.getBirthday());
            ps.setString(index++, user.getDegree());
            ps.setString(index++, user.getHobbies());
            ps.setString(index++, user.getRemark());
            ps.setInt(index++, user.getId());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JdbcUtil.close(con, ps);
        }
    }

    public void remove(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = ConnectionFactory.getConnection();
            String sql = "delete from user_tbl where id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JdbcUtil.close(con, ps);
        }
    }

    public List<User> findByPage(int startRow, int count) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> users = null;
        try {
            con = ConnectionFactory.getConnection();
            StringBuffer sql = new StringBuffer();
            sql.append("select * from (");
            sql.append("select users.*, rownum r from (");
            sql.append("select id,uname,pwd,rname,gender,birthday,degree,hobbies,remark from user_tbl) users) ");
            sql.append("where r between ? and ?");
            ps = con.prepareStatement(sql.toString());
            ps.setInt(1, startRow);
            ps.setInt(2, startRow + count - 1);
            rs = ps.executeQuery();
            users = getUsers(rs);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JdbcUtil.close(con, ps, rs);
        }
        return users;
    }

    public int getRowCount() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int rows = -1;
        try {
            con = ConnectionFactory.getConnection();
            String sql = "select count(id) from user_tbl";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            rs.next();
            rows = rs.getInt(1);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            JdbcUtil.close(con, ps, rs);
        }

        return rows;
    }

//    public static void main(String[] args) {
//        UserBiz biz = new UserBiz();
//        User user = new User();
//        // user.setUserName("jerry");
//        // user.setPassword("1");
//        user.setId(203);
//        List<User> users = biz.find(user);
//        for (User u : users) {
//            System.out.println(u.getUserName());
//        }
//    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -