2012年6月18日

           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) | 評論 (0)編輯 收藏

          2012年4月18日

          在進行模糊查詢時,經常用到使用漢字拼音或者首字母進行匹配查詢。以下是獲取漢字拼音或者首字母的java代碼實現
          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;
              }

              /**
               * 將字符串轉移為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) | 評論 (8)編輯 收藏

          2010年11月2日

          jdbc訪問數據庫

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

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

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

          Sysbase:
              String Driver="com.sybase.jdbc.SybDriver";    //驅動程序
              String URL="jdbc:Sysbase://localhost:5007/db_name";    //db_name為數據可名
              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";    //連接數據庫的方法
              String URL="jdbc:oracle:thin:@loaclhost:1521:orcl";    //orcl為數據庫的SID
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).newInstance();    //加載數據庫驅動
              Connection con=DriverManager.getConnection(URL,Username,Password);   

          PostgreSQL:
              String Driver="org.postgresql.Driver";    //連接數據庫的方法
              String URL="jdbc:postgresql://localhost/db_name";    //db_name為數據可名
              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實例
              //String Driver="com.ibm.db2.jdbc.net.DB2.Driver";    //連接不具有DB2客戶端的Provider實例
              String URL="jdbc:db2://localhost:5000/db_name";    //db_name為數據可名
              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為數據可名
              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為數據源名
              String Username="username";    //用戶名
              String Password="password";    //密碼
              Class.forName(Driver).newInstance();   
              Connection con=DriverManager.getConnection(URL,Username,Password);

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

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

          2010年10月26日

           

              // 驅動: msbase.jar 、 mssqlserver.jar 、 msutil.jar
              
          // driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
              
          // url = "jdbc:microsoft:sqlserver://192.168.0.82:1433;databasename=JFGLD";

              
          // 驅動:jtds-1.2.jar
              
          // DB.DRIVER=net.sourceforge.jtds.jdbc.Driver
              
          // DB.URL=jdbc:jtds:sqlserver://localhost:1433/數據庫名;s=8.0;lastupdatecount=true

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

          2010年10月19日

          1. 關于InputStream.read()
               在從數據流里讀取數據時,為圖簡單,經常用InputStream.read()方法。這個方法是從流里每次只讀取讀取一個字節,效率會非常低。     更好的方法是用InputStream.read(byte[] b)或者InputStream.read(byte[] b,int off,int len)方法,一次讀取多個字節。


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

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

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

          1.Java代碼

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

          2.Html代碼

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

          3.xml代碼

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

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

          2010年10月15日

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

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

          /**
           * 測試監控類
           * 
           * 
          @author
           * 
           
          */
          public class WatchThread {

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

              
          /**
               * 測試函數
               
          */
              
          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());
                      
          // 線程結束時計數器減1
                      threadsSignal.countDown();
                      System.out.println(Thread.currentThread().getName() 
          + "結束. 還有"
                              
          + threadsSignal.getCount() + " 個線程");
                  }
              }

          }

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

           

          /*Java定時器代碼*/
          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之后開始執行,每隔1000ms執行一次
              }    
          }

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

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

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

          2010年10月14日

               摘要:         一直是在NET的平臺做應用軟件的開發的,在上一個項目完成后,公司定下新的項目改為在java平臺開發,說是行業要求。這可是要了我的命。我對java可是一點都不了解,更搞不懂這java,jsp等等這些的差別,突然要搞這個,而且項目還緊,郁悶死。都有想要離職的念頭。可是總在猶豫不決中,遲遲下不了決心......&nbs...  閱讀全文

          posted @ 2010-10-14 13:10 myfavorite 閱讀(2356) | 評論 (0)編輯 收藏

          僅列出標題  
          主站蜘蛛池模板: 白山市| 壶关县| 榕江县| 仪陇县| 白山市| 盐池县| 嵩明县| 兰西县| 榆社县| 商水县| 林西县| 霞浦县| 武冈市| 大方县| 上林县| 南开区| 台州市| 姚安县| 海林市| 新野县| 佛学| 安远县| 来宾市| 曲麻莱县| 肥乡县| 阿拉善右旗| 册亨县| 鄂温| 潍坊市| 西畴县| 根河市| 贵港市| 南汇区| 监利县| 稷山县| 永顺县| 武冈市| 岳普湖县| 建阳市| 滦南县| 屏东市|