浏览 3646 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-12-19
最后修改:2010-12-19
import java.util.ArrayList; public class Test { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); Test.parse("select * from sdf where a = :中文 and a >= to_date(:date,'yyyy-mm-dd hh24:mi:ss')", al); System.out.println("ssssss"); } public static void parse(String sqlString,ArrayList<String> params){ int stringLength = sqlString.length(); boolean inQuote = false; for ( int indx = 0; indx < stringLength; indx++ ) { char c = sqlString.charAt( indx ); if ( inQuote ) {\\如果标记为true,那么一直循环到结束的' if ( '\'' == c ) { inQuote = false; } } else if ( '\'' == c ) {\\这里第一次遇到'的时候打标记 inQuote = true; } else if ( c == ':' ) { // named parameter int right = firstIndexOfChar( sqlString, " \n\r\f\t,()=<>&|+-=/*'^![]#~\\", indx + 1 ); int chopLocation = right < 0 ? sqlString.length() : right; String param = sqlString.substring( indx + 1, chopLocation ); if ( param == null || param.length() == 0) { throw new RuntimeException("Space is not allowed after parameter prefix ':' '" + sqlString + "'"); } params.add(param); indx = chopLocation - 1; } } } public static int firstIndexOfChar(String sqlString, String string, int startindex) { int matchAt = -1; for ( int i = 0; i < string.length(); i++ ) { int curMatch = sqlString.indexOf( string.charAt( i ), startindex ); if ( curMatch >= 0 ) { if ( matchAt == -1 ) { // first time we find match! matchAt = curMatch; } else { matchAt = Math.min( matchAt, curMatch ); } } } return matchAt; } } 以上代码比较简单,不解释了. 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |