/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package LoginDb;

/**
 *
 * @author 590685
 */
import java.sql.*;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DbHandler {
//	private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
//	private static final String DB_CONNECTION = "jdbc:oracle:thin:@172.18.15.109:1521:INSP";
//	private static final String DB_USER = "pacsdata";
//	private static final String DB_PASSWORD = "pacsdata";

    public static Connection getDBConnection() {
//            String dbUrl = "";
//        String dbUsr = "";
//        String dbPass = "";
//        String dbSchema = "";

        Connection dbConnection = null;

//            try {
//                    Class.forName(DB_DRIVER);
//                }catch (ClassNotFoundException e) {
//                    System.out.println(e.getMessage());
//                }
//            try {
//                    dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,DB_PASSWORD);
//			return dbConnection;
//                }catch (SQLException e) {
//                    System.out.println(e.getMessage());
//                }
        String DATASOURCE_CONTEXT = "java:comp/env/jdbc/conDS";
        try {
            Context initialContext = new InitialContext();
            if (initialContext == null) {
                System.out.println("JNDI problem. Cannot get InitialContext.");
            }
            DataSource datasource = (DataSource) initialContext.lookup(DATASOURCE_CONTEXT);
            if (datasource != null) {
                dbConnection = datasource.getConnection();
            } else {
                System.out.println("Failed to lookup datasource.");
            }
        } catch (NamingException ex) {
           // System.out.println("Cannot get connection: " + ex);
        } catch (SQLException ex) {
           // System.out.println("Cannot get connection: " + ex);
        } finally {
            System.out.println("Processing done.");
        }

//            try {
//           // InputStream input = DbHandler.class.getClassLoader().getResourceAsStream("config.properties");
//            Properties prop = new Properties();
//            
//            InputStream input = LoginDb.DbHandler.class.getResourceAsStream("config.properties");
//            prop.load(input);
//           // Properties prop = new Properties();
//            if (prop == null) {
//                System.out.println("Sorry, unable to find config.properties");
//                
//            }           
//
//            dbUrl = prop.getProperty("DB_URL");
//            dbUsr = prop.getProperty("DB_USER");
//            dbPass = prop.getProperty("DB_PASS");
//            dbSchema = prop.getProperty("DB_SCHEMA");            
//
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }
//            
//            try { 
//                try{
//                    LoginDb.DbHandler.class.forName(DB_DRIVER);
//                    }catch (ClassNotFoundException e) {
//                 System.out.println(e.getMessage());
//                }
//                   
//                    dbConnection = DriverManager.getConnection(dbUrl, dbUsr, dbPass);
//                    if (dbConnection == null) {
//                    System.out.println("Cannot get connection.");
//                    }
//            } catch(SQLException ex){
//                    System.out.println("Cannot get connection: " + ex);
//                    }                                                    
        return dbConnection;
    }

    public static void closeDbConnection(Connection conn) throws SQLException {
        try {
            conn.close();
        } catch (SQLException ex) {
            // ex.printStackTrace();
            System.out.println("Error occurred during connection close.");
        }

    }
}
