package utility;
import java.util.Vector;
import java.sql.*;

public class Query
{
	private java.sql.Connection conn;
	public Query()
	{
		conn=DbConnection.getDbConnection();
	}
	synchronized  public ResultSelect selectQuery(String query)
	{
		ResultSelect resSel=null;
		Statement st=null;
		ResultSet rs=null;
		try
		{
			st=conn.createStatement();
			rs=st.executeQuery(query);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

		if(rs == null)
			System.out.println("rs is null");
		else
			resSel=new ResultSelect(rs);
		try
		{
			if (rs != null)
				rs.close();
			if (st != null)
				st.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return resSel;
	}
	synchronized public int updateQuery(String query)
	{
		int effectedRows = 0;
		Statement st = null;
		try
		{
			st=conn.createStatement();
			effectedRows = st.executeUpdate(query);
		}
		catch(Exception e)
		{
			//e.printStackTrace();
			System.out.println(e);
		}
		finally
		{
			try
			{
				if (st != null)
					st.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		return effectedRows;
	}
	protected void finalize()
	{
		try
		{
			if (conn !=null)
				conn.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
