Cloudera CDH/CDP 및 Hadoop EcoSystem, Semantic IoT등의 개발/운영 기술을 정리합니다. gooper@gooper.com로 문의 주세요.
Mariadb에 접근하여 쿼리를 수행하고 필요시 정렬하여 List<Map<String, String>>에 담아서 리턴하는 메세드 예제
private final List<Map<String, String>> getResult (String query, String[] idxVals) throws Exception {
String db_server = ;
String db_port = ;
String db_name = ;
String db_user = ;
String db_pass = ;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mariadb://" + db_server + ":" + db_port + "/" + db_name, db_user, db_pass);
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
int cnt = 0;
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
while (rs.next()){
HashMap<String,String> row = new HashMap<String, String>(columns);
for(int i=1; i<=columns; i++){
row.put(md.getColumnName(i), rs.getObject(i).toString());
}
//log.debug("row("+(cnt++)+") ===========>"+row.toString());
list.add(row);
}
// 정렬(test)
// Collections.sort(list, new MapStringComparator("rest_type"));
//Collections.sort(list, new MapStringComparator("corner_id"));
//Collections.sort(list, new MapFloatComparator("cnt"));
return list;
} catch (Exception e) {
throw e;
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException sqle) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException sqle) {
}
if (conn != null) {
try {
conn.close();
} catch (SQLException sqle) {
}
}
}
}
*정렬을 위해서 사용되는 클래스(문자열)
class MapStringComparator implements Comparator<Map<String, String>> {
private final String key;
public MapStringComparator(String key) {
this.key = key;
}
@Override
public int compare(Map<String, String> first, Map<String, String> second) {
String firstValue =first.get(key);
String secondValue = second.get(key);
// 내림차순 정렬
return firstValue.compareTo(secondValue);
}
}*정렬을 위해서 사용되는 클래스(숫자)
class MapFloatComparator implements Comparator<Map<String, String>> {
private final String key;
public MapFloatComparator(String key) {
this.key = key;
}
@Override
public int compare(Map<String, String> first, Map<String, String> second) {
float firstValue = Float.valueOf(first.get(key));
float secondValue = Float.valueOf(second.get(key));
// 내림차순 정렬
if (firstValue > secondValue) {
return -1;
} else if (firstValue < secondValue) {
return 1;
} else /* if (firstValue == secondValue) */ {
return 0;
}
}
}
댓글 0
| 번호 | 제목 | 날짜 | 조회 수 |
|---|---|---|---|
| 13 | update 샘플 | 2018.03.12 | 4908 |
| 12 | [DBeaver 4.3.0]import/export시 "Client home is not specified for connection" 오류발생시 조치사항 | 2017.12.21 | 4647 |
| 11 | 권한회수 및 권한부여 명령 몇가지 | 2017.11.16 | 4528 |
| 10 | db를 통째로 새로운 이름의 db로 복사하는 방법/절차 | 2017.11.14 | 5057 |
| 9 | mysql에서 외부 디비를 커넥션할 경우 접속 속도가 느려질때 | 2017.06.30 | 5562 |
| 8 | mysql-server 기동시 Do you already have another mysqld server running on port 오류 발생할때 확인및 조치방법 | 2017.05.14 | 5444 |
| 7 | Mysql DB 생성 및 권한. 특정아이피, 대역에 대한 접근 허용 | 2017.05.04 | 5582 |
| 6 | Ubuntu 16.04 LTS에 MariaDB 10.1설치 및 포트변경 및 원격접속 허용 | 2017.05.01 | 4948 |
| 5 | new Gson().toJson(new ObjectId())을 사용하면 값이 다르게 나오는 경우가 있음 | 2016.12.23 | 4395 |
| » | ResultSet에서 데이타를 List<Map<String,String>>형태로 만들어서 리턴하는 소스(Collections.sort를 이용한 정렬 가능) | 2016.12.15 | 4326 |
| 3 | centos 6에서 mariadb 5.1 to 10.0 으로 upgrade | 2016.11.01 | 3305 |
| 2 | AIX 7.1에 MariaDB 10.2 소스 설치 | 2016.09.24 | 5669 |
| 1 | root계정으로 MariaDB설치후 mysql -u root -p로 db에 접근하여 바로 해줘야 하는일..(케릭터셑은 utf8) | 2015.10.02 | 3819 |