<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 70,comments - 408,trackbacks - 0

    package net;

     

    import java.sql.Connection;

    import java.sql.Date;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.sql.Statement;

    import java.util.ArrayList;

    import java.util.Calendar;

    import java.util.Iterator;

    import java.util.Locale;

     

    import javax.naming.InitialContext;

    import javax.naming.NamingException;

    import javax.sql.DataSource;

    /**

     * @author 張宏亮

     * @Date 2006-3-1

     * version 1.0

     * explain 通用方法的封裝類

     * Email it5719@163.com

     */

    public class ActionUtil {

           /**

         * 截取中文字符串的方法

         * @param str 要截取的字符串,bytes截取之后每個(gè)串的字節(jié)數(shù)

         * @return 包含截串的結(jié)果的ArrayList

         * @throws Exception 截取漢字異常

         */

           public static ArrayList splitString(String str,int bytes) throws Exception{

                  ArrayList array = new ArrayList();

                  try {

                         String splitStr = str;

                         int splitByte = bytes;

                         int loopCount = 0;

                         loopCount = (splitStr.length()%splitByte==0)?(splitStr.length()/splitByte):(splitStr.length()/splitByte+1);

                         //System.out.println("字符串的長(zhǎng)度是"+splitStr.length()+",要截取成"+loopCount+",因?yàn)槊慷涡枰厝?/SPAN>"+splitByte+"字節(jié)!");

                         for(int i=1;i<=loopCount;i++) {

                                if(i==loopCount)

                                       array.add(splitStr.substring((i-1)*splitByte,splitStr.length()));

                                else

                                       array.add(splitStr.substring((i-1)*splitByte,(i*splitByte)));

                         }

                         Iterator it = array.iterator();

                         while(it.hasNext()){

                                System.out.println(it.next());

                         }

                  } catch (RuntimeException e) {

                         e.printStackTrace();

                         throw new Exception("截取漢字發(fā)生異常");

                  }

                  return array;

           }

           /**

         * 測(cè)試池連接的方法

         * @param name 連接池的名字在TOMCAT中配置,sql 要發(fā)送的SQL語(yǔ)句

         * @return void

         * @throws Exception 數(shù)據(jù)庫(kù)連接異常,NamingException JNDI初始化異常

         */

           public static void poolConnection(String name,String sql) {

                   Connection con = null;

                   Statement stmt = null;

                   try {

                            InitialContext ctx = new InitialContext();

                            DataSource ds = (DataSource)ctx.lookup(name);

                            con = ds.getConnection();

                            stmt = con.createStatement();

                            ResultSet rs = stmt.executeQuery(sql);

                            while(rs.next()) {

                                 System.out.print(rs.getString(1));

                                 System.out.print(rs.getString(2));

                                 System.out.print(rs.getString(3));

                            }

                   } catch (NamingException e) {

                         System.out.println("JNDI初始化異常");

                         e.printStackTrace();

                   } catch (SQLException e) {

                         System.out.println("數(shù)據(jù)庫(kù)連接異常");

                         e.printStackTrace();

                   }

                   finally {

                         try {

                                stmt.close();

                                con.close();

                         } catch (SQLException e) {

                                System.out.println("關(guān)閉連接池異常");

                                e.printStackTrace();

                         }

                   }

           }

        /**

         * 輸入身份證號(hào)碼返回年齡

         * @param id 身份證號(hào)碼

         * @return 年齡

         * @throws Exception 身份證號(hào)碼不正確(長(zhǎng)度不夠)

         */

        public static int getAge(String id) throws Exception {

            int age = -1;

            int length = id.length();

            String birthday = "";

            if (length == 15) {

                birthday = id.substring(6, 8);

                birthday = "19" + birthday;

            } else if (length == 18)

                birthday = id.substring(6, 10);

            else

                throw new Exception("錯(cuò)誤的身份證號(hào)");

            int currentYear = Calendar.getInstance().get(1);

            age = currentYear - (new Integer(birthday)).intValue();

            System.out.println("您的身份證號(hào)碼是"+id+",您的年齡是"+age);

            return age;

        }

        /**

         * 獲得當(dāng)前日期字符串

         * @return 日期字符串

         */

        public static String getCurrentDate() {

            Calendar cal = Calendar.getInstance();

            String currentDate = null;

            java.util.Date d = cal.getTime();

            String currentYear = (new Integer(cal.get(1))).toString();

            String currentMonth = null;

            String currentDay = null;

            if (cal.get(2) < 9)

                currentMonth = "0" + (new Integer(cal.get(2) + 1)).toString();

            else

                currentMonth = (new Integer(cal.get(2) + 1)).toString();

            if (cal.get(5) < 10)

                currentDay = "0" + (new Integer(cal.get(5))).toString();

            else

                currentDay = (new Integer(cal.get(5))).toString();

            currentDate = currentYear + currentMonth + currentDay;

            System.out.println(currentDate);

            return currentDate;

        }

       

           public static void main(String[] args) {

                  try {

                         getCurrentDate();

                         getAge("220103198601211017");

                         //因?yàn)槌剡B方法需要數(shù)據(jù)源,所以在著就不測(cè)試了

                         splitString("2005年國(guó)民經(jīng)濟(jì)和社會(huì)發(fā)展統(tǒng)計(jì)公報(bào)》",3);

                  } catch (Exception e) {

                         e.printStackTrace();

                         System.out.println("測(cè)試發(fā)生異常");

                  }

           }

    }

     

    posted on 2006-03-02 11:14 我心依舊 閱讀(707) 評(píng)論(1)  編輯  收藏

    FeedBack:
    # re: 工作空閑時(shí)間,隨便寫(xiě)了幾個(gè)方法,寫(xiě)的很爛希望不要給我丟雞蛋.
    2006-11-06 16:55 | breezedancer
    你的文章被轉(zhuǎn)至我的論壇,若有不便,請(qǐng)通知論壇管理員http://http://www.51forum.uni.cc  回復(fù)  更多評(píng)論
      

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 美女视频黄a视频全免费网站色 | 成人影片麻豆国产影片免费观看 | 精精国产www视频在线观看免费| 成人毛片免费在线观看| 亚洲w码欧洲s码免费| 91成人免费观看网站| 亚洲卡一卡2卡三卡4麻豆| 99国产精品永久免费视频| 亚洲小说图片视频| 黄页网站免费在线观看| 日韩亚洲不卡在线视频中文字幕在线观看 | 国产精品亚洲A∨天堂不卡| 免费观看一区二区三区| 久久久亚洲精品国产| 亚洲免费视频网址| 精品亚洲456在线播放| 国产免费av一区二区三区| eeuss影院www天堂免费| 亚洲AV无码码潮喷在线观看| 99在线免费观看视频| 亚洲人成小说网站色| 亚洲AV无码专区日韩| a级毛片在线免费| 亚洲国产成人久久77| 国产一级淫片视频免费看| 久久国产精品免费一区二区三区 | 亚洲性久久久影院| 四虎国产成人永久精品免费| 亚洲精品国产精品国自产网站| 国产精品久久免费视频| 国产又黄又爽又大的免费视频| 亚洲字幕在线观看| 免费大黄网站在线观看| 午夜精品一区二区三区免费视频| 亚洲一区精品视频在线| 亚洲一区二区三区在线播放| 国产在线观看麻豆91精品免费| 国产成人亚洲精品电影| 亚洲人成电影福利在线播放| 日本不卡视频免费| 久久午夜夜伦鲁鲁片免费无码 |