SQL查询修改通用处理工具


/**
	 * oracle数据库类型转换之 ClobToString
	 * 
	 * @param clob
	 * @return String
	 */
	public static String ClobToString(CLOB clob) {

		String reString = "";
		try {
			BufferedReader br = new BufferedReader(clob.getCharacterStream());
			
			String s = br.readLine();
			StringBuffer sb = new StringBuffer();
			while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
				// sb.append("\r\n");
				sb.append(System.getProperty("line.separator"));
				sb.append(s);
				s = br.readLine();
			}
			reString = sb.toString();
		} catch (SQLException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 得到流

		return reString;
	}


/**
	 * 得到总和的数量
	 * @param sqlNeedDeal sql 是否需要处理
	 * @param sql
	 * @param params
	 * @return
	 */
	public int getCount(boolean sqlNeedDeal,String sql, Object... params) {
		int result = 0;
		if(sqlNeedDeal){
			sql = "select count(0) from ("+sql+")";
		}
		
		try (Connection connection = ds.getConnection()) {
			try (PreparedStatement pp = connection.prepareStatement(sql)) {
				for (int i = 0; i < params.length; i++) {
					pp.setObject(i + 1, params[i]);
				}
				try (ResultSet rs = pp.executeQuery()) {
					while (rs.next()) {
						result = rs.getInt(1);
					}
				} catch (Exception e) {
					System.err.println("getCount:数据库获取失败!");
					e.printStackTrace();
				}
			} catch (SQLException e) {
				System.err.println("getCount:数据库准备失败!");
				e.printStackTrace();
			}
		} catch (SQLException e) {
			System.err.println("getCount:数据库获取失败!");
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 更新数据
	 * @param sql
	 * @param params
	 * @return
	 */
	public int executeUpdate(String sql, Object... params) throws SQLException{
		int result = -1;
		try (Connection connection = ds.getConnection()) {
			try (PreparedStatement pp = connection.prepareStatement(sql)) {
				for (int i = 0; i < params.length; i++) {
					pp.setObject(i + 1, params[i]);
				}
				result = pp.executeUpdate();
			} catch (SQLException e) {
				System.err.println("executeUpdate:数据库准备或者更新失败!");
				e.printStackTrace();
				throw(e);
			}
		} catch (SQLException e) {
			System.err.println("executeUpdate:数据库获取失败!");
			e.printStackTrace();
			throw(e);
		}
		return result;
	}
	
	//public Map<String,Object> execute
	/**
	 * 得到查询数据 ,该方法暂时只支持比较普通的查询
	 * @param sql
	 * @param columns
	 * @param params
	 * @return
	 */
	public List<LinkedHashMap<String, Object>>   executeSelect(String sql,List<String> columns, Object... params){
		List<LinkedHashMap<String, Object>> datas=new ArrayList<>();
		try (Connection connection = ds.getConnection()) {
			try (PreparedStatement pp = connection.prepareStatement(sql)) {
				for (int i = 0; i < params.length; i++) {
					pp.setObject(i + 1, params[i]);
				}
				try (ResultSet rs = pp.executeQuery()) {
					while (rs.next()) {
						LinkedHashMap<String, Object> one = new LinkedHashMap<>();
						for (String key : columns) {
							one.put(key,rs.getObject(key));
						}
						if(one.size()>0){
							datas.add(one);
						}
					}
				} catch (Exception e) {
					System.err.println("executeSelect:数据库获取失败!");
					e.printStackTrace();
				}
			} catch (SQLException e) {
				System.err.println("executeSelect:数据库准备失败!");
				e.printStackTrace();
			}
		} catch (SQLException e) {
			System.err.println("executeSelect:数据库获取失败!");
			e.printStackTrace();
		}
		return datas;
	}
	/**
	 * 得到查询数据 ,该方法暂时只支持比较普通的查询
	 * @param sql
	 * @param columns Map<String, String>   第一个表示的name 第二个是数据库的name
	 * @param params  
	 * @return
	 */
	public List<LinkedHashMap<String, Object>>   executeSelect(String sql,Map<String, String> columns, Object... params){
		List<LinkedHashMap<String, Object>> datas=new ArrayList<>();
		try (Connection connection = ds.getConnection()) {
			try (PreparedStatement pp = connection.prepareStatement(sql)) {
				for (int i = 0; i < params.length; i++) {
					pp.setObject(i + 1, params[i]);
				}
				try (ResultSet rs = pp.executeQuery()) {
					while (rs.next()) {
						LinkedHashMap<String, Object> one = new LinkedHashMap<>();
						for (Map.Entry<String,String> key : columns.entrySet()) {
							Object value = rs.getObject(key.getValue());
							one.put(key.getKey(),value==null?" ":value);
						}
						if(one.size()>0){
							datas.add(one);
						}
					}
				} catch (Exception e) {
					System.err.println("executeSelect:数据库获取失败!");
					e.printStackTrace();
				}
			} catch (SQLException e) {
				System.err.println("executeSelect:数据库准备失败!");
				e.printStackTrace();
			}
		} catch (SQLException e) {
			System.err.println("executeSelect:数据库获取失败!");
			e.printStackTrace();
		}
		return datas;
	}
	
ArrayList<Object> params = new ArrayList<Object>();
/*params添加入sql中的条件值。如:
if(time_title !=null && (time_s != null && time_e != null)){
			where += " and s." + time_title + ">=to_date(?,'yyyy-mm-dd')";
			params.add(time_s.trim());
			where += " and s." + time_title + "<(to_date(?,'yyyy-mm-dd')+1)";
			params.add(time_e.trim());
			
		}
*/
List<LinkedHashMap<String, Object>> datas=new ArrayList<>();
LinkedHashMap<String, String> columns = new LinkedHashMap<String, String>();
			
			columns.put("类型", "z_type");
			columns.put("状态", "status");
			
			datas = helper.executeSelect(sql, columns, params.toArray());
String	jsonString=JSON.toJSONString(datas);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值