참조: http://blog.naver.com/PostView.nhn?blogId=jspark226&logNo=80027833188&redirect=Dlog&widgetTypeCall=true
참고) Flex 에서 자바 스크립트를 연동할수 있는 방법이기도합니다.
FLEX Remote Object 를 이용하여 DB 연결하기
1 . C:\fds2\jrun4\servers\default\flex\WEB-INF\flex 경로에 있는
* services-config.xml
* remoting-config.xml
요 두개의 파일을 손봐야 한다.(경로는 flex data service 를 default 설치시..)
2. 먼저 services-config.xml 파일의 channel 부분을 아래와 같이설정..
<channel-definition id="
my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint uri="
http://localhost:8700/flex/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>false</polling-enabled>
</properties>
</channel-definition>
3. remoting-config.xml 파일에서는
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService"
messageTypes="flex.messaging.messages.RemotingMessage">
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="nhnRO">
<properties>
<source>nhn.NHNDataBean</source>
</properties>
</destination>
</service>
굵게 써진 부분처럼 destination 을 셋팅한다.
id : remoteObject 객체의 참조하고픈 이름이다.
source : 패키징화 되어있는 java bean class 파일이다.
4. java bean 을 살펴보자...
경로 : C:\fds2\jrun4\servers\default\flex\WEB-INF\classes\nhn
밑에 NHNDataBean.class 가 위치하면 된다.
DB 연결부분은...
private Connection getConnection() {
Connection con = null;
String DB_URL = "jdbc:odbc:stat";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(DB_URL);
} catch( ClassNotFoundException e ) {
System.err.println( "드라이버를 찾을수 없습니다." + e.getMessage() );
} catch (Exception ex ) {
System.err.println( "DB연결에러 \n" + ex.getMessage() );
}
return con;
}
드라이버는 테스트 용으로 MS-ACCESS 용이다.
(FLEX 는 자체적으로 ODBC 용 드라이버는 가지고 있는 모냥이다..ㅋㅋ)
DATA 가져오는 부분은.....
public List getList(String sql) {
System.out.println("조회쿼리1 : " + sql);
ArrayList list = new ArrayList();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
java.text.DecimalFormat df = new java.text.DecimalFormat("###,##0.000000");
System.gc();
Runtime monitor = Runtime.getRuntime();
long MAX = 10000000;
long time1 = System.currentTimeMillis();
long time2 = 0;
try{
con = getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
NHNDataVo vo = new NHNDataVo();
vo.YYYYMMDD = rs.getString(1);
vo.GAME_SERVICE_ID3 = rs.getString(2);
vo.CCNT = rs.getInt(3);
vo.UV = rs.getInt(4);
list.add(vo);
}
rs.close();
stmt.close();
con.close();
time2 = System.currentTimeMillis();
System.out.println("instance total elased time=" + (time2-time1));
System.out.println("ave instance elased time=" + df.format((double)(time2-time1)/MAX));
System.out.println("total mem = " + monitor.totalMemory() );
System.out.println("free mem = " + monitor.freeMemory() + "\n");
System.out.println("조회건수 : " + list.size());
} catch(SQLException se) {
System.out.println("SQL Exception : " + se);
} catch(Exception e) {
System.out.println("Exception : " + e);
} finally {
try { if( stmt != null ) stmt.close(); } catch (Exception e) { }
try { if( con != null ) con.close(); } catch (Exception e) { }
}
return list;
}
정도로 구현하면 되겠다...
TEST 용 날림코딩이니 그냥 이해해 주었음 한다.
5. FLEX 소스 부분을 살펴보자......
(태그부분)
<!-- JAVA REMOTE OBJECT 호출 -->
<mx:RemoteObject id="ro" destination="nhnRO" showBusyCursor="true" >
<mx:method name="getList" result="resultHandler(event)" />
</mx:RemoteObject>
위에 dataservice 설정에서 지정해주었던 destination 을 셋팅한다.
(스크립트 부분)
/******************************************************
초기화
*******************************************************/
private function initApp():void {
//getData('1week', combo1.selectedItem.data , combo2.selectedItem.data , combo3.selectedItem.data);
var token:AsyncToken = ro.getList(sql); // 물론 sql 은 만들어줘야죵? 머든간에.
slicedResults = new ArrayCollection();
slicedResults1 = new ArrayCollection();
slicedResults2 = new ArrayCollection();
}
******************************************************
RemoteObject 호출하여 DB 의 데이터 원본을 가져온다.
*******************************************************/
private function resultHandler(event:ResultEvent):void {
if(ArrayCollection(event.result).length == 0){
mx.controls.Alert.show("DATA 가 없습니다.");
}
// DB 에서 가져온 result 를 담는다.
allDataAC = ArrayCollection(event.result);
var retAC:ArrayCollection = new ArrayCollection();
// 초기 데이터를 구성한다.
makeMonthArr();
try{
for( var i:uint = 0 ; i < allDataAC.length ; i++){
retAC.addItem({Month:allDataAC[i].YYYYMMDD, svc:allDataAC[i].GAME_SERVICE_ID3, cu:allDataAC[i].CCNT, uv:allDataAC[i].UV});
}
}catch(err:Error){
trace("Error");
}
// 선택된 Combo에 따른 DATA 저장
if(selectedComboBox.id == "combo1"){
slicedResults = retAC;
}else if(selectedComboBox.id == "combo2"){
slicedResults1 = retAC;
}else if(selectedComboBox.id == "combo3"){
slicedResults2 = retAC;
}
}