Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


JDBC DataConversion(Using Metadata)
 
´Ù¸¥ boxÀÇ µ¥ÀÌÅͺ£À̽ºÀÇ Å×ÀÌºí³»¿ªÀ» Metadata·Î¼­ ÀÌ°üÇÒ ¼ö ÀÖ´Â ¹æ¹ý¿¡ ´ëÇÏ¿© ¼³¸íÇÑ´Ù. ( 2003/04/24 ) 438
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 

ÀÚ. À̹ø±ÛÀ» ¿Ö ¾²°Ô µÇ¾ú´ÂÁöÀÇ ÀÌÀ¯¸¦ ¶Ç ¼³¸íÇÑ´Ù. Ç×»ó ¿©±â¿¡ ¿Ã¶ó¿À´Â ¾ÆƼŬÀÇ °æ¿ì´Â
³î»õ°¡ ½ÇÁ¦ ÇÁ·ÎÁ§Æ®¿¡¼­ ÇÊ¿ä¿¡ ÀÇÇÏ¿© ¸¸µé¾îÁö°Ô µÈ ÄÚµåÀÇ µ¥ÀÌÅÍÈ­°¡ ÁÖ ¸ñÀûÀ̱â
¶§¹®ÀÌ´Ù.

¾ó¸¶Àü¿¡ EJB Container ManageÀÇ xml configuration¹æ¹ýÀ» ÀÌ¿ëÇÏ¿© µ¥ÀÌÅ͸¦ º¯È¯ÇÏ´Â ÇÁ·Î±×·¥À»
ÀÛ¼ºÇÑ ÀûÀÌ ÀÖ¾ú´Ù. 
±× ÇÁ·Î±×·¥ÀÇ ¿äÁö´Â ÇÁ·Î±×·¥°ú EJB CMP Çʵ尣ÀÇ ¸ÅÇÎÀ» ÅëÇÏ¿© µ¥ÀÌÅͺ£À̽º Æ®·£Àè¼ÇÀ»
ó¸®ÇÏ´Â °Íó·³ source table°ú target table°£ÀÇ µ¥ÀÌÅ͸¦ ´Ù¸¥ ÇüÅÂÀÇ Ä÷³À¸·Î º¯°æÇÒ ¼ö ÀÖµµ·Ï
ÇÏ´Â °Í¾ú´Ù. 

Áö±Ý ÀÛ¼ºÇÏ¿© ¾ÆƼŬ·Î ¾²°Ô µÈ ÇÁ·Î±×·¥Àº ¿ì¼± °°Àº ±¸Á¶(Å×À̺í¸í ¹× Ä÷³¸í)¸¦ °¡Áø 
Å×À̺íÀÌ ³ª¶õÈ÷ ¼­·Î ´Ù¸¥ box¿¡ Á¸ÀçÇÒ °æ¿ì µ¥ÀÌÅ͸¦ ÀÌ°üÇϵµ·Ï ÇÏ¿´´Ù. 
"¾Æ´Ï~ ³î»õ¾ß~ PL-SQLÀº È£±¸·Î Àִ°¨?" Çϸ鼭 µýÁö°É¸é ÀÌ·¸°Ô ´ë´äÇÏ°Ú´Ù.
"¾Æ´Ï~ µÎ°³°¡ °°Àº º¥´õÀÇ µ¥ÀÌÅͺ£À̽º¸¸ µÉ°Å¶ó´Â°É Àå´ãÇϼî?" ¶ó°í..

