/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package adgsyncms.repository;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.concurrent.Executor;

/**
 *
 * @author amartinez
 */
public class db_express {

    public static Connection _ex_connection;
    public static boolean _ex_return = false;
    public static Statement _ex_stmt;
    public static String _ex_SQLQuery = "";
    public static String _ex_Host;
    public static String _ex_Database;
    public static String _ex_User;
    public static String _ex_Port;
    public static String _ex_Password;
    public static String Ultimo_Error;

    public static boolean ex_openConnection() {

//        repository repo1 = new repository();
//        HashMap hashMap = repo1.readSetting();

        try {
            //set value to variables
            //_ex_Host = hashMap.get("db_host_name").toString();
//            _ex_Database = hashMap.get("ex_name").toString();
//            _ex_User = hashMap.get("ex_user").toString();
//            _ex_Port = hashMap.get("ex_port").toString();
//            _ex_Password = hashMap.get("ex_secret").toString();

            //register class name 
            Class.forName("org.postgresql.Driver");

            //connect to database
            DriverManager.setLoginTimeout(10);

            _ex_connection = DriverManager.getConnection("jdbc:postgresql://" + _ex_Host.trim() + ":" + _ex_Port + "/" + _ex_Database, _ex_User, _ex_Password);

            //set auto commit false
            _ex_connection.setAutoCommit(false);
            //_ex_connection.setClientInfo("ApplicationName","ADGSync MS");
            //set return to true
            _ex_return = true;
        } catch (Exception e) {
            //set return    
            Ultimo_Error = e.getMessage();
            _ex_return = false;
        }
        return _ex_return;
    }

    public static void ex_closeConnection() {
        //if connection if not null
        if (_ex_connection != null) {
            //close 
            try {
                _ex_connection.close();
            } catch (Exception e) {
                // message.show(e.getMessage());
            }
        }
    }

}
