瀏覽代碼

关闭 preparedStatement 和 resultSet

sunyj 8 年之前
父節點
當前提交
3cef225f86
共有 1 個文件被更改,包括 40 次插入24 次删除
  1. 40 24
      src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

+ 40 - 24
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -757,33 +757,49 @@ public class PrintServiceImpl implements PrintService {
 	 * @throws SQLException
 	 */
 	private int getCount(Connection connection, String sql) throws SQLException {
-		// 如果直接在 sql 外用 count(1) 统计数目,当关联表多时,可能会出现错误
-		// ORA-01792: 表或视图中的最大列数为 1000
-		// 报错主要发生在 select * 的情况下,但是不能这样判断,因为可能存在 select tt.*, pi_id from
-		// purchase t left join 这样的情况,很难区分
-		// 因此 1. 对于普通 sql ,将 select 后的字段改为 count(1)
-		// 2. 而最外层含有 group by 的 sql ,直接改为 count(1) 可能得到不止一行,结果也并非实际行数。再加上 group
-		// by 的结果列数一般很小,所以可以在外面使用 count(1) ,一般不会超出 1000 行
-		String lowerSql = sql.toLowerCase();
-		if (!lowerSql.matches("[\\s\\S]+?group[\\s]+?by[\\s]+?[^)]+?")) {
-			String regex = "([\\s\\S]+?from)[\\s]+?[^,]+?";
-			Pattern pattern = Pattern.compile(regex);
-			Matcher matcher = pattern.matcher(lowerSql);
-			if (matcher.find()) {
-				int start = matcher.start(1);
-				int end = matcher.end(1);
-				sql = sql.substring(0, start) + "select count(1) from" + sql.substring(end);
+		PreparedStatement preparedStatement = null;
+		ResultSet resultSet = null;
+		try {
+			// 如果直接在 sql 外用 count(1) 统计数目,当关联表多时,可能会出现错误
+			// ORA-01792: 表或视图中的最大列数为 1000
+			// 报错主要发生在 select * 的情况下,但是不能这样判断,因为可能存在 select tt.*, pi_id from
+			// purchase t left join 这样的情况,很难区分
+			// 因此 1. 对于普通 sql ,将 select 后的字段改为 count(1)
+			// 2. 而最外层含有 group by 的 sql ,直接改为 count(1) 可能得到不止一行,结果也并非实际行数。再加上
+			// group
+			// by 的结果列数一般很小,所以可以在外面使用 count(1) ,一般不会超出 1000 行
+			String lowerSql = sql.toLowerCase();
+			if (!lowerSql.matches("[\\s\\S]+?group[\\s]+?by[\\s]+?[^)]+?")) {
+				String regex = "([\\s\\S]+?from)[\\s]+?[^,]+?";
+				Pattern pattern = Pattern.compile(regex);
+				Matcher matcher = pattern.matcher(lowerSql);
+				if (matcher.find()) {
+					int start = matcher.start(1);
+					int end = matcher.end(1);
+					sql = sql.substring(0, start) + "select count(1) from" + sql.substring(end);
+				} else {
+					throw new IllegalStateException("sql 解析错误:未发现第一个 from");
+				}
 			} else {
-				throw new IllegalStateException("sql 解析错误:未发现第一个 from");
+				sql = "select count(1) from (" + sql + ")";
+			}
+			preparedStatement = connection.prepareStatement(sql);
+			resultSet = preparedStatement.executeQuery();
+			resultSet.next();
+			int count = resultSet.getInt(1);
+			return count;
+		} finally {
+			if (resultSet != null) {
+				try {
+					resultSet.close();
+				} catch (SQLException e) {
+					logger.error("", e);
+				}
+			}
+			if (preparedStatement != null) {
+				preparedStatement.close();
 			}
-		} else {
-			sql = "select count(1) from (" + sql + ")";
 		}
-		PreparedStatement preparedStatement = connection.prepareStatement(sql);
-		ResultSet resultSet = preparedStatement.executeQuery();
-		resultSet.next();
-		int count = resultSet.getInt(1);
-		return count;
 	}
 
 }