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

数据库编程

开发平台:

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.                                 {        System.out.print("count:"+e.getMessage());
  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 ";
  188.                                 if (page == 1)
  189.                                 {
  190.                                         sqlStr = sqlStr + " order by Id desc limit 0,"+pageSize;
  191.                                 } else {
  192.                                         sqlStr = sqlStr + " order by Id desc limit "+(recordCount-pageSize*page) +","+(recordCount - pageSize * (page-1));
  193.                                 }
  194.                                 try
  195.                                 {DataBase db = new DataBase();
  196.                                 db.connect();
  197.                                 stmt = db.conn.createStatement ();
  198.                                         rs = stmt.executeQuery(sqlStr);
  199.                                         userlist = new Vector();
  200.                                         while (rs.next()){
  201.                                                 user user = new user();
  202.                                                 user.setId(rs.getLong("id"));
  203.                                                 user.setUserName(rs.getString("username"));
  204.                                                 user.setPassWord(rs.getString("password"));
  205.                                                 user.setNames(rs.getString("names"));
  206.                                                 user.setSex(rs.getString("sex"));
  207.                                                 user.setAddress(rs.getString("address"));
  208.                                                 user.setPhone(rs.getString("Phone"));
  209.                                                 user.setPost(rs.getString("post"));
  210.                                                 user.setEmail(rs.getString("email"));
  211.                                                 user.setRegTime(rs.getString("regtime"));
  212.                                                 user.setRegIpAddress(rs.getString("RegIpaddress"));
  213.                                                 userlist.addElement(user);
  214.                                         }
  215.                                         rs.close();
  216.                                         return true;
  217.                                 }
  218.                                 catch (SQLException e)
  219.                                 {        System.out.print(e.getMessage());
  220.                                         return false;
  221.                                 }
  222.                         }
  223.                   public boolean update(HttpServletRequest req) throws Exception {
  224.                            if (getRequest(req)){
  225.                                                 sqlStr = "update shop_user set ";
  226.                                                 sqlStr = sqlStr + "username = '" + dataFormat.toSql(user.getUserName()) + "',";
  227.                                                 sqlStr = sqlStr + "password = '" + dataFormat.toSql(user.getPassWord()) + "',";
  228.                                                 sqlStr = sqlStr + "Names = '" + dataFormat.toSql(user.getNames()) + "',";
  229.                                                 sqlStr = sqlStr + "sex = '" + dataFormat.toSql(user.getSex()) + "',";
  230.                                                 sqlStr = sqlStr + "address = '" + dataFormat.toSql(user.getAddress()) + "',";
  231.                                                 sqlStr = sqlStr + "phone = '" + dataFormat.toSql(user.getPhone()) + "',";
  232.                                                 sqlStr = sqlStr + "post = '" + dataFormat.toSql(user.getPost()) + "',";
  233.                                                 sqlStr = sqlStr + "Email = '" + dataFormat.toSql(user.getEmail()) + "' ";
  234.                                                 sqlStr = sqlStr + " where id = '" + user.getId() + "'";
  235.                                                 try
  236.                                                 { DataBase db = new DataBase();
  237.                                                         db.connect();
  238.                                                         stmt = db.conn.createStatement ();
  239.                                                         stmt.execute(sqlStr);
  240.                                                         return true;
  241.                                                 }
  242.                                                 catch (SQLException e)
  243.                                                 {
  244.                                                         return false;
  245.                                                 }
  246.                                         } else {
  247.                                                 return false;
  248.                                         }
  249.                                 }
  250.                                 public boolean delete( long aid ) throws Exception {
  251.                                         sqlStr = "delete from shop_user where id = "  + aid ;
  252.                                         try
  253.                                         {       DataBase db = new DataBase();
  254.                                                 db.connect();
  255.                                                 stmt = db.conn.createStatement ();
  256.                                                 stmt.execute(sqlStr);
  257.                                                 return true;
  258.                                         }
  259.                                         catch (SQLException e)
  260.                                         {
  261.                                                 System.out.println(e);
  262.                                                 return false;
  263.                                         }
  264.                                 }
  265.                                 public boolean getUserinfo(long newid ) throws Exception {
  266.                                         try
  267.                                         {
  268.                                                 sqlStr="select  * from shop_user where Id = " + newid ;
  269.                                                 DataBase db = new DataBase();
  270.                                                 db.connect();
  271.                                                 stmt = db.conn.createStatement ();
  272.                                                 rs = stmt.executeQuery(sqlStr);
  273.                                                 userlist = new Vector();
  274.                                                 while (rs.next()){
  275.                                                         user.setId(rs.getLong("id"));
  276.                                                         user.setUserName(rs.getString("username"));
  277.                                                         user.setPassWord(rs.getString("password"));
  278.                                                         user.setNames(rs.getString("names"));
  279.                                                         user.setSex(rs.getString("sex"));
  280.                                                         user.setAddress(rs.getString("address"));
  281.                                                         user.setPhone(rs.getString("Phone"));
  282.                                                         user.setPost(rs.getString("post"));
  283.                                                         user.setEmail(rs.getString("email"));
  284.                                                         user.setRegTime(rs.getString("regtime"));
  285.                                                         user.setRegIpAddress(rs.getString("RegIpaddress"));
  286.                                                         userlist.addElement(user);
  287.                                                 }
  288.                                                 rs.close();
  289.                                                 return true;
  290.                                         }
  291.                                         catch (Exception e)
  292.                                         {         System.out.print(e.getMessage());
  293.                                                 return false;
  294.                                         }
  295.                                 }
  296.   public String getGbk( String str) {
  297.                try  {
  298.                 return new String(str.getBytes("ISO8859-1"));
  299.                 }catch (Exception e){
  300.                   return str;
  301.                   }
  302.                 }
  303.                 public int getPage() { //显示的页码
  304.                               return page;
  305.                       }
  306.                       public void setPage(int newpage) {
  307.                               page = newpage;
  308.                       }
  309.                       public int getPageSize(){ //每页显示的图书数
  310.                               return pageSize;
  311.                       }
  312.                       public void setPageSize(int newpsize) {
  313.                               pageSize = newpsize;
  314.                       }
  315.                       public int getPageCount() { //页面总数
  316.                               return pageCount;
  317.                       }
  318.                       public void setPageCount(int newpcount) {
  319.                               pageCount = newpcount;
  320.                       }
  321.                       public long getRecordCount() {
  322.                               return recordCount;
  323.                       }
  324.                       public void setRecordCount(long newrcount) {
  325.                               recordCount= newrcount;
  326.                       }
  327.                       public String getMessage() {
  328.                               return message;
  329.                       }
  330.                       public void setMessage(String msg) {
  331.                               message = msg;
  332.                       }
  333.                       public void setUserid(long uid) {
  334.                               userid = uid;
  335.                       }
  336.                       public long getUserid() {
  337.                               return userid;
  338.                       }
  339.                       public void setUserName(String uName) {
  340.                               username = uName;
  341.                       }
  342.                       public String getUserName() {
  343.                               return username;
  344.                       }
  345.                       public Vector getUserlist() {
  346.                            return userlist;
  347.                        }
  348.               };