码迷,mamicode.com
首页 > 数据库 > 详细

Caused by: java.sql.SQLException: ORA-01795: 列表中的最大表达式数为 1000解决方案

时间:2015-06-09 11:59:15      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:caused by java.sql.s

这个原因在于Oracle语法限制,in内数量必须小于1000,故采取满1000分割用OR连接,下面是解决方案:

	/**
	  * Example: List sqhlist=[''aa''"bb""cc""dd""ee""ff""gg"'] ;
	  * Test.getSqlStrByList(sqhList,3,"SHENQINGH")= "SHENQING IN
	  * ('aa','bb','cc') OR SHENQINGH IN ('dd','ee','ff') OR SHENQINGH IN ('g
	  *
	  * 把超过1000的in条件集合拆分成数量splitNum的多组sql的in 集合。
	  *
	  * @param sqhList
	  *            in条件的List
	  * @param splitNum
	  *            拆分的间隔数目,例如: 1000
	  * @param columnName
	  *            SQL中引用的字段名例如: Z.SHENQINGH
	  * @return
	  **/
	private static String getSqlStrByList(List sqhList, int splitNum,
			String columnName) {
		if (splitNum > 1000) // 因为数据库的列表sql限制,不能超过1000.
			return null;
		StringBuffer ids = new StringBuffer("");
		if (sqhList != null) {
			ids.append(" ").append(columnName).append(" IN ( ");
			for (int i = 0; i < sqhList.size(); i++) {
				ids.append("'").append(sqhList.get(i) + "'");
				if ((i + 1) % splitNum == 0 && (i + 1) < sqhList.size()) {
					ids.deleteCharAt(ids.length() - 1);
					ids.append(" ) OR ").append(columnName).append(" IN (");
				}
			}
			ids.deleteCharAt(ids.length() - 1);
			ids.append(" )");
		}
		return ids.toString();
	}
	
	public static void main(String[] args) {
		List list = new ArrayList();
		int id = 2;
		for(int i=0;i<1020;i++){
			list.add("'"+id+"'");
		}
		String ss = getSqlStrByList(list, 1000,"test");
		System.out.println(ss);
	}


Caused by: java.sql.SQLException: ORA-01795: 列表中的最大表达式数为 1000解决方案

标签:caused by java.sql.s

原文地址:http://blog.csdn.net/zeb_perfect/article/details/46422877

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!