package dbqueries;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DataBase {

    private static String databaseUserName = "root"; // MySql user name
    private static String databasePassword = "root"; // MySQL password
    private static String URL = "jdbc:mysql://localhost:3306/sdms";

    private static String databaseDriver = "com.mysql.jdbc.Driver";

    private static Connection con = null;
    private static Statement st = null;

    public DataBase() {
    }

    public static Connection getConnection() throws SQLException {

        if (con == null) {
            try {
                Class.forName(databaseDriver);
                con = DriverManager.getConnection(URL,databaseUserName,databasePassword);


            } catch (Exception e) {
                e.printStackTrace();

            }
        }
        return con;
    }

    static boolean authenticateUser(String s, String s1) {

        try {
            con = getConnection();
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {



            st = con.createStatement();

            String query = "select * from sdms.signup where empid = '" + s + "' and  Password = '" + s1 + "'";
            ResultSet rs = st.executeQuery(query);
            if(rs.next()){
                return true;
            }


        } catch (Exception e) {
            e.printStackTrace();
            
        }
        return false;

    }

    public static String getUsername(String userid) {
        ResultSet rs = null;
        try {

            st = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            rs = st.executeQuery("select first_name from sdms.signup where empid = '" + userid + "'");
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            if (rs.next()) {

                return rs.getString("first_name");
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;

    }
     public static String[] getEmailIDPassword(String userid) {
        ResultSet rs = null;
        String[]details=new String[4];
        try {

            st = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            rs = st.executeQuery("select email,password from sdms.signup where empid = '" + userid + "'");
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            if (rs.next()) {

                details[0]=rs.getString("email");
                details[1]=rs.getString("password");
                return details;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;

    }
     
    
     
     
}
