package com.venstar.services;

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

/*
 * MySQLService Class handles all the mysql connections that the gui requires.
 * author @ Adrian Sanchez
 */

public class MySQLService {
	//Server variables
	private String DBHOST = "";
	private String DBNAME = "";
	private String DBUNAME = "";
	private String DBUPASS = "";
	
	private Connection CONN = null;
	private String ConnectionURL = "";
	
	
	public MySQLService(String host, String name, String uname, String upass)
	{
		ConnectionURL = "jdbc:mysql://"+host+'/'+name;
		setDBUNAME(uname);
		setDBUPASS(upass);
	}
	
	public void connect() throws Exception
	{
		try{
		 Class.forName("com.mysql.jdbc.Driver").newInstance();
		 CONN = DriverManager.getConnection(ConnectionURL, getDBUNAME(), getDBUPASS());
	  } catch (InstantiationException e) {
        e.printStackTrace();
	  } catch (IllegalAccessException e) {
        e.printStackTrace();
	  } catch (ClassNotFoundException e) {
        e.printStackTrace();
	   }
	}
	
	public ResultSet doQuery(String query)
	{
		ResultSet result = null;
		try{
			Statement stc = CONN.createStatement();
			result = stc.executeQuery(query);
		}catch(SQLException ex){
			ex.printStackTrace();
		}
	    return result;
	}
	
	public void close() throws SQLException
	{
		CONN.close();
	}
	
	/* getter and setter for MySQLService */
	public void setDBHOST(String dBHOST) {
		DBHOST = dBHOST;
	}

	public String getDBHOST() {
		return DBHOST;
	}

	public void setDBNAME(String dBNAME) {
		DBNAME = dBNAME;
	}

	public String getDBNAME() {
		return DBNAME;
	}

	public void setDBUNAME(String dBUNAME) {
		DBUNAME = dBUNAME;
	}

	public String getDBUNAME() {
		return DBUNAME;
	}

	public void setDBUPASS(String dBUPASS) {
		DBUPASS = dBUPASS;
	}

	public String getDBUPASS() {
		return DBUPASS;
	}
}
