


** Properties 컬렉션 객체 **
try { Scanner sc = new Scanner(System.in); // Properties 객체 생성 Properties prop = new Properties(); System.out.print("생성할 파일 이름 : "); String fileName = sc.next(); // FileOutputStream FileOutputStream fos = new FileOutputStream(fileName + ".xml"); // Properties 객체를 이용해서 XML 파일 생성 prop.storeToXML(fos, fileName + ".xml file!!!"); System.out.println(fileName + ".xml 파일 생성 완료"); } catch (Exception e) { System.out.println("XML 파일 생성 중 예외발생"); e.printStackTrace(); } } }
이렇게 해서 만들면
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <!-- 마크업 언어 주석 --> <!-- ctrl + shift + / --> <!-- DB 연결을 위한 정보들을 별도 파일에 작성하여 읽어오는 형식으로 코드를 변경 이유 1 : Github에 DB연결 정보를 올리면 해킹하세요~ 라는 뜻 보안적인 측면에서 코드를 직접적으로 보지 못하게 함 이유 2 : 혹시라도 연결하는 DB 정보가 변경될 경우 JAVA 코드가 아닌 읽어오는 파일의 내용을 수정하면 되기 때문에 JAVA 코드 수정 X -> 추가 컴파일 필요 X -> 개발 시간 단축!!! --> <comment>driver.xml</comment> <!-- entry 태그 (K : V 한 쌍) --> <entry key ="driver">oracle.jdbc.driver.OracleDriver</entry> <entry key = "url">jdbc:oracle:thin:@localhost:1521:XE</entry> <entry key = "userName">kh</entry> <entry key = "password">kh1234</entry> </properties>
이걸 읽으면!!!
// XML 파일 읽어오기 (FileInputStream, Properties) try { Properties prop = new Properties(); // driver.xml 파일을 읽기 위한 InputStream 객체 생성 FileInputStream fis = new FileInputStream("driver.xml"); // 연결된 driver.xml 파일에 있는 내용을 모두 읽어와 // Properties 객체에 K:V 형식으로 저장 prop.loadFromXML(fis); // prop.getProperty("key") : key가 일치하는 속성값(value)을 얻어옴 String driver = prop.getProperty("driver"); String url = prop.getProperty("url"); String userName = prop.getProperty("userName"); String password = prop.getProperty("password"); Class.forName(driver); Connection conn = DriverManager.getConnection(url, userName, password); System.out.println(conn); } catch(Exception e) { e.printStackTrace(); } /* * 왜 XML 파일을 이용해서 JDBC를 진행하는가? * * 1. DB 연결정보 / 드라이버 정보 등 코드 중복 제거 * 2. 보안 측면에서 별도 관리 필요 * 3. 재컴파일을 진행하지 않기 위해서 * 4. XML 파일에 작성된 문자열 형태를 그대로 읽어오기 때문에 * XMl 파일에 SQL문 작성 시 다루기가 좀 더 편리해짐. */ } }
**** 중요 ****
어디서든지 JDBCTemplate 클래스를
객체로 만들지 않고도 메서드를 사용할 수 있도록 하기 위해
모든 메서드를 public static으로 선언
-> 싱글톤 패턴 적용
public class JDBCTemplate { // 필드 private static Connection conn = null; // -> static 메서드에서 사용할 static 필드 선언 // 메서드 /** 호출 시 Connection 객체를 생성해서 반환하는 메서드 + AutoCommit 끄기 * @return conn */ public static Connection getConnection() { try { // 이전에 참조하던 Connection 객체가 존재하고 // 아직 close() 된 상태가 아니라면 // 새로 만들지 않고 기존 Connection 반환 if( conn != null && !conn.isClosed() ) { return conn; } // 1. Properties 객체 생성 Properties prop = new Properties(); // 2. Properties가 제공하는 메서드를 이용해서 driver.xml 파일 내용을 읽어오기 String filePath = "driver.xml"; prop.loadFromXML(new FileInputStream(filePath)); // 3. prop에 저장된 값을 이용해서 Connection 객체 생성 Class.forName(prop.getProperty("driver")); // Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("userName"), prop.getProperty("password")); // conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","kh","kh1234") // 4. 만들어진 Connection에서 AutoCommit 끄기 conn.setAutoCommit(false); } catch (Exception e) { System.out.println("커넥션 생성 중 예외 발생.."); e.printStackTrace(); } return conn; }
// ------------------------------------------------------------------------------- /** 전달 받은 커넥션에서 수행한 SQL을 Commit 하는 메서드 * @param conn */ public static void commit(Connection conn) { try { if(conn != null && !conn.isClosed()) conn.commit(); } catch (Exception e) { System.out.println("커밋 중 예외 발생"); e.printStackTrace(); } } /** 전달 받은 커넥션에서 수행한 SQL을 ROllBACK 하는 메서드 * @param conn */ public static void rollback(Connection conn) { try { if(conn != null && !conn.isClosed()) conn.rollback(); } catch (Exception e) { System.out.println("롤백 중 예외발생"); e.printStackTrace(); } }
// ---------------------------------------------------------------------------- // 커넥션, Statement(PreparedStatement), ResultSet /** 전달 받은 커넥션을 close(자원반환) 하는 메서드 * @param conn */ public static void close(Connection conn) { try { if(conn != null && !conn.isClosed()) conn.close(); } catch (Exception e) { System.out.println("커넥션 close() 중 예외발생"); e.printStackTrace(); } } /** 전달 받은 Statement or PreparedStatement 둘 다 close() 하는 메서드 * + 다형성의 업캐스팅 적용 * -> PreparedStatement는 Statement 의 자식 * @param stmt */ public static void close(Statement stmt) { try { if(stmt != null && !stmt.isClosed()) stmt.close(); } catch (Exception e) { System.out.println("Statement close()중 예외 발생"); e.printStackTrace(); } } public static void close(ResultSet rs) { try { if(rs != null && !rs.isClosed()) rs.close(); } catch (Exception e) { System.out.println("ResultSet close() 중 예외 발생"); e.printStackTrace(); } } }