This article uses the database operation function module source code, please refer to my article.
http://www.9cbs.net/develop/read_article.asp?id=23713
Package skydev.modules.accounts.data;
Import java.sql. *; import java.util. *; import skydev.modules.data. *; import javax.crypto. *;
Public class user extends dbobject {public user () {// super (New SQLServerConnectionFactory ("Localhost", 1433, "THESCHOOL", "SA", ""); super ();}
/ ** * Add a user, if the emaildress has been registered, then the registration failed, does not modify the database, return -1 * Otherwise, returning new add-added Userid * @Param EmailadDress * @Param password * @Param fullname * @Param address1 * address2 * @param city * @param province * @param zipCode * @param homePhone * @param country * @return add @param successful return new UserID, otherwise it returns -1 * / public int create (String emailAddress, byte [] password , String fullName, String address1, String address2, String city, String province, String zipCode, String homePhone, String country) {Connection con = this.getConnection (); CallableStatement cs = null; int userID = -1;
// Detect whether the current EmailAddress has been registered, if you register if it is registered, you cannot register again (this.testemailaddress (Emailaddress) == false) {return userid;}
Try {cs = con?preparecall ("{call sp_accounts_createuser (?,?,?,?,?,?,?,?,?,?)}"); cs.setstring (1, emailaddress); cs. SetBytes (2, password); cs.setstring (3, fulname); cs.setstring (4, address1); cs.setstring (5, address2); cs.setstring (6, city); cs.setstring (7, province ); Cs.setstring (8, zipcode); cs.setstring (9, homphone); cs.setstring (10, country); cs.registeroutparameter (11, types.integer); @ Userid cs.execute (); Userid = cs.getint (11);} catch (sqlexception ex) {} finally {return userid;}}
/ ** * More shape already existing user information, if the userid does not exist, do not make any modifications * @Param userid * @Param emaildress * @Param password * @Param fullname * @Param address1 * @Param address2 * @Param City * @param province * @param zipCode * @param homePhone * @param country * @return * / public boolean update (int userID, String emailAddress, byte [] password, String fullName, String address1, String address2, String city, String province String Zipcode, String Homephone, String Country) {
Callablestatement (); callablestatement cs = null; boolean ret = false; try {cs = con?preparecall ("{call sp_accounts_updateuser (?,?,?,?,?,?,?,?, ?,?)} "); Cs.setstring (1, emaildress); cs.setBytes (2, password); cs.setstring (3, fullname); cs.setstring (4, address1); cs.setstring (5, Address2); cs.setstring (6, city); cs.setstring (7, province); cs.setstring (8, zipcode); cs.setstring (9, homephone); cs.setstring (10, country); cs. Setint (11, userid); cs.execute (); ret = true;} cat = false;} finally {return}}}}
/ ** * Delete the user information of the specified user ID. If you delete an exception, you will be able to delete * @Param userid * @return * / public boolean delete (int userid) {connection con = this.getConnection (); CallableStatement cs = null; boolean ret = false; try {cs = Con.PrepareCall ("{Call sp_accounts_deleteuseuser (?)}"); cs.setint (1, userid); cs.executeUpdate (); Ret = true;
} Cat = false;} finally {return}}}
/ ** * Check the login user name and password * @Param emaildress * @Param password * @Return returns UserID, if the login failed, returned -1; * / public int validatelogin (String emailDress, Byte [] password) {Connection Con = this.getConnection (); callablestatement cs = null; int userid = -1; try {cs = con?preparecall ("{? = call sp_accounts_validatelogin (?,?)}"); cs.setstring (2, emailaddress) Cs.setbytes; cs.registeroutparameter (1, types.integer); @ Userid cs.execute (); userid = cs.getint (1);} catch (sqlexception ex) {} finally { Return UserId;}}
Public Boolean TestPassword (int userid, byte [] encpassword) {connection con = this.getConnection (); callablestatement cs = NULL;
Try {cs = con.preparecall ("{? = call sp_accounts_testpassword (?,?)}"); cs.setint (2, userid); cs.setbytes (3, encpassword); cs.registeroutparameter (1, Types.integer ); @ Userid cs.execute (); if (cs.getint (1) == 1) {// password qualified Return true;} else {return false;}} catch (sqlexception ex) {Return False;} Catch (Exception E) {Return False;}
}
Public Boolean TestemailAddress (String Emailaddress) {
CONNECTION Con = THITCONNECTION (); CALLABLESTATEMENT CS = NULL;
Try {cs = con?preparecall ("{? = call sp_accounts_teestemailaddress (?)}"); cs.setstring (2, emailaddress); cs.registeroutparameter (1, types.integer); @ userid cs.execute () ; If (CS.Getint (1) == 1) {// This emaildDress can register Return true;} else {return false;}} catch (sqlexception ex) {returnaf false;} catCH (Exception E) {Return False; }
Public HashMap GetUserroles (int Userid) throws sqlexception {connection con = this.getConnection (); Callablestatement cs = null; resultset = null; hashmap hm = new hashmap ();
Try {cs = con?preparecall ("{call sp_accounts_getuserroles (?)}"); cs.setint (1, userid); rs = cs.executeQuery (); while (rs.next ()) {hm.put (new Integer (CS.Getint ("RoleID")), cs.getstring ("description"));}} catch (sqlexception ex) {system.out.println ("Error: Hashmap getUserroles); System. Out.println (ex);} finally {return hm;}
}
// returns an ArrayList to illustrate a list of all user-based licensing its role membership conferred // need to manage public ArrayList getEffectivePermissionList (int userID) throws SQLException {// sp_Accounts_GetEffectivePermissionList ArrayList al = new ArrayList (); Connection con = this. GetConnection (); callablestatement cs = null; ResultSet RS = NULL;
Try {cs = con?prepareCall ("{call sp_accounts_geteffectivePermissionList (?)); cs.setint (1, userid); rs = cs.executeQuery ();
While (rs.next ()) {al.add (new integer ("permissionid")));}} catch (sqlexception ex) {} finally {return al;}}
public ArrayList getUserList () {Connection con = this.getConnection (); CallableStatement cs = null; ResultSet rs = null; ArrayList al = new ArrayList (); try {cs = con.prepareCall ( "{call sp_Accounts_GetUserList ()}") Rs = cs.executeQuery (); while (rs.next ()) {al.add (NEWEGER ("UserID"))));}} catch (sqlexception ex) {} finally {return al; }
// Basic idea: Save the form of data usage in the sword, field name as a key, field value as KeyValue Public Hashmap Retrive (int Userid) throws sqlexception {// Retrieve specified user's details sp_accounts_getuserDetails HashMap HM = New HashMap (); connection con = this.getConnection (); Callablestatement cs = null; ResultSet RS = NULL;
Try {cs = con?preparecall ("{call sp_accounts_getuserdetails); cs.setint (1, userid); rs = cs.executeQuery (); while (rs.next ()) {hm.put (" UserID ", NEW INTEGER (" UserID "))); HM.PUT (" EmailAddress ", RS.GetString (" EmailAddress ")); HM.PUT (" Fullname ", Rs.getstring (" Fullname " )); Hm.put ("address1", rs.getstring ("address1")); hm.put ("address2", rs.getstring ("address2")); hm.put ("city", rs.getstring ("City")); hm.put ("province", rs.getstring ("province")); hm.put ("zipcode", rs.getstring ("zipcode")); HM.PUT ("Homephone" rs.getstring ("homephone")); hm.put ("country", rs.getstring ("country"));}} catch (sqlexception ex) {system.out.println (ex);} finally {return HM;}}
public HashMap retrive (String emailAddress) {HashMap hm = new HashMap (); Connection con = this.getConnection (); CallableStatement cs = null; ResultSet rs = null; try {cs = con.prepareCall ( "{call sp_Accounts_GetUserDetailsByEmail ()? } "); Cs.setstring (1, emailaddress); RS = cs.executeQuery ();
While (Rs.Next ()) {HM.PUT ("UserID", New Integer ("UserID")))); HM.PUT ("Emailaddress", RS.getstring ("EmailAddress"); HM .put ("fullname", RS.GetString ("fulname")); HM.PUT ("Address1", RS.GetString ("address1")); HM.PUT ("Address2", RS.Getstring ("Address2" )); Hm.put ("city", rs.getstring ("city")); hm.put ("province", rs.getstring ("province")); hm.put ("zipcode", RS.getstring ("Zipcode")); hm.put ("Homephone", RS.GetString ("Homephone")); HM.PUT ("Country", RS.GetString ("Country"));}} Catch (Sqlexception EX) {System.out.println (ex);} finally {return hm;}}
}
User module only