commit ceb03cfb6be5477fbcbfe22be2fb38cccbf6cf08 Author: insanity Date: Tue Apr 11 12:22:18 2017 +0900 sql crawler diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..18d4871 --- /dev/null +++ b/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + + com.loafle + maven_parent_jar + 1.0.0-RELEASE + + + com.loafle.overflow + sql + jar + 1.0.0-SNAPSHOT + com.loafle.overflow.sql + + + + + mysql + mysql-connector-java + 5.1.6 + + + + + org.postgresql + postgresql + 42.0.0 + + + + + com.microsoft.sqlserver + mssql-jdbc + 6.1.0.jre8 + + + + com.oracle + ojdbc7 + 12.1.0.1.0 + system + ${basedir}/src/lib/ojdbc7.jar + + + + com.loafle + crawler_java + 1.0.0-SNAPSHOT + + + + + \ No newline at end of file diff --git a/sql_crawler.iml b/sql_crawler.iml new file mode 100644 index 0000000..e7d444a --- /dev/null +++ b/sql_crawler.iml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/lib/ojdbc7.jar b/src/lib/ojdbc7.jar new file mode 100644 index 0000000..fd38a6c Binary files /dev/null and b/src/lib/ojdbc7.jar differ diff --git a/src/main/java/com/loafle/overflow/sql/DBMetaInfo.java b/src/main/java/com/loafle/overflow/sql/DBMetaInfo.java new file mode 100644 index 0000000..682bfc2 --- /dev/null +++ b/src/main/java/com/loafle/overflow/sql/DBMetaInfo.java @@ -0,0 +1,10 @@ +package com.loafle.overflow.sql; + +/** + * Created by insanity on 17. 4. 11. + */ +public class DBMetaInfo { + private String name; + public String getName() {return name;} + public void setName(String name) {this.name = name;} +} diff --git a/src/main/java/com/loafle/overflow/sql/SQLCrawler.java b/src/main/java/com/loafle/overflow/sql/SQLCrawler.java new file mode 100644 index 0000000..0a22696 --- /dev/null +++ b/src/main/java/com/loafle/overflow/sql/SQLCrawler.java @@ -0,0 +1,122 @@ +package com.loafle.overflow.sql; + +import com.loafle.overflow.crawler.Crawler; + +import java.sql.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by insanity on 17. 4. 11. + */ +public class SQLCrawler extends Crawler { + + public SQLCrawler() {} + + public Object get(String id) { + TargetInfo info = getTargetInfo(id); + return getMetrics(info); + } + + private TargetInfo getTargetInfo(String id) { + TargetInfo info = new TargetInfo(); + +// //MySQL + info.setUrl("jdbc:mysql://192.168.1.215:3306"); + info.setId("root"); + info.setPw("qwe123"); + info.setSSL(false); + info.setQuery("show session status"); +// //MSSQL +// info.setUrl("jdbc:sqlserver://192.168.1.106:1433"); +// info.setId("sa"); +// info.setPw("qwe123"); +// info.setSSL(false); +// info.setQuery("select * from master.dbo.sysprocesses"); +// //PGSQL +// info.setUrl("jdbc:postgresql://192.168.1.107:5432/postgres"); +// info.setId("postgres"); +// info.setPw("!@#$qwer1234"); +// info.setSSL(false); +// info.setQuery("select * from pg_stat_activity"); +// //Oracle +// info.setUrl("jdbc:oracle:thin:@192.168.1.30:1521/oracle.loafle.com"); +// info.setId("sys as sysdba"); +// info.setPw("qwer5795QWER"); +// info.setSSL(false); +// info.setQuery("select * from v$sysstat"); + + return info; + } + + private List> getMetrics(TargetInfo info) { + + Connection conn = null; + Statement stmt = null; + ResultSet rs = null; + try { + + conn = DriverManager.getConnection(info.getUrl(),info.getId(), info.getPw()); + stmt = conn.createStatement(); + + rs = stmt.executeQuery(info.getQuery()); + + ResultSetMetaData md = rs.getMetaData(); + int cnt = md.getColumnCount(); + + List metaList = new ArrayList(); + + for (int i =0 ; i < cnt ; ++i) { + DBMetaInfo o = new DBMetaInfo(); + o.setName(md.getColumnName(i+1)); + metaList.add(o); + } + + List> datas = new ArrayList>(); + while(rs.next()){ + Map row = new HashMap(); + for (DBMetaInfo o : metaList) { + row.put(o.getName(), rs.getString(o.getName())); + } + datas.add(row); + } + + return datas; + + } catch (Exception e) { + + e.printStackTrace(); + + } finally { + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + } + + return null; + } + +} diff --git a/src/main/java/com/loafle/overflow/sql/TargetInfo.java b/src/main/java/com/loafle/overflow/sql/TargetInfo.java new file mode 100644 index 0000000..cb067bc --- /dev/null +++ b/src/main/java/com/loafle/overflow/sql/TargetInfo.java @@ -0,0 +1,53 @@ +package com.loafle.overflow.sql; + +/** + * Created by insanity on 17. 4. 11. + */ +public class TargetInfo { + + private String url; + private String id; + private String pw; + private Boolean isSSL; + private String query; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getPw() { + return pw; + } + + public void setPw(String pw) { + this.pw = pw; + } + + public Boolean getSSL() { + return isSSL; + } + + public void setSSL(Boolean SSL) { + isSSL = SSL; + } + + public String getQuery() { + return query; + } + + public void setQuery(String query) { + this.query = query; + } +} diff --git a/src/main/resources/_ b/src/main/resources/_ new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/com/loafle/overflow/AppTest.java b/src/test/java/com/loafle/overflow/AppTest.java new file mode 100644 index 0000000..d5e0d13 --- /dev/null +++ b/src/test/java/com/loafle/overflow/AppTest.java @@ -0,0 +1,22 @@ + +package com.loafle.overflow; + +import com.loafle.overflow.sql.SQLCrawler; +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +public class AppTest { + @Test + public void testSQL() { + SQLCrawler sc = new SQLCrawler(); + List> result = (List>)sc.get("1111"); + + for(Map m : result) { + for (Map.Entry entry : m.entrySet()) { + System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); + } + } + } +} \ No newline at end of file diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml new file mode 100644 index 0000000..f3498c8 --- /dev/null +++ b/src/test/resources/logback.xml @@ -0,0 +1,17 @@ + + + sql + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n + + + + + + + + + \ No newline at end of file