Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29335708
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2010-06-01 22:21:40

1、动态拼结SQL。依据IP查询结果集

package cn.test;
public class Test {
    public static void main(String[] args) {
        String ips = "19.2.128.*;124.225.*.32;124.225.65.30";
        String[] results = ips.split(";");
        String dysqlString = "";
        StringBuffer tmp = new StringBuffer();
        int k = 0;
        for (String string : results) {
            if (k == results.length - 1) {
                if (string.contains("*")) {
                    int position = string.indexOf("*");
                    tmp.append("(left(localip,"+(position-1)+") = '" + string.substring(0, position-1) + "' or left(foreightip," + (position - 1)+") = '" + string.substring(0, position-1) +"') ");
                } else {
                    tmp.append("(localip='"+string.trim()+"' or foreigip='" + string.trim() + "') ");
                }
                break;
            }
            if (string.contains("*")) {
                int position = string.indexOf("*");
                tmp.append("(left(localip,"+(position-1)+") = '" + string.substring(0, position-1) + "' or left(foreightip," + (position - 1)+") = '" + string.substring(0, position-1) +"') or ");
            } else {
                tmp.append("(localip='"+string.trim()+"' or foreigip='" + string.trim() + "') or ");
            }
            k++;
        }
        dysqlString = tmp.toString();
        System.out.println(dysqlString);
    }
}


阅读(766) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

hkebao2010-06-04 11:51:03

http://developer.51cto.com/developer/top10Architect/