天天看點

資訊系統開發實踐-任務釋出系統(TaskMS)(八)

JavaBeans的設計和實作

(五)User類和UserInfor類是和使用者相關的兩個類。User類是用于管理使用者登入的類,UserInfor是關于使用者個人資訊的類。詳情如下:

public class User {

public String UserName, Password, Unit;//使用者名、密碼、所屬部門(機關)

public boolean IsAdmin;//是否管理者

/* 通過使用者名确定使用者 */

public static User FindByName(String _UserName){

DBConnect dbc = null;

User user = new User();

try {

dbc = new DBConnect();

dbc.prepareStatement("SELECT * FROM UserTable WHERE UserName = ?");

dbc.setString(1, _UserName);

ResultSet rs = dbc.executeQuery();

if (rs.next()){

user.setUserName(rs.getString("UserName"));

user.setPassword(rs.getString("Password"));

user.setIsAdmin(rs.getBoolean("IsAdmin"));

user.setUnit(rs.getString("Unit"));

}else

{user = null;}}

catch (Exception e)

{System.err.println(e);

}finally

{try

{dbc.close();

}catch (Exception e)

{e.printStackTrace();

user = null;

}}

return user;}

/*驗證登入*/

public static boolean ChkLogin(String _UserName, String _Password)

throws Exception{

DBConnect dbc = null;

boolean IsExist = false;

try{

String EncPassword;

dbc = new DBConnect();

dbc.prepareStatement("SELECT PassWord FROM UserTable WHERE UserName = ? ");

dbc.setString(1, _UserName);

ResultSet rs = dbc.executeQuery();

if (!rs.next())

{IsExist = false;

}else{if (_Password.equals(rs.getString("PassWord")))

IsExist = true;

}}catch (Exception e)

{System.err.println(e);

}finally

{try

{dbc.close();}

catch (Exception e)

{e.printStackTrace();

}}

return IsExist;}

/*修改密碼*/

public static String ModifyPassword(String _UserName, String _NewPassword)

{DBConnect dbc = null;

try

{

dbc = new DBConnect();

dbc.prepareStatement("UPDATE UserTable SET PassWord=? WHERE UserName=?");

dbc.setString(1, _NewPassword);

dbc.setString(2, _UserName);

dbc.executeUpdate();

dbc.close();

return "1";}

catch (Exception e){

e.printStackTrace();

return e.toString();}}

UserInfor類詳情:

public class UserInfor {

public String Mobile,Name,Sex,Adress,School,Discipline,Depart;//手機号,姓名,性别,家庭住址,學校位址,專業,部門(機關)

public java.sql.Date Birthday,RegDate;//出生日期,注冊日期

public int GetS,UseS,Ulevel;//獲得的積分,使用的積分,使用者級别

/*列出同一機關的人員清單*/

public static Vector ListByDepart(String _Depart) {

DBConnect dbc = null;

Vector UserInforVector = new Vector();

String strSQL = "SELECT * FROM UserInfor where Depart=? ORDER BY RegDate DESC";

try {

dbc = new DBConnect();

dbc.prepareStatement(strSQL);

dbc.setString(1, _Depart);

//dbc.setString(2, _Depart);

ResultSet rs = dbc.executeQuery();

while (rs.next()) {

UserInfor userInfor = new UserInfor();

userInfor.setMobile(rs.getString("Mobile"));

userInfor.setName(rs.getString("Name"));

userInfor.setSex(rs.getString("Sex"));

userInfor.setBirthday(rs.getString("Birthday"));

userInfor.setRegDate(rs.getString("RegDate"));

userInfor.setUlevel(rs.getInt("Ulevel"));

userInfor.setAdress(rs.getString("Adress"));

userInfor.setGetS(rs.getInt("GetS"));

userInfor.setUseS(rs.getInt("UseS"));

userInfor.setSchool(rs.getString("School"));

userInfor.setDiscipline(rs.getString("Discipline"));

userInfor.setDepart(rs.getString("Depart"));

UserInforVector.add(userInfor);

}} catch (Exception e) {

System.err.println(e);

} finally {

try {

dbc.close();

} catch (Exception e) {

e.printStackTrace();

}}

return UserInforVector;

}

/*更新使用者級别*/

public static String UpdateUlevel(String _Mobile, int _Ulevel) {

DBConnect dbc = null;

try {

dbc = new DBConnect();

dbc.prepareStatement("UPDATE UserInfor SET Ulevel=? WHERE Mobile=?");

dbc.setInt(1, _Ulevel);

dbc.setString(2, _Mobile);

dbc.executeUpdate();

dbc.close();

return "1";

} catch (Exception e) {

e.printStackTrace();

return e.toString();

}}

/*更新使用者得分*/

public static String UpdateGets(String _Mobile, int _Gets) {

DBConnect dbc = null;

try {

dbc = new DBConnect();

dbc.prepareStatement("UPDATE UserInfor SET Gets=? WHERE Mobile=?");

dbc.setInt(1, _Gets);

dbc.setString(2, _Mobile);

dbc.executeUpdate();

dbc.close();

return "1";

} catch (Exception e) {

e.printStackTrace();

return e.toString();

}}

/*更新使用者使用得分*/

public static String UpdateUseS(String _Mobile, int _UseS) {

DBConnect dbc = null;

try {

dbc = new DBConnect();

dbc.prepareStatement("UPDATE UserInfor SET UseS=? WHERE Mobile=?");

dbc.setInt(1, _UseS);

dbc.setString(2, _Mobile);

dbc.executeUpdate();

dbc.close();

return "1";

} catch (Exception e) {

e.printStackTrace();

return e.toString();

}}

/* 增加積分明細 */

public static String AddS(String _UNO,String _Mobile, String _Sort, int _Score,String _GDate,String _Note) {

DBConnect dbc = null;

try {

dbc = new DBConnect();

dbc.prepareStatement("INSERT INTO GetS (UNO,Mobile,Sort,Score,GetDate,Note) VALUES (?,?,?,?,?,?)");

dbc.setString(1, _UNO);

dbc.setString(2, _Mobile);

dbc.setString(3, _Sort);

dbc.setInt(4, _Score);

dbc.setDate(5, DataConvert.StrTosqlDate(_GDate));

dbc.setString(6, _Note);

dbc.executeUpdate();

dbc.close();

return "1";

} catch (Exception e) {

System.err.println(e);

return e.toString();}}}

繼續閱讀