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 阅读(317) | 评论 (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 阅读(8441) | 评论 (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 阅读(277) | 评论 (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 阅读(248) | 评论 (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 阅读(527) | 评论 (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 阅读(839) | 评论 (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 阅读(1423) | 评论 (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 阅读(240) | 评论 (0)编辑 收藏

     摘要: import java.util.*; import java.text.*; import java.util.Calendar; //日期类 public class VeDate {     /** *//**     &nbs...  阅读全文

posted @ 2010-10-15 11:20 myfavorite 阅读(173) | 评论 (0)编辑 收藏

2010年10月14日

     摘要:         一直是在NET的平台做应用软件的开发的,在上一个项目完成后,公司定下新的项目改为在java平台开发,说是行业要求。这可是要了我的命。我对java可是一点都不了解,更搞不懂这java,jsp等等这些的差别,突然要搞这个,而且项目还紧,郁闷死。都有想要离职的念头。可是总在犹豫不决中,迟迟下不了决心......&nbs...  阅读全文

posted @ 2010-10-14 13:10 myfavorite 阅读(2339) | 评论 (0)编辑 收藏

仅列出标题