본문 바로가기

ComputerScience/Graphics

데이터 시각화 시작. Processing #1.

데이터 시각화의 대중화 된
프레임워크 Processing을 사용한
간단한 예제.

Processing + JDBC의 응용.


위의 사진은 기본 oracle hr 계정
emp 테이블의 salary를 salary액수의 비례해서
원의 크기로 표현한 예이다.

소스를 살펴보면,

processing.java
package common2;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import static common2.JdbcTemplate.*;


import processing.core.*;

public class processing extends PApplet{
private Connection conn;
private ArrayList<Integer> salary;

public processing() {
salary = new ArrayList<Integer>();
conn = getConnection();

String query = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
try {
query = "SELECT salary FROM employees";
pstmt = conn.prepareStatement(query);
//pstmt.setString(1, emp.getEmpLoginId());
//pstmt.setString(2, emp.getEmpLoginPasswd());
rset = pstmt.executeQuery();
//System.out.println(rset.);
while(rset.next()) {
salary.add(rset.getInt(1));
//salary.add(rset.getInt(2));

}
/*
for(int i=0;i<salary.size();i++) {
System.out.println(salary.get(i));
}
*/


}
catch (SQLException e){
e.printStackTrace();
}
finally {
close(rset);
close(pstmt);
}
}

public void setup() {
size(480, 120);
//smooth();
}

public void draw() {
int j=1;
//System.out.println(salary.size());
for(int i=0;i<salary.size();i++) {
j++;
//System.out.println(salary.get(i));
ellipse(j*10,50,salary.get(i)/100,salary.get(i)/100);
}



/*
if (mousePressed) {
fill(0);
} else {
fill(255);
}
ellipse(mouseX, mouseY, 80, 80);
*/

}


}





JdbcTemplate.java

package common2;


import java.sql.*;

import javax.naming.*;
import javax.sql.*;

/*
* DB 연결, 종료
* 명령 연결, 종료
*
*
*/



public class JdbcTemplate {

public JdbcTemplate() {
}


public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("드라이버 로딩 성공...");
String url="jdbc:oracle:thin:@127.0.0.1:1521:xe";

String user="hr";
String pwd="hr";

conn=DriverManager.getConnection(url,user,pwd);
System.out.println("DB 연결 성공!");

conn.setAutoCommit(false);

} catch (Exception e) {
System.out.println("[JdbcTemplate.getConnection] : " + e.getMessage());
e.printStackTrace();
}
return conn;
}






public static boolean isConnected(Connection conn) {
boolean validConnection = true;
try {
if (conn == null || conn.isClosed())
validConnection = false;
} catch (SQLException e) {
validConnection = false;
e.printStackTrace();
}
return validConnection;
}

public static void close(Connection conn) {

if (isConnected(conn)) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


public static void close(Statement stmt) {

try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}


public static void close(ResultSet rset) {

try {
if (rset != null)
rset.close();
} catch (SQLException e) {
e.printStackTrace();
}
}


public static void commit(Connection conn) {

try {
if (isConnected(conn)) {
conn.commit();
System.out.println("[JdbcTemplate.commit] : DB Successfully Committed!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}


public static void rollback(Connection conn) {

try {
if (isConnected(conn)) {
conn.rollback();
System.out.println("[JdbcTemplate.rollback] : DB Successfully Rollbacked!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}

}