Àü¿¡ ÇÁ·ÎÁ§Æ®¿¡¼­ Sybase¿¡¼­ Oracle·ÎÀÇ µ¥ÀÌÅÍÄÁ¹öÁ¯ ¹× ÀÌ°üÀÛ¾÷¿¡ 2´ÞÀÌ °É·È´Ù´Â (³»°¡ ¾Æ¹«¸®
»ý°¢Çغ¸¾Æµµ rule¸¸ Á¤È®È÷ Á¤Àǵȴٸé 1´ÞÀ̸é Á·È÷ ³¡³µÀ¸¸®¶ó »ý°¢ÇÑ´Ù) ¾ê±â¸¦ µé¾ú´Ù.
°Ô´Ù°¡ ±× ÄÁ¹öÁ¯À» ´ã´çÇÑ »ç¶÷µéÀÌ ¿À·ÎÁö C¸¸ ÇÒÁÙ ¾Æ´Â »ç¶÷µéÀ̾ú´Âµ¥, ±×»ç¶÷µéÀÌ ÀÚ¹ÙAPI¸¦
Á» ´õ ÀÚÀ¯ÀÚÀç·Î¸¸ ¾µ ¼ö ÀÖ¾ú´õ¶óµµ ±×·± ½Ã°£³¶ºñ´Â ÇÏÁö ¾Ê¾ÒÀ¸¸®¶ó~
¼ÒÀ§ ¿¤¸®Æ®ÀǽĿ¡ Á¸½ÉÀÌ ÇÏ´ÃÀ» Â´Â S¸ð ȸ»ç¿¡¼­ ¸»ÀÌ´Ù. ¹«½¼ ¹æ¹ýÀ» ½èÀ»±î? 
Sybase Data -> SAM File -> File Read -> Conversion by rule -> Pro*C insert -> Oracle
°ÅÂü..ÂÁÂÁ.. 

ÀÚ¹ÙÀÇ Ç÷§Æû µ¶¸³~! ¿¹¼úÀÌ´Ù~

¶Ç À̾߱Ⱑ »ïõÆ÷·Î ºüÁö·Á ÇÑ´Ù. ^^

º»·ÐÀ¸·Î µé¾î°¡ÀÚ. 

À̱ÛÀ» ÀÐÀº ´ç½Å, ³î»õ´Â µ¿±âºÎ¿©¸¦ À§ÇÏ¿© ´ç½Å¿¡°Ô »ý°¢ÇÒ °ÍÀ» ÁÖ¹®ÇÑ´Ù. 
¾î¶»°Ô ÇÏ¸é °°Àº ÇÑÂÊ DBÀÇ µ¥ÀÌÅ͸¦ ´Ù¸¥ÂÊDB·Î ÇÁ·Î±×·¥ÀÇ ¼öÁ¤¾øÀÌ °è¼Ó ¿Å±â°Ô ÇÒ ¼ö ÀÖÀ»±î?

java.sqlÆÐÅ°Áö¸¦ ÀÌ¿ëÇÒ °ÍÀº ´ç¿¬ÇÏ°í, ÇÁ·Î±×·¥±¸µ¿½ÃÀÇ ÀÔ·ÂÀ» Å×À̺í¸í¸¸ ÁáÀ» °æ¿ì
µ¿ÀÛÀ» ÇÏ°Ô ÇÑ´Ù¸é sqlÆÐÅ°ÁöÀÇ »ç¿ëŬ·¡½º´Â?

Connection, PreparedStatement, ResultSet ¸¸ °¡Áö°í´Â ÇÒ ¼ö ¾øÀ» °ÍÀÌ´Ù. ±×·¸´Ù¸é Ãß°¡ÀûÀ¸·Î 
ÇÊ¿äÇÑ°ÍÀº ¹«¾ùÀΰ¡?
¹Ù·Î ResultSetMetaDataÀÎÅÍÆäÀ̽º°¡ ±× ¿ªÇÒÀ» Ãæ½ÇÇÒ °ÍÀÌ´Ù.
Å×À̺íÄ÷³ÀÇ À̸§ ¹× °³¼ö, ŸÀÔµîÀÌ ¸ðµÎ µé¾îÀֱ⠶§¹®¿¡ ±×·¯ÇÑ Á¤º¸¸¸ °¡Áö°í¼­µµ 
ÇØ´ç Å×ÀÌºí¿¡¼­ insert query¹®ÀåÀ» ÃßÃâÇس»´Â °ÍÀº ±×¸® ¾î·ÆÁö ¾ÊÀº ÀÏÀ̱⠶§¹®ÀÌ´Ù.

http://java.sun.com/j2se/1.4.1/docs/api/java/sql/ResultSetMetaData.html

