TestCode

          ??1 package ?onlyfun.caterpillar;
          ??2
          ??3 import ?javax.servlet. * ;
          ??4 import ?java.util. * ;
          ??5 import ?java.sql. * ;
          ??6
          ??7 public ? class ?Functions? {
          ??8 ???? private ? static ?Connection?conn? = ? null ;
          ??9
          ?10 ???? public ? static ? boolean ?filled_out(ServletRequest?req)? {
          ?11 ????????Enumeration?form_vars? = ?req.getParameterNames();
          ?12 ???????? while (form_vars.hasMoreElements())? {
          ?13 ????????????String?var? = ?(String)?form_vars.nextElement();
          ?14 ???????????? if (req.getParameter(var).length()? == ? 0 )
          ?15 ???????????????? return ? false ;
          ?16 ????????}

          ?17 ???????? return ? true ;
          ?18 ????}

          ?19
          ?20 ???? public ? static ? boolean ?valid_email(String?address)? {
          ?21 ???????? if (address.matches( " ^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$ " ))
          ?22 ???????????? return ? true ;
          ?23 ???????? else
          ?24 ???????????? return ? false ;
          ?25 ????}

          ?26
          ?27 ???? public ? static ?Connection?getDBConnection(ServletContext?context)? {
          ?28 ???????? if (conn? == ? null )? {
          ?29 ????????????String?sqluri? = ? " jdbc:mysql://localhost:3306/ " ? + ?context.getInitParameter( " sqldb " )? + ? " ?useUnicode=true&characterEncoding=big5 " ;
          ?30 ????????????String?sqluser? = ??context.getInitParameter( " sqluser " );
          ?31 ????????????String?sqlpwd? = ??context.getInitParameter( " sqlpwd " );
          ?32 ???????????? try ? {
          ?33 ????????????????Class.forName(context.getInitParameter( " sqldriver " ));
          ?34 ????????????????conn? = ?DriverManager.getConnection(sqluri,?sqluser,?sqlpwd);
          ?35 ????????????}

          ?36 ???????????? catch (Exception?e)? {
          ?37 ????????????}

          ?38 ????????}

          ?39
          ?40 ???????? return ?conn;
          ?41 ????}

          ?42
          ?43 ???? public ? static ? boolean ?login(ServletContext?application,?String?username,?String?passwd)? {
          ?44 ???????? try ? {
          ?45 ????????????Connection?conn? = ?getDBConnection(application);
          ?46 ????????????ResultSet?result? = ?conn.createStatement().executeQuery(
          ?47 ??????????????????? " SELECT?*?FROM?users?WHERE?username=' " ? + ?username? + ? " '?and?passwd=password(' " ? + ?passwd? + ? " ') " );
          ?48 ???????????? return ?result.next();
          ?49 ????????}

          ?50 ???????? catch (Exception?e)? {
          ?51 ????????}

          ?52 ????????????????????????????????????????????????????????????????????????????????????????????????
          ?53 ???????? return ? false ;
          ?54 ????}

          ?55
          ?56 ???? public ? static ? boolean ?change_password(ServletContext?application,?String?username,?String?old_password,?String?new_password)? {
          ?57 ???????? if (login(application,?username,?old_password))? {
          ?58 ????????????Connection?conn? = ?getDBConnection(application);
          ?59 ???????????? if (conn? == ? null )? {
          ?60 ???????????????? return ? false ;
          ?61 ????????????}

          ?62
          ?63 ???????????? try ? {
          ?64 ???????????????? int ?result? = ?conn.createStatement().executeUpdate( " UPDATE?users?SET?passwd=password(' " ? + ?new_password? + ? " ')?WHERE?username=' " ? + ?username? + ? " ' " );
          ?65
          ?66 ???????????????? if (result? == ? 0 )? {
          ?67 ???????????????????? return ? false ;
          ?68 ????????????????}

          ?69 ???????????????? else ? {
          ?70 ???????????????????? return ? true ;
          ?71 ????????????????}

          ?72
          ?73 ????????????}
          ? catch (SQLException?e)? {}
          ?74 ????????}

          ?75
          ?76 ???????? return ? false ;
          ?77 ????}

          ?78
          ?79 ???? public ? static ? boolean ?add_bm(ServletContext?application,?String?username,?String?new_url)? {
          ?80 ????????Connection?conn? = ?getDBConnection(application);
          ?81 ???????? if (conn? == ? null )? {
          ?82 ???????????? return ? false ;
          ?83 ????????}

          ?84 ???????? try ? {????????
          ?85 ???????????? // ?乓琌狡
          ?86 ????????????ResultSet?result? = ?conn.createStatement().executeQuery( " SELECT?*?FROM?bookmark?WHERE?username=' " ? + ?username? + ? " '?AND?bm_URL=' " ? + ?new_url? + ? " ' " );
          ?87
          ?88 ???????????? if (result.next())
          ?89 ???????????????? return ? false ;
          ?90
          ?91 ???????????? int ?dn? = ?conn.createStatement().executeUpdate( " INSERT?INTO?bookmark?VALUES(' " ? + ?username? + ? " ',' " ? + ?new_url? + ? " ') " );
          ?92
          ?93 ???????????? if (dn? == ? 0 )
          ?94 ???????????????? return ? false ;
          ?95 ????????}

          ?96 ???????? catch (SQLException?e) {
          ?97 ???????????? return ? false ;
          ?98 ????????}

          ?99 ???????? return ? true ;
          100 ????}

          101
          102 ???? public ? static ? boolean ?delete_bm(ServletContext?application,?String?username,?String?url)? {
          103 ????????Connection?conn? = ?getDBConnection(application);
          104 ???????? if (conn? == ? null )? {
          105 ???????????? return ? false ;
          106 ????????}

          107 ???????? try ? {
          108 ???????????? int ?dn? = ?conn.createStatement().executeUpdate( " DELETE?FROM?bookmark?WHERE?username=' " ? + ?username? + ? " '?AND?bm_URL=' " ? + ?url? + ? " ' " );
          109 ???????????? if (dn? == ? 0 )
          110 ???????????????? return ? false ;
          111 ????????}

          112 ???????? catch (SQLException?e) {
          113 ???????????? return ? false ;
          114 ????????}

          115 ???????? return ? true ;
          116 ????}

          117 }

          118
          posted on 2007-03-21 15:34 易不易 閱讀(102) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 北票市| 利川市| 上虞市| 新乡县| 澄江县| 横峰县| 壶关县| 奈曼旗| 太湖县| 栾城县| 瑞金市| 澄迈县| 于田县| 县级市| 五河县| 广水市| 思茅市| 夏河县| 科技| 潼关县| 广元市| 祁阳县| 孙吴县| 铜陵市| 闻喜县| 太原市| 页游| 台江县| 旬阳县| 大新县| 阿鲁科尔沁旗| 开阳县| 英山县| 怀远县| 德江县| 高雄县| 中江县| 呼伦贝尔市| 广灵县| 凤庆县| 丹凤县|