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 易不易 閱讀(103) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 南丰县| 蛟河市| 多伦县| 灵台县| 越西县| 大丰市| 麻阳| 沛县| 当涂县| 宾阳县| 贡嘎县| 肇州县| 宣化县| 抚宁县| 绥芬河市| 岢岚县| 教育| 仁怀市| 长泰县| 新绛县| 清原| 文山县| 寿光市| 柯坪县| 汕尾市| 抚宁县| 黄骅市| 安宁市| 梨树县| 衡阳县| 新巴尔虎右旗| 新兴县| 临夏市| 桓台县| 红河县| 乌鲁木齐市| 绥宁县| 彭州市| 凤山县| 安龙县| 县级市|