À§ÀÇ ³»¿ëÀ» ÂüÁ¶ÇÏ°Ô µÇ¸é ´ëü ÇØ´ç ÀÎÅÍÆäÀ̽º°¡ ¹«¾ùÀ» ÇÏ°Ô µÇ´ÂÁö¸¦ ¾Ë°Ô µÉ°ÍÀÌ´Ù.

¹Ù·Î Äڵ带 º¸µµ·Ï ÇÏÀÚ. 


import java.sql.*;
import java.util.*;
public class DataConversion {
    public DataConversion(){}
    public void start(String tableName) {
        Connection tmp_conn = null;
        Connection abs_conn = null;
        PreparedStatement pstmt1 = null; // select pstmt
        PreparedStatement pstmt2 = null; // inser pstmt
        ResultSet rset = null;
        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@192.168.0.137:1521:abn";
            tmp_conn = DriverManager.getConnection(url, "abs_tmp", "abs_tmp");
            abs_conn = DriverManager.getConnection(url, "abn_new", "abn_new");
            abs_conn.setAutoCommit(false);
            StringBuffer query = new StringBuffer();
            query.append("select * from ").append(tableName);

            pstmt1 = tmp_conn.prepareStatement(query.toString());
            rset = pstmt1.executeQuery();

            ResultSetMetaData rsmd = null;
            int row = 0;
            int col = 0;
            rsmd = rset.getMetaData();
            //Ä÷³ÀÇ °¹¼ö¸¦ °¡Á®¿Â´Ù.
            col = rsmd.getColumnCount();
            System.out.println("Number of Column = " + col);

            //¹è¿­À» ÃʱâÈ­½ÃŲ´Ù.
            String [] columnNames = new String[col];
            int [] columnTypes = new int[col];
            StringBuffer insertQuery = new StringBuffer(1024);
            insertQuery.append("insert into ").append(tableName).append("(");
            for(int i = 0 ; i < col ; i++){
                    columnNames[i] = rsmd.getColumnName(i+1);
                    columnTypes[i] = rsmd.getColumnType(i+1);
            }

            for(int i = 0 ; i < columnNames.length; i++) {
                insertQuery.append(columnNames[i]);
                if( i != columnNames.length - 1 ) insertQuery.append(",");
            }
            insertQuery.append(") values (");
            for(int i = 0 ; i < columnNames.length; i++) {
                insertQuery.append("?");
                if( i != columnNames.length - 1 ) insertQuery.append(",");
            }
            insertQuery.append(")");
            System.out.println(insertQuery.toString());

            pstmt2 = abs_conn.prepareStatement(insertQuery.toString());

            for(int i = 1 ; rset.next() ; i++){
                for(int j = 0 ; j < col ; j++){
                    switch (columnTypes[j]) {
                        case	Types.NUMERIC: 
                            pstmt2.setLong(j+1, rset.getLong(j+1));
                            break;
                        case	Types.VARCHAR: 
                            pstmt2.setString(j+1, rset.getString(j+1));
                            break;
                        case	Types.TIMESTAMP: 
                            pstmt2.setDate(j+1, rset.getDate(j+1));
                            break;
                        default:
                    }
                }
                
                pstmt2.addBatch();
                if( i % 10000 == 0) {
                    pstmt2.executeBatch();
                    System.out.println(i + "°Ç ó¸®Áß ");
                }
            }

            pstmt2.executeBatch();
            abs_conn.commit();
        }catch(Exception e) {
            try{
                abs_conn.rollback();
            }catch(Exception e2){}
            e.printStackTrace();
        }finally{
            try{
                rset.close();
                pstmt1.close();
                tmp_conn.close();
                abs_conn.close();
            }catch(Exception e1) {}
        }
    }

    public static void main(String [] args) {
        new DataConversion().start(args[0]);
    }
}

    
¿äÁò ÇÁ·ÎÁ§Æ® Á¾·á¶§¹®¿¡ ³Ê¹« ¹Ùºü¼­ ±ÛÀ» ÀÚÁÖ ¿Ã¸®Áö ¸øÇÔÀ» Á˼ÛÇÏ°Ô »ý°¢ÇÕ´Ï´Ù.
 
1
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD