본문 바로가기

JAVA를 잡아라!/Java

상위 레코드읽어서 레코드복사하여 출력시키는 예제

/*
아래의 구문은 northwind DB에서 Customers 테이블에서 상위 20개의
레코드를 읽어서 pubs DB의 Temp_Customers 테이블을 생성하여 레코드를
복사하는 프로그램입니다.

*/
import java.sql.*;
import java.util.*;
import java.io.*;

public class  JavaTop20
{
        public static void main(String[] args)        {

           String src_url= "jdbc:odbc:northwind"; //사용폴더지정pubs경우는 pubs로 바꿔줘야한다.
              Connection  src_con   = null;
              Statement   src_stmt  = null;

              String driverSpec  = "sun.jdbc.odbc.JdbcOdbcDriver";
           String con_id       = "sam";
           String con_pw
       = "1234"; //아이디/비번지정안되있을경우에는 무시된다~

                try         {
                       
                                Class.forName(driverSpec);                       

                    } catch (ClassNotFoundException e) {

                                  System.err.println(e.getMessage());
                 }       

                try {

                                src_con  = DriverManager.getConnection(src_url, con_id, con_pw);       
                               
                                src_stmt = src_con.createStatement();

                                String src_sql = "SELECT TOP 20 * FROM customers";

                                ResultSet    src_rs            = src_stmt.executeQuery(src_sql);
                            
                                ResultSetMetaData meta    = src_rs.getMetaData();                       
                               
                                int row_count                    = 0;
                                int col_count                     = meta.getColumnCount();

                                String src_rs_array[]   = new String[col_count];


                                  for (int i = 1; i <= col_count; i++)       {

                                  System.out.print(meta.getColumnName(i)  + " | ");
                                  }

                                        System.out.println("");

                                  for (int i = 1; i <= col_count*15; i++)         {

                                        System.out.print("-");

                                  }                                

                                          System.out.println("");


                        while ( (src_rs.next()) && (row_count < 20) )        {

                                   for (int i = 0; i < col_count; i++)         {

                                                        src_rs_array[i] = src_rs.getString(i+1);

                                                    System.out.print(src_rs_array[i] + " | ");
                               
                                        }

                                        System.out.println("");

                                        row_count++;

                         }


                } catch (SQLException e) {

                          System.out.println(e.getMessage());

                } finally {

                         if (src_stmt!=null)   try { src_stmt.close();   } catch (Exception e) { }
               
                         if (src_con!=null)    try { src_con.close();     } catch (Exception e) { }
               
                }
        }
}