[Java] 싱글턴패턴으로 connection 객체 생성

Published: by Creative Commons Licence

import java.sql.Connection;
import java.sql.DriverManager;

public class Connector {
    private static Connection conn = null;

    private Connector() {}

    public static Connection getConnection() {
        String url = "jdbc:oracle:thin:@220.76.176.66:1521:orcl", user = "noritersand", pwd = "java301$!";
        if (conn == null) {
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                conn = DriverManager.getConnection(url, user, pwd);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return conn;
    }

    public static void close() {
        if (conn != null) {
            try {
                if (!conn.isClosed())
                    conn.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        conn = null;
    }
}