Package Library;
Import java.sql. *; import java.util.Vector; import java.lang. *; import javax.servlet.http.httpservletRequest; import library. *;
/ ** * Title: * description: * Copyright: * company: * @Author: * @version 1.0 * /
Public class userdb {private user user = new user (); // New User Object Private Javax.Servlet.http.httpservletRequest Request; // Create Page Request Private String Message = ""; // Error Information Tips Private Long UserId = 0; // Register the user ID private int id; private string username; // Return User Name Private Vector UserList; // Display User List Vector Array Private INT Page = 1; // Displayed Page Private Int PageSize = 6; // Various private int pagecount = 0; // Total number of pages private int recordcount = 0; // Total number of records of the query Private int deptid; private string sqlstr; private project ket; private OpenDB Userdb; privat
Public userdb () throws exception {} public vector getUserList () {return userlist;}
Public String getGBK (Str.GetBytes ("ISO8859-1"));} catCh (Exception E) {Return Str;}} // Decompose the information from the page form PUBLIC BOOLEAN getRequest (javax.servlet.http.HttpServletRequest newrequest) {boolean flag = false; try {request = newrequest; String ID = request.getParameter ( "userid"); if (! ID = null) {userid = 0; try {userid = Long.parselong (id); user.setid;} catch (exception e) {message = message "You want to modify the user number error!";}} String username = request.getParameter ("username") ; If (username == null || username.equals (")) {username =" "; message = message " User name is empty! "; Returnaf (username); string Password = request.getParameter ("passwd"); if (password == null || Password.equals (")) {password ="; message = message "The password is empty!"; Return False;} String Pwdconfirm = Request.GetParameter ("passconfirm"); if (! Password.equals (pwdco NFIRM)) {Message = Message "Confirm the password is not the same!"; Return False;} User.SetPassword (getGBK (Password)); String Sex = Request.GetParameter ("SEX"); user.setsex (getGBK (SEX) String address = request.getParameter ("address"); if (address == null) {address = "";} user.setaddress (getGBK (address)); string dept = request.getParameter ("dept"); IF (dept! = null) {deptid = 0; try {deptid = integer.parseint (dept);
User.SetDeptid (DeptID);} catch (Exception E) {// message = message "You want to modify the user number error!";}} User.SetDept (getGBK (dept)); string phone = request.getParameter "phone"); if (phone == null) {phone = "";} user.setphone (phone); string email = request.getParameter ("email"); if (email == null) {email = "" User.semail (GETGBK (EMAIL)); if (Message.Equals (")) {flag = true;} Return flag;} catCH (Exception E) {Return flag;}}
public boolean insert (HttpServletRequest req) throws Exception {if (getRequest (req)) {OpenDB userDB = new OpenDB (); sqlStr = "select * from users where username = '" user.getUserName () "'"; Rs = Userdb.executeQuery (SQLSTR); if (rs.next ()) {Message = Message "The username already exists!"; return false;} SQLSTR = "Insert Into Users (username, password, sex, address, phone , DEPT, Email, Regtime VALUES ('"; sqlstr = SQLSTR STRFORMAT.TOSQL (user.getusername ()) "', '"; sqlstr = SQLSTR Strformat.tosql (user.getpassword ()) "' , '"; Sqlstr = sqlstr strformat.tosql (user.getsex ()) "', '"; sqlstr = SQLSTR STRFORMAT.TOSQL (user.getaddress ()) "', '"; sqlstr = SQLSTR Strformat.tosql (user.getphone ()) ""; sqlstr = SQLSTR STRFORMAT.TOSQL (String.Valueof (user.getdeptid ())) ", '"; sqlstr = SQLSTR STRFORMAT.TOSQL (User .getemail ()) "', getdate ()"; system.out.println (SQLSTR); try {r = userdb.executeQuery (SQLSTR); SQLSTR = "Select ID from users where UserName = '" user.getusername () "' "; rs = userdb.executeQuery (sqlstr); while (rs.next ()) {userid = rs.getlong (1);} Userdb.closeconn (); return true;} catch (Exception e) {return false;}} else {return false;}} public boolean update (HttpServletRequest req) throws Exception {if (getRequest (req)) {sqlStr = "update users set"; sqlStr = sqlStr "
Username = '" strformat.tosql (user.getusername ()) " "; sqlstr = SQLSTR " password =' " strformat.tosql (user.getpassword ()) "; "sqlstr = SQLSTR "SEX = '" strformat.tosql (user.getsex ()) ""; sqlstr = SQLSTR "address ='" strformat.tosql (user.getaddress ()) ";" SQLSTR = SQLSTR "phone = '" strformat.tosq (user.getphone ()) ""; sqlstr = sqlstr "dept =" strformat.tosql (String.Valueof (user.getdeptid ())) ","; SQLSTR = SQLSTR "email = '" strformat.tosql (user.getemail ()) "'"; sqlstr = SQLSTR "where id = '" user.getid () "'"; try {OpenDB userDB = new OpenDB (); System.out.println (sqlStr); userDB.executeUpdate (sqlStr); userDB.CloseConn (); return true;} catch (Exception e) {return false;}} else {System .out.println; Return False;}} public boolean delete (long Aid) throws exception {
SQLSTR = "delete from users where id =" AID; try {OPENDB Userdb = new OPENDB (); Userdb.executeQuery (SQLSTR); Userdb.closeConn (); return true;} catch (Exception E) {System.out. println (e); return false;}} public boolean execute (HttpServletRequest res) throws Exception {request = res; String PAGE = request.getParameter ( "page"); // p String classid = request.getParameter ( "classid") ; // Classification ID number string keyword = request.getParameter ("keyword"); // Query Keyword string id = request.getParameter ("userid"); if (classid == null) classid = ""; if (Keyword) == NULL) Keyword = ""; if (page == null) Page = ""; if (id == null) id = ""; keyword = getGBK (keyword) .touppercase (); try {page = integer. PARSEINT (PAGE);} catch (number = 1;} try {id = integer.parseint (id);} catch (number = 1;}) {ID = 1;} IF (Page <0) Page = 1; String consTION1 = "and a.dept = '" classid "" "; string cyper2 =" and upper (a.username) Like'% keyword "% '" String consTION3 = "and a.id = '" ID ""; sqlstr = "Select A.ID, Username, Password, SEX, Address, Phone, DEPT, Email, Regtime, L_Secename from Users A, L_SECTIONOffice B Where A .dept = B.ID "; if (keyword.equals (") && id.equals (")) {if (! ClassID.Equals (")) SQLSTR = consTION1;} else {if (! iD.equals (")) SQLSTR = CONTION3; Else Sqlstr = contion2;
} Try {system.out.println (SQLSTR); OpenDB userdb = new OPENDB (); rs = userdb.executeQuery (sqlstr); rs.last (); recordcount = rs.getrow (); if (RecordCount <0) PageCount = 0; Else PageCount = (RecordCount Pagesize-1) / PageSize; if (page> pagecount) Page = pagecount; rs.absize 1); int i = 0; userlist = new Vector (); WHILE (I } Public boolean getUserinfo (long newid) throws Exception {try {sqlStr = "select a.id, username, password, sex, address, phone, dept, email, regtime, L_OfficeName from users a, L_SectionOffice b where a.dept = b .id and a.id = " newid; OpenDB userdb = new OPENDB (); rs = userdb.executeQuery (sqlstr); userlist = new vector (); while (rs.next ()) {user.setID (RS. Getlong ("ID")); user.setusername (RS.GetString ("UserName"); user.setpassword (rs.getstring ("password"); user.setsex (rs.getstring ("sex"); User.Setaddress (rs.getstring ("rgetstring (" phone "); user.setdept (rs.getstring (" l_officename "); user.seTetring (rs.getstring "email"); user.setRegTime (RS.GTIME ")); user.SetDeptid (Rs.GetInt (" dept ")); userlist.addelement (user);} Userdb.closeConn (); Return True (SQLEXCEPTION E) {Return False;}} public boolean isadmin (string dept) throws exception {try {sqlstr = "SELECT * FROM L_sectionOffice where l_officename = '" dept " "; OPENDB Userdb = new OPENDB (); int admin = 0; rs = userdb.executeQuery (sqlstr); system.out.println (sqlstr); while (rs.next () ) {Admin = rs.Getint ("l_lend");} Userdb.closeconn (); if (admin == 1) Return true; else returnaf false;} catch (sqlexception e) {userdb.closeconn (); returnaf }} Public string getMessage () {Return Message;} public void setmessage (string msg) {message = msg; Public void setUserid (long) {userid = uid;} public long getUserid () {return userid;} public void setname (String uname) {username = uname;} Public string getname () {return username;} Public int getPage () {// Display page number return page;} public void setpage (int newpage) {Page = newpage; Public int getpagesize () {// The number of books displayed per page return pagesize;} public void setpagesize (int newpsize) {pagesize = newpsize;} Public int getPageCount () {// Total number Return PageCount;} public void setPageCount (int newpcount) {pagecount = newpcount;} Public long getRecordCount () {return recordcount;} public void setRecordcount (int newrcount) {recordcount = newrcount;}}