package com.eactive.eai.common.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class JdbcTest {
	
//	String driver        = "org.mariadb.jdbc.Driver";
//	String driver        = "com.mysql.jdbc.Driver";
	String driver		 = "com.ibm.db2.jcc.DB2Driver"; //db2
//  String url           = "jdbc:mariadb://localhost:4406/eai?useUnicode=true&characterEncoding=euckr";
    String url			 = "jdbc:db2://localhost:50000/DSEADT";
    String uId           = "db2admin";
    String uPwd          = "e1001";
    
    Connection               con;
    PreparedStatement        pstmt;
    ResultSet                rs;
    
    public JdbcTest() {
         try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, uId, uPwd);
            System.out.println(con);
            if( con != null ){ System.out.println("데이터 베이스 접속 성공"); }
            
        } catch (ClassNotFoundException e) { 
        	//e.printStackTrace();
        	System.out.println("드라이버 로드 실패");    
        } 
        catch (SQLException e) { 
        	//e.printStackTrace();
        	System.out.println(e);
        	System.out.println("데이터 베이스 접속 실패"); 
        }
    }
    
    
    public void insert(String id, String name){
        String sql    = "insert into test values(?, ?)";
        try {
            pstmt                = con.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.setString(2, name);
            pstmt.executeQuery();
            
        } catch (SQLException e) { 
        	//e.printStackTrace();
        	System.out.println("쿼리 수행 실패"); 
        }
    }
    
    public void select(){
        String sql    = "select * from test";
        try {
            pstmt                = con.prepareStatement(sql);
            rs                   = pstmt.executeQuery();
            while(rs.next()){
                System.out.println("id    : " + rs.getString("id"));
                System.out.println("name     : " + rs.getString("name"));
            }
        } catch (SQLException e) { System.out.println("쿼리 수행 실패"); }
        finally {
        try { if(rs!=null) rs.close();} catch(Exception e) {}
        try { if(con!=null) con.close();} catch(Exception e) {}
    	}   
    }
    
    public static void main(String[] args){
    	JdbcTest dbm    = new JdbcTest();
    }
}
