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

数据库编程

开发平台:

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