1. 程式人生 > 其它 >SQL查詢修改通用處理工具

SQL查詢修改通用處理工具

技術標籤:Oracle資料庫jAVA工具資料庫java


/**
	 * 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);