op_user.java~32~
上传用户:sxlinghang
上传日期:2022-07-20
资源大小:1405k
文件大小:12k
源码类别:

数据库编程

开发平台:

Java

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