2010年10月15日

           1         final CheckBoxMultipleChoice<String> resultlistChoice = new CheckBoxMultipleChoice<String>("resultlist", new PropertyModel<List<String>>(this, "valueList"), new PropertyModel<List<String>>(this, "list"));
           2 
           3         resultlistChoice.add(new AjaxFormComponentUpdatingBehavior("onclick") {
           4 
           5             @Override
           6             protected void onUpdate(AjaxRequestTarget target) {
           7                 // TODO Auto-generated method stub
           8             }
           9         });
          10         
          11         resultlistChoice.add(new AjaxFormChoiceComponentUpdatingBehavior() {
          12             
          13             @Override
          14             protected void onUpdate(AjaxRequestTarget target) {
          15                 // TODO Auto-generated method stub
          16                 selectedList.clear();
          17                 for (String item : resultlistChoice.getModelObject()) {
          18                     selectedList.add(item);
          19                     System.out.println(item);
          20                 }
          21                 target.add(selectedChoice);
          22             }
          23         });

          posted @ 2012-06-18 00:07 myfavorite 閱讀(335) | 評(píng)論 (0)編輯 收藏

          在進(jìn)行模糊查詢時(shí),經(jīng)常用到使用漢字拼音或者首字母進(jìn)行匹配查詢。以下是獲取漢字拼音或者首字母的java代碼實(shí)現(xiàn)
          package com;

          import net.sourceforge.pinyin4j.PinyinHelper;
          import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
          import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
          import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
          import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
          import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

          public class GetPinyin {

              /**
               * 得到 全拼
               * 
               * 
          @param src
               * 
          @return
               
          */
              public static String getPingYin(String src) {
                  char[] t1 = null;
                  t1 = src.toCharArray();
                  String[] t2 = new String[t1.length];
                  HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
                  t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
                  t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
                  t3.setVCharType(HanyuPinyinVCharType.WITH_V);
                  String t4 = "";
                  int t0 = t1.length;
                  try {
                      for (int i = 0; i < t0; i++) {
                          // 判斷是否為漢字字符
                          if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                              t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                              t4 += t2[0];
                          } else {
                              t4 += java.lang.Character.toString(t1[i]);
                          }
                      }
                      return t4;
                  } catch (BadHanyuPinyinOutputFormatCombination e1) {
                      e1.printStackTrace();
                  }
                  return t4;
              }

              /**
               * 得到中文首字母
               * 
               * 
          @param str
               * 
          @return
               
          */
              public static String getPinYinHeadChar(String str) {

                  String convert = "";
                  for (int j = 0; j < str.length(); j++) {
                      char word = str.charAt(j);
                      String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
                      if (pinyinArray != null) {
                          convert += pinyinArray[0].charAt(0);
                      } else {
                          convert += word;
                      }
                  }
                  return convert;
              }

              /**
               * 將字符串轉(zhuǎn)移為ASCII碼
               * 
               * 
          @param cnStr
               * 
          @return
               
          */
              public static String getCnASCII(String cnStr) {
                  StringBuffer strBuf = new StringBuffer();
                  byte[] bGBK = cnStr.getBytes();
                  for (int i = 0; i < bGBK.length; i++) {
                      // System.out.println(Integer.toHexString(bGBK[i]&0xff));
                      strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
                  }
                  return strBuf.toString();
              }

              public static void main(String[] args) {

                  String cnStr = "戩浜";
                  System.out.println(getPingYin(cnStr));
                  System.out.println(getPinYinHeadChar(cnStr));
              }

          }
          pinyin4j-2.5.0.jar

          posted @ 2012-04-18 21:58 myfavorite 閱讀(8487) | 評(píng)論 (8)編輯 收藏

          jdbc訪問數(shù)據(jù)庫

          1 將數(shù)據(jù)庫的JDBC驅(qū)動(dòng)加載到classpath中,在基于JAVAEE的WEB應(yīng)用實(shí)際開發(fā)過程中,通常要把目標(biāo)數(shù)據(jù)庫產(chǎn)品的JDBC驅(qū)動(dòng)復(fù)制到WEB-INF/lib下.
          2 加載JDBC驅(qū)動(dòng),并將其注冊(cè)到DriverManager中;
          3 建立數(shù)據(jù)庫連接,取得Connection對(duì)象.例如:
          MySQL:   
              String Driver="com.mysql.jdbc.Driver";    //驅(qū)動(dòng)程序
              String URL="jdbc:mysql://localhost:3306/db_name";    //連接的URL,db_name為數(shù)據(jù)庫名   
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).new Instance();
              Connection con=DriverManager.getConnection(URL,Username,Password);

          Microsoft SQL Server驅(qū)動(dòng)(msbase.jar、mssqlserver.jar、msutil.jar):
              String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";    //連接SQL數(shù)據(jù)庫的方法
              String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name";    //db_name為數(shù)據(jù)庫名
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).new Instance();    //加載數(shù)據(jù)驅(qū)動(dòng)
              Connection con=DriverManager.getConnection(URL,UserName,Password); 

          Microsoft SQL Server驅(qū)動(dòng)(jtds-1.2.jar):
              String Driver="net.sourceforge.jtds.jdbc.Driver";    //連接SQL數(shù)據(jù)庫的方法
              String URL="jdbc:jtds:sqlserver://localhost:1433/db_name;s=8.0;lastupdatecount=true";    //db_name為數(shù)據(jù)庫名
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).new Instance();    //加載數(shù)據(jù)驅(qū)動(dòng)
              Connection con=DriverManager.getConnection(URL,UserName,Password); 

          Sysbase:
              String Driver="com.sybase.jdbc.SybDriver";    //驅(qū)動(dòng)程序
              String URL="jdbc:Sysbase://localhost:5007/db_name";    //db_name為數(shù)據(jù)可名
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).newInstance();   
              Connection con=DriverManager.getConnection(URL,Username,Password);

          Sysbase:
            String url="jdbc:sybase:Tds:localhost:5007/tsdata";
            Properties sysProps=System.getProperties();
            SysProps.put("user","userid");
            SysProps.put("password","user_password");
            Connection conn=DriverManager.getConnection(url,SysProps);

          Oracle(用thin模式):
              String Driver="oracle.jdbc.driver.OracleDriver";    //連接數(shù)據(jù)庫的方法
              String URL="jdbc:oracle:thin:@loaclhost:1521:orcl";    //orcl為數(shù)據(jù)庫的SID
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).newInstance();    //加載數(shù)據(jù)庫驅(qū)動(dòng)
              Connection con=DriverManager.getConnection(URL,Username,Password);   

          PostgreSQL:
              String Driver="org.postgresql.Driver";    //連接數(shù)據(jù)庫的方法
              String URL="jdbc:postgresql://localhost/db_name";    //db_name為數(shù)據(jù)可名
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).newInstance();   
              Connection con=DriverManager.getConnection(URL,Username,Password);

          DB2:
              String Driver="com.ibm.db2.jdbc.app.DB2.Driver";    //連接具有DB2客戶端的Provider實(shí)例
              //String Driver="com.ibm.db2.jdbc.net.DB2.Driver";    //連接不具有DB2客戶端的Provider實(shí)例
              String URL="jdbc:db2://localhost:5000/db_name";    //db_name為數(shù)據(jù)可名
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).newInstance();   
              Connection con=DriverManager.getConnection(URL,Username,Password);

          Informix:
              String Driver="com.informix.jdbc.IfxDriver";   
              String URL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver";    //db_name為數(shù)據(jù)可名
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).newInstance();   
              Connection con=DriverManager.getConnection(URL,Username,Password);

          JDBC-ODBC:
              String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
              String URL="jdbc:odbc:dbsource";    //dbsource為數(shù)據(jù)源名
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).newInstance();   
              Connection con=DriverManager.getConnection(URL,Username,Password);

          4 建立Statement對(duì)象或PreparedStatement對(duì)象.例如:
            //建立Statement對(duì)象
            Statement stmt=conn.createStatement();
            //建立ProparedStatement對(duì)象
            String sql="select * from user where userName=? and password=?";
            PreparedStatement pstmt=Conn.prepareStatement(sql);
            pstmt.setString(1,"admin");
            pstmt.setString(2,"liubin");
          5 執(zhí)行SQL語句.例如:
            String sql="select * from users";
            ResultSet rs=stmt.executeQuery(sql);
            //執(zhí)行動(dòng)態(tài)SQL查詢
            ResultSet rs=pstmt.executeQuery();
            //執(zhí)行insert update delete等語句,先定義sql
            stmt.executeUpdate(sql);
          6 訪問結(jié)果記錄集ResultSet對(duì)象。例如:
            while(rs.next)
            {
            out.println("你的第一個(gè)字段內(nèi)容為:"+rs.getString());
            out.println("你的第二個(gè)字段內(nèi)容為:"+rs.getString(2));
            }
          7 依次將ResultSet、Statement、PreparedStatement、Connection對(duì)象關(guān)閉,釋放所占用的資源.例如:
            rs.close();
            stmt.clost();
            pstmt.close();
            con.close();

          posted @ 2010-11-02 20:36 myfavorite 閱讀(293) | 評(píng)論 (0)編輯 收藏

           

              // 驅(qū)動(dòng): msbase.jar 、 mssqlserver.jar 、 msutil.jar
              
          // driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
              
          // url = "jdbc:microsoft:sqlserver://192.168.0.82:1433;databasename=JFGLD";

              
          // 驅(qū)動(dòng):jtds-1.2.jar
              
          // DB.DRIVER=net.sourceforge.jtds.jdbc.Driver
              
          // DB.URL=jdbc:jtds:sqlserver://localhost:1433/數(shù)據(jù)庫名;s=8.0;lastupdatecount=true

          posted @ 2010-10-26 19:28 myfavorite 閱讀(272) | 評(píng)論 (0)編輯 收藏

          1. 關(guān)于InputStream.read()
               在從數(shù)據(jù)流里讀取數(shù)據(jù)時(shí),為圖簡單,經(jīng)常用InputStream.read()方法。這個(gè)方法是從流里每次只讀取讀取一個(gè)字節(jié),效率會(huì)非常低。     更好的方法是用InputStream.read(byte[] b)或者InputStream.read(byte[] b,int off,int len)方法,一次讀取多個(gè)字節(jié)。


          2. 關(guān)于InputStream類的available()方法
              要一次讀取多個(gè)字節(jié)時(shí),經(jīng)常用到InputStream.available()方法,這個(gè)方法可以在讀寫操作前先得知數(shù)據(jù)流里有多少個(gè)字節(jié)可以讀取。需要注意的是,如果這個(gè)方法用在從本
          地文件讀取數(shù)據(jù)時(shí),一般不會(huì)遇到問題,但如果是用于網(wǎng)絡(luò)操作,就經(jīng)常會(huì)遇到一些麻煩。比如,Socket通訊時(shí),對(duì)方明明發(fā)來了1000個(gè)字節(jié),但是自己的程序調(diào)用available()方法卻只得到900,或者100,甚至是0,感覺有點(diǎn)莫名其妙,怎么也找不到原因。其實(shí),這是因?yàn)榫W(wǎng)絡(luò)通訊往往是間斷性的,一串字節(jié)往往分幾批進(jìn)行發(fā)送。本地程序調(diào)用available()方法有時(shí)得到0,這可能是對(duì)方還沒有響應(yīng),也可能是對(duì)方已經(jīng)響應(yīng)了,但是數(shù)據(jù)還沒有送達(dá)本地。對(duì)方發(fā)送了1000個(gè)字節(jié)給你,也許分成3批到達(dá),這你就要調(diào)用3次available()方法才能將數(shù)據(jù)總數(shù)全部得到。
                如果這樣寫代碼:
            int count = in.available();
            byte[] b = new byte[count];
            in.read(b);
                在進(jìn)行網(wǎng)絡(luò)操作時(shí)往往出錯(cuò),因?yàn)槟阏{(diào)用available()方法時(shí),對(duì)發(fā)發(fā)送的數(shù)據(jù)可能還沒有到達(dá),你得到的count是0。
                   需要改成這樣:
            int count = 0;
            while (count == 0) {
             count = in.available();
            }
            byte[] b = new byte[count];
            in.read(b);
          3. 關(guān)于InputStream.read(byte[] b)和InputStream.read(byte[] b,int off,int len)這兩個(gè)方法都是用來從流里讀取多個(gè)字節(jié)的,有經(jīng)驗(yàn)的程序員就會(huì)發(fā)現(xiàn),這兩個(gè)方法經(jīng)常 讀取不到自己想要讀取的個(gè)數(shù)的字節(jié)。比如第一個(gè)方法,程序員往往希望程序能讀取到b.length個(gè)字節(jié),而實(shí)際情況是,系統(tǒng)往往讀取不了這么多。仔細(xì)閱讀Java的API說明就發(fā)現(xiàn)了,這個(gè)方法 并不保證能讀取這么多個(gè)字節(jié),它只能保證最多讀取這么多個(gè)字節(jié)(最少1個(gè))。因此,如果要讓程序讀取count個(gè)字節(jié),最好用以下代碼:
            byte[] b = new byte[count];
            int readCount = 0; // 已經(jīng)成功讀取的字節(jié)的個(gè)數(shù)
            while (readCount < count) {
             readCount += in.read(bytes, readCount, count - readCount);
            }
                用這段代碼可以保證讀取count個(gè)字節(jié),除非中途遇到IO異常或者到了數(shù)據(jù)流的結(jié)尾(EOFException)

          posted @ 2010-10-19 18:41 myfavorite 閱讀(551) | 評(píng)論 (0)編輯 收藏

          編輯完成代碼,用MyEclipse的代碼格式化后,本來不長的代碼也被自動(dòng)轉(zhuǎn)成了多行。雖然自動(dòng)換行以后在編輯器中一眼就能看到全部的代碼,但是可讀性卻大打折扣,避免出現(xiàn)這種情況的辦法是:

          1.Java代碼

          打開Eclipse的Window菜單,然后Preferences->Java->Code Style->Formatter->Edit/Show(根據(jù)不同版本可用的按鈕會(huì)不一樣) ->Line Wrapping->Maximum line width:由默認(rèn)的80改成自己想要設(shè)定的長度

          2.Html代碼

          Window->Preferences->MyEclipse->Files and Editors->Html->Html Source->Line width->加個(gè)0以后保存。

          3.xml代碼

          Window->Preferences->MyEclipse->Files and Editors->xml->xml Source->->Line width->999

          posted @ 2010-10-19 18:34 myfavorite 閱讀(853) | 評(píng)論 (0)編輯 收藏

          場景:需要啟動(dòng)多線程處理事情,而在所有事情做完之后,需要修改系統(tǒng)狀態(tài);那么如何判斷所有線程(事情)都做完了呢?這就需要判斷所有當(dāng)前運(yùn)行的線程狀態(tài)了。

          import java.util.concurrent.CountDownLatch;
          import java.util.concurrent.Executor;
          import java.util.concurrent.Executors;

          /**
           * 測(cè)試監(jiān)控類
           * 
           * 
          @author
           * 
           
          */
          public class WatchThread {

              
          /**
               * 測(cè)試函數(shù)
               * 
               * 
          @throws InterruptedException
               
          */
              
          public void testThread() throws InterruptedException {
                  
          int threadNum = 10;
                  
          // 初始化countDown
                  CountDownLatch threadSignal = new CountDownLatch(threadNum);
                  
          // 創(chuàng)建固定長度的線程池
                  Executor executor = Executors.newFixedThreadPool(threadNum);
                  
          for (int i = 0; i < threadNum; i++) { // 開threadNum個(gè)線程
                      Runnable task = new TestThread(threadSignal);
                      
          // 執(zhí)行
                      executor.execute(task);
                  }
                  threadSignal.await(); 
          // 等待所有子線程執(zhí)行完
                  
          // do work
                  System.out.println(Thread.currentThread().getName() + "+++++++結(jié)束.");
              }

              
          /**
               * 測(cè)試函數(shù)
               
          */
              
          public static void main(String[] args) throws InterruptedException {
                  WatchThread test 
          = new WatchThread();
                  test.testThread();
              }

              
          /**
               * 
               * 
          @author jill
               * 
               
          */
              
          private class TestThread implements Runnable {
                  
          private CountDownLatch threadsSignal;

                  
          public TestThread(CountDownLatch threadsSignal) {
                      
          this.threadsSignal = threadsSignal;
                  }

                  
          public void run() {
                      System.out.println(Thread.currentThread().getName() 
          + "開始");
                      
          // do shomething
                      System.out.println("開始了線程::::" + threadsSignal.getCount());
                      
          // 線程結(jié)束時(shí)計(jì)數(shù)器減1
                      threadsSignal.countDown();
                      System.out.println(Thread.currentThread().getName() 
          + "結(jié)束. 還有"
                              
          + threadsSignal.getCount() + " 個(gè)線程");
                  }
              }

          }

          posted @ 2010-10-15 20:28 myfavorite 閱讀(1441) | 評(píng)論 (0)編輯 收藏

           

          /*Java定時(shí)器代碼*/
          import java.util.Timer;
          import java.util.TimerTask;

          public class TryTimer {
              
          public static void main(String[] args) {
                  Timer timer 
          = new Timer();
                  timer.schedule(
          new TimerTask() {
                      
          public void run() {
                          System.out.println(
          "test");
                      }
                  }, 
          01000);// 0ms之后開始執(zhí)行,每隔1000ms執(zhí)行一次
              }    
          }

          posted @ 2010-10-15 18:14 myfavorite 閱讀(258) | 評(píng)論 (0)編輯 收藏

               摘要: import java.util.*; import java.text.*; import java.util.Calendar; //日期類 public class VeDate {     /** *//**     &nbs...  閱讀全文

          posted @ 2010-10-15 11:20 myfavorite 閱讀(196) | 評(píng)論 (0)編輯 收藏

          主站蜘蛛池模板: 合山市| 布拖县| 新化县| 邳州市| 海南省| 阜南县| 潞西市| 肥西县| 崇文区| 清流县| 敦煌市| 彰化县| 固始县| 错那县| 怀来县| 容城县| 泗洪县| 南宁市| 来凤县| 内黄县| 谷城县| 衡东县| 虞城县| 平乡县| 乌海市| 伽师县| 专栏| 德阳市| 平邑县| 河东区| 水城县| 闻喜县| 安仁县| 绥中县| 越西县| 塔城市| 望奎县| 卢氏县| 四会市| 墨江| 沁源县|