op_user.java
上传用户:tanyanyong
上传日期:2013-06-23
资源大小:1355k
文件大小:12k
源码类别:

电子政务应用

开发平台:

MultiPlatform

  1. package bookshop.run;
  2. /**
  3.  * <p>Title: </p>
  4.  * <p>Description: </p>
  5.  * <p>Copyright: Copyright (c) 2004</p>
  6.  * <p>Company: </p>
  7.  * @author not attributable
  8.  * @version 1.0
  9.  */
  10. import java.sql.*;
  11. import java.util.Vector;
  12. import javax.servlet.http.HttpServletRequest;
  13. import bookshop.util.*;
  14. import bookshop.book.*;
  15. public class op_user extends DataBase{
  16.         private user user = new user();                         //新的用户对象
  17.         private javax.servlet.http.HttpServletRequest request;  //建立页面请求
  18.         private Vector userlist; //显示用户列表向量数组
  19.         private int page = 1; //显示的页码
  20.         private int pageSize=8; //每页显示的图书数
  21.         private int pageCount =0; //页面总数
  22.         private long recordCount =0;         //查询的记录总数
  23.         private String message = "";         //出错信息提示
  24.         private String username = "";       //注册后返回的用户名
  25.         private long userid = 0;      //注册后返回的用户ID
  26.   public op_user() {
  27.   }
  28.   //添加新用户
  29.   public boolean add(HttpServletRequest req) throws Exception {
  30.         if (getRequest(req)) {
  31.             DataBase db = new DataBase();
  32.             db.connect();
  33.             stmt = db.conn.createStatement ();
  34.             sqlStr = "select * from shop_user where username = '" +
  35.                       user.getUserName() +"'";
  36.             sqlStr = "insert into shop_user (username,password,Names,sex,"+
  37.                      "Address,Phone,Post,Email,RegTime,RegIpaddress) values ('";
  38.             sqlStr = sqlStr + dataFormat.toSql(user.getUserName()) + "','";
  39.             sqlStr = sqlStr + dataFormat.toSql(user.getPassWord()) + "','";
  40.             sqlStr = sqlStr + dataFormat.toSql(user.getNames()) + "','";
  41.             sqlStr = sqlStr + dataFormat.toSql(user.getSex()) + "','";
  42.             sqlStr = sqlStr + dataFormat.toSql(user.getAddress()) + "','";
  43.             sqlStr = sqlStr + dataFormat.toSql(user.getPhone()) + "','";
  44.             sqlStr = sqlStr + dataFormat.toSql(user.getPost()) + "','";
  45.             sqlStr = sqlStr + dataFormat.toSql(user.getEmail()) + "',now(),'";
  46.             sqlStr = sqlStr + user.getRegIpAddress() + "')";
  47.             try{
  48.                 stmt.executeUpdate(sqlStr);
  49.                 sqlStr = "select max(id) from shop_user where username = '" +
  50.                          user.getUserName()+ "'";
  51.                          rs = stmt.executeQuery(sqlStr);
  52.                          while (rs.next()){
  53.                               userid = rs.getLong(1);
  54.                               }
  55.                              rs.close();
  56.                              return true;
  57.                 }catch (Exception sqle){
  58.                   System.out.print(sqle.getMessage());
  59.                   return false;
  60.                 }
  61.            } else{
  62.             return false;
  63.            }
  64.   }
  65.   //分析页面转递的参数
  66.   public boolean getRequest(javax.servlet.http.HttpServletRequest newrequest) {
  67.        boolean flag = false;
  68.        try {
  69.         request = newrequest;
  70.         String ID = request.getParameter("userid");
  71.         if (ID!=null ){
  72.         userid = 0;
  73.         try{
  74.             userid = Long.parseLong(ID);
  75.             user.setId(userid);
  76.             }catch (Exception e){
  77.                  message = message + "你要修改的用户号出错!";
  78.             }
  79.          }
  80.          username = request.getParameter("username");
  81.          if (username==null || username.equals("")){
  82.               username = "";
  83.               message = message + "用户名为空!";
  84.               }
  85.               user.setUserName(getGbk(username));
  86.               String password = request.getParameter("passwd");
  87.               if (password==null || password.equals("")){
  88.                   password = "";
  89.                   message = message + "密码为空!";
  90.                   }
  91.               String pwdconfirm = request.getParameter("passconfirm");
  92.               if (!password.equals(pwdconfirm)){
  93.                  message = message + "确认密码不相同!";
  94.                  }
  95.               user.setPassWord(getGbk(password));
  96.               String names = request.getParameter("names");;
  97.               if (names==null){
  98.                    names = "";
  99.                    }
  100.               user.setNames(getGbk(names));
  101.               String sex = request.getParameter("sex");
  102.               user.setSex(getGbk(sex));
  103.               String address = request.getParameter("address");
  104.               if (address == null){
  105.                    address = "";
  106.                   }
  107.               user.setAddress(getGbk(address));
  108.               String post = request.getParameter("post");
  109.               if (post == null){
  110.                   post = "";
  111.                  }
  112.                user.setPost(getGbk(post));
  113.               String phone = request.getParameter("phone");
  114.               if (phone== null){
  115.                   phone = "";
  116.               }
  117.              user.setPhone(phone);
  118.              String email = request.getParameter("email");
  119.              if (email == null){
  120.                   email = "";
  121.                   }
  122.               user.setEmail(getGbk(email));
  123.              String IP = request.getRemoteAddr();
  124.              user.setRegIpAddress(IP);
  125.              if (message.equals("")){
  126.                   flag = true;
  127.                   }
  128.                return flag;
  129.         }catch (Exception e){
  130.             return flag;
  131.             }
  132.  }
  133.  //查询书店所有的用户
  134. public boolean get_alluser() throws Exception {
  135.       sqlStr = "select count(*) from shop_user";    //取出记录数
  136.       int rscount = pageSize;
  137.       try{
  138.         DataBase db = new DataBase();
  139.         db.connect();
  140.         stmt = db.conn.createStatement ();
  141.         ResultSet rs1 = stmt.executeQuery(sqlStr);
  142.         if (rs1.next()) recordCount = rs1.getInt(1);
  143.         rs1.close();
  144.         }catch (SQLException e){
  145.           System.out.print("count:"+e.getMessage());
  146.           return false;
  147.         }
  148.                                 //设定有多少pageCount
  149.         if (recordCount < 1)
  150.            pageCount = 0;
  151.         else
  152.            pageCount = (int)(recordCount - 1) / pageSize + 1;
  153.                                 //检查查看的页面数是否在范围内
  154.         if (page < 1)
  155.            page = 1;
  156.         else if (page > pageCount)
  157.           page = pageCount;
  158.           rscount = (int) recordCount % pageSize;  // 最后一页记录数
  159.                                 //sql为倒序取值
  160.           sqlStr = "select  * from shop_user ";
  161.           if (page == 1){
  162.              sqlStr = sqlStr + " order by Id desc limit 0,"+pageSize;
  163.              } else {
  164.                sqlStr = sqlStr + " order by Id desc limit "+(recordCount-pageSize*page) +","+(recordCount - pageSize * (page-1));
  165.                }
  166.         try{
  167.           DataBase db = new DataBase();
  168.           db.connect();
  169.           stmt = db.conn.createStatement ();
  170.           rs = stmt.executeQuery(sqlStr);
  171.           userlist = new Vector();
  172.           while (rs.next()){
  173.                 user user = new user();
  174.                 user.setId(rs.getLong("id"));
  175.                 user.setUserName(rs.getString("username"));
  176.                 user.setPassWord(rs.getString("password"));
  177.                 user.setNames(rs.getString("names"));
  178.                 user.setSex(rs.getString("sex"));
  179.                 user.setAddress(rs.getString("address"));
  180.                 user.setPhone(rs.getString("Phone"));
  181.                 user.setPost(rs.getString("post"));
  182.                 user.setEmail(rs.getString("email"));
  183.                 user.setRegTime(rs.getString("regtime"));
  184.                 user.setRegIpAddress(rs.getString("RegIpaddress"));
  185.                 userlist.addElement(user);
  186.                 }
  187.                 rs.close();
  188.                  return true;
  189.             }catch (SQLException e){
  190.               System.out.print(e.getMessage());
  191.               return false;
  192.               }
  193. }
  194. //修改用户
  195. public boolean update(HttpServletRequest req) throws Exception {
  196. if (getRequest(req)){
  197. sqlStr = "update shop_user set ";
  198. sqlStr = sqlStr + "username = '" + dataFormat.toSql(user.getUserName()) + "',";
  199. sqlStr = sqlStr + "password = '" + dataFormat.toSql(user.getPassWord()) + "',";
  200. sqlStr = sqlStr + "Names = '" + dataFormat.toSql(user.getNames()) + "',";
  201. sqlStr = sqlStr + "sex = '" + dataFormat.toSql(user.getSex()) + "',";
  202. sqlStr = sqlStr + "address = '" + dataFormat.toSql(user.getAddress()) + "',";
  203. sqlStr = sqlStr + "phone = '" + dataFormat.toSql(user.getPhone()) + "',";
  204. sqlStr = sqlStr + "post = '" + dataFormat.toSql(user.getPost()) + "',";
  205. sqlStr = sqlStr + "Email = '" + dataFormat.toSql(user.getEmail()) + "' ";
  206. sqlStr = sqlStr + " where id = '" + user.getId() + "'";
  207. try{
  208.                   DataBase db = new DataBase();
  209.                   db.connect();
  210.                   stmt = db.conn.createStatement ();
  211.                   stmt.execute(sqlStr);
  212.                   return true;
  213.     } catch (SQLException e){
  214.       return false;
  215.        }
  216. } else {
  217.        return false;
  218.        }
  219. }
  220. //删除用户
  221.  public boolean delete( long aid ) throws Exception {
  222.     sqlStr = "delete from shop_user where id = "  + aid ;
  223.     try{
  224.       DataBase db = new DataBase();
  225.       db.connect();
  226.       stmt = db.conn.createStatement ();
  227.       stmt.execute(sqlStr);
  228.       return true;
  229.        }catch (SQLException e){
  230.         System.out.println(e);
  231.         return false;
  232.         }
  233. }
  234. //查询指定id的用户,用于支持页面的查看详细资料请求
  235.  public boolean  getUserinfo(long newid ) throws Exception {
  236.     try{
  237.       sqlStr="select  * from shop_user where Id = " + newid ;
  238.       DataBase db = new DataBase();
  239.       db.connect();
  240.       stmt = db.conn.createStatement ();
  241.       rs = stmt.executeQuery(sqlStr);
  242.       userlist = new Vector();
  243.       while (rs.next()){
  244.         user.setId(rs.getLong("id"));
  245.         System.out.print(rs.getLong("id"));
  246.         user.setUserName(rs.getString("username"));
  247.         user.setPassWord(rs.getString("password"));
  248.         user.setNames(rs.getString("names"));
  249.         user.setSex(rs.getString("sex"));
  250.         user.setAddress(rs.getString("address"));
  251.         user.setPhone(rs.getString("Phone"));
  252.         user.setPost(rs.getString("post"));
  253.         user.setEmail(rs.getString("email"));
  254.         user.setRegTime(rs.getString("regtime"));
  255.         user.setRegIpAddress(rs.getString("RegIpaddress"));
  256.          userlist.addElement(user);
  257.          }
  258.          rs.close();
  259.          return true;
  260.         }catch (Exception e){
  261.           System.out.print(e.getMessage());
  262.           return false;
  263.         }
  264. }
  265.   public String getGbk( String str) {
  266.                try  {
  267.                 return new String(str.getBytes("ISO8859-1"));
  268.                 }catch (Exception e){
  269.                   return str;
  270.                   }
  271.           }
  272.   public int getPage() { //显示的页码
  273.                               return page;
  274.                       }
  275.                       public void setPage(int newpage) {
  276.                               page = newpage;
  277.                       }
  278.                       public int getPageSize(){ //每页显示的图书数
  279.                               return pageSize;
  280.                       }
  281.                       public void setPageSize(int newpsize) {
  282.                               pageSize = newpsize;
  283.                       }
  284.                       public int getPageCount() { //页面总数
  285.                               return pageCount;
  286.                       }
  287.                       public void setPageCount(int newpcount) {
  288.                               pageCount = newpcount;
  289.                       }
  290.                       public long getRecordCount() {
  291.                               return recordCount;
  292.                       }
  293.                       public void setRecordCount(long newrcount) {
  294.                               recordCount= newrcount;
  295.                       }
  296.                       public String getMessage() {
  297.                               return message;
  298.                       }
  299.                       public void setMessage(String msg) {
  300.                               message = msg;
  301.                       }
  302.                       public void setUserid(long uid) {
  303.                               userid = uid;
  304.                       }
  305.                       public long getUserid() {
  306.                               return userid;
  307.                       }
  308.                       public void setUserName(String uName) {
  309.                               username = uName;
  310.                       }
  311.                       public String getUserName() {
  312.                               return username;
  313.                       }
  314.                       public Vector getUserlist() {
  315.                            return userlist;
  316.                        }
  317. };