对增加一个字段比如字段姓名    拼音字段
                  江海涛       jht

解决方案 »

  1.   

    名字的每个字放一个字段。
    把数据库的中文排序排序方式设为按拼音排序。(SQL Server,DB2 ,Oracle好像默认是,其他的不知道)
    然后再建一张映射表:(link)
    -----------------------
    字母  该字母对用的字
    letter charactor
    a       啊
    b       吧
    c       擦
    ...     ...
    z       咋
    -----------------------
    就是用26个字母和以这26字母加韵母a等到的汉字作个对用关系。(u,v 都对应 哇)检索 江海涛(jht) 时 可用
    select charactor from link where letter in ('j','k')
    select charactor from link where letter in ('h','i')
    select charactor from link where letter in ('t','u')
    搜索出条件中每个字母和这个字母前一个字母对应的汉字放在数组m1[],m2[],m3[]中
    select 
       c1+c2+c3 as name 
    from 
       user
    where 
       (user.c1>=m1[0] and user.c1<m1[1]) AND
       (user.c2>=m2[0] and user.c1<m2[1]) AND
       (user.c3>=m3[0] and user.c1<m3[1]) AND条件有a和z时,检索条件只取一边。
    SQL语句应该还可以优化。
      

  2.   

    famousboy(famousboy) 说的很有道理,这样做的效果是最好的,不用构建什么词典!
      

  3.   

    找字母对应的第一个汉字比较麻烦。
    可以肯定的是,先按拼音的字母顺序,再按声调顺去,一声最小,轻声最大。
    如果读音完全相同,好像是看笔画的,第一画是的横比第一画是点的小(没有验证过)。有哪位大哥知道汉字按拼音排序的规则请赐教。但是可以把常用的a韵母的一声的常用汉字存到一列里,然后用order by 拍一下序。生僻字暂时就不考虑了。
      

  4.   

    jsp 中 有没有象 asp 中的Asc 函数Asc 函数 返回字符串首字母的 ANSI 字符代码
      

  5.   

    想用asc 类似的方法实现字名称转换成拼音首字母
    jsp 中类似的函数是什么?
      

  6.   

    没有这样的方法,自已写一个
    public class GetCh2Spell {
        public static int compare(String str1, String str2)
         {
             int result = 0;
             String m_s1 = null;
             String m_s2 = null;
             try
             {
                 m_s1 = new String(str1.getBytes(_FromEncode_), _ToEncode_);
                 m_s2 = new String(str2.getBytes(_FromEncode_), _ToEncode_);
             }
             catch(Exception e)
             {
                 return str1.compareTo(str2);
             }
             result = chineseCompareTo(m_s1, m_s2);
             return result;
         }     public static int getCharCode(String s)
         {
             if(s == null && s.equals(""))
                 return -1;
             byte b[] = s.getBytes();
             int value = 0;
             for(int i = 0; i < b.length && i <= 2; i++)
                 value = value * 100 + b[i];         return value;
         }     public static int chineseCompareTo(String s1, String s2)
         {
             int len1 = s1.length();
             int len2 = s2.length();
             int n = Math.min(len1, len2);
             for(int i = 0; i < n; i++)
             {
                 int s1_code = getCharCode(s1.charAt(i) + "");
                 int s2_code = getCharCode(s2.charAt(i) + "");
                 if(s1_code * s2_code < 0)
                     return Math.min(s1_code, s2_code);
                 if(s1_code != s2_code)
                     return s1_code - s2_code;
             }         return len1 - len2;
         }     public static String getBeginCharacter(String res)
         {
             String a = res;
             String result = "";
             for(int i = 0; i < a.length(); i++)
             {
                 String current = a.substring(i, i + 1);
                 if(compare(current, "\u554A") < 0)
                     result = result + current;
                 else
                 if(compare(current, "\u554A") >= 0 && compare(current, "\u5EA7") <= 0)
                     if(compare(current, "\u531D") >= 0)
                         result = result + "z";
                     else
                     if(compare(current, "\u538B") >= 0)
                         result = result + "y";
                     else
                     if(compare(current, "\u6614") >= 0)
                         result = result + "x";
                     else
                     if(compare(current, "\u6316") >= 0)
                         result = result + "w";
                     else
                     if(compare(current, "\u584C") >= 0)
                         result = result + "t";
                     else
                     if(compare(current, "\u6492") >= 0)
                         result = result + "s";
                     else
                     if(compare(current, "\u7136") >= 0)
                         result = result + "r";
                     else
                     if(compare(current, "\u671F") >= 0)
                         result = result + "q";
                     else
                     if(compare(current, "\u556A") >= 0)
                         result = result + "p";
                     else
                     if(compare(current, "\u54E6") >= 0)
                         result = result + "o";
                     else
                     if(compare(current, "\u62FF") >= 0)
                         result = result + "n";
                     else
                     if(compare(current, "\u5988") >= 0)
                         result = result + "m";
                     else
                     if(compare(current, "\u5783") >= 0)
                         result = result + "l";
                     else
                     if(compare(current, "\u5580") >= 0)
                         result = result + "k";
                     else
                     if(compare(current, "\u51FB") > 0)
                         result = result + "j";
                     else
                     if(compare(current, "\u54C8") >= 0)
                         result = result + "h";
                     else
                     if(compare(current, "\u5676") >= 0)
                         result = result + "g";
                     else
                     if(compare(current, "\u53D1") >= 0)
                         result = result + "f";
                     else
                     if(compare(current, "\u86FE") >= 0)
                         result = result + "e";
                     else
                     if(compare(current, "\u642D") >= 0)
                         result = result + "d";
                     else
                     if(compare(current, "\u64E6") >= 0)
                         result = result + "c";
                     else
                     if(compare(current, "\u82AD") >= 0)
                         result = result + "b";
                     else
                     if(compare(current, "\u554A") >= 0)
                         result = result + "a";
             }         return result;
         }     public static String getFirstStr(String str)
         {
             char a = str.charAt(0);
             char aa[] = {
                 a
             };
             String sss = new String(aa);
             if(Character.isDigit(aa[0]))
                 sss = "data";
             else
             if(a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z')
                 sss = "character";
             else
                 sss = getBeginCharacter(sss);
             return sss;
         }     private static String _FromEncode_ = "GBK";
         private static String _ToEncode_ = "GBK";}