import java.sql.*;

public class NagiosDBTest
{

        public static void
        main(String[] args) throws ClassNotFoundException
        {
                String user = "guest";
                String pass = "guest";
                String query = "select 'ACCESS_SUCCESSFUL' from dual;";
                String connstr = "jdbc:oracle:thin:@oracledb:1521:DB";
		String result = "";

		if (args[0] != null)
			connstr = args[0];
                if (args[1] != null)
                	user = args[1];
                if (args[2] != null)
                	pass = args[2];
                if (args[3] != null)
                	query = args[3];

                // Load the Oracle Driver
                Class.forName("oracle.jdbc.driver.OracleDriver");

                try
                {
                          // Get a connection from the connection factory
                        Connection con = DriverManager.getConnection(connstr,user,pass);

                        // Create a Statement object so we can submit SQL statements to the driver
                        Statement stmt = con.createStatement();

                        // Submit a query, creating a ResultSet object
                        ResultSet rs = stmt.executeQuery(query);

                        // Display all columns and rows from the result set
                        result = processResultSet(rs);

                        // Close the result set
                        rs.close();

                        // Close the statement
                        stmt.close();

                        // Close the connection
                        con.close();

			if (result.length()>0) {
	                        System.out.println("Query Result: "+result);
				System.exit(0);
			}
			else {
				System.out.println("Query ResultSet Empty!");
                                System.exit(2);
			}
                }
                catch (SQLException e)
                {
                        printSQLExceptions(e);
                 }
        }

        /**
        * This method displays all columns and rows in the given result set.
        * @param rs  The result set to be displayed.
        * @return  None.
        * @exception SQLException.
        */

        static public String processResultSet(ResultSet rs) throws SQLException
        {
                int i;
		String output = "";

                // Get the ResultSetMetaData.  This will be used for the column headings
                ResultSetMetaData rsmd = rs.getMetaData();

                // Get the number of columns in the result set
                int numCols = rsmd.getColumnCount();

                // Display data, fetching until end of the result set
                while (rs.next())
                {
                        for (i = 1; i <= numCols; i++)
                        {
                                if (i > 1) 	
					output += ",";
                                output += rs.getString(i);
                        }
                }
		return output;
        }

        /**
        * This method checks for exceptions and displays error information.
        * Note that multiple exception objects could be chained together.
        * Very similar to printSQLWarnings.
        * @param warn The list of SQLException.
        * @return true if an exception existed, false otherwise.
        * @see Utils#printSQLWarnings
        */
        static public void printSQLExceptions(SQLException ex)
        {
                boolean rc = false;
		String output = "SQLException: Messages = ";

                if (ex != null)
                {
                        rc = true;
                        while (ex != null)
                        {
                                output += ex.getMessage();
                                ex = ex.getNextException();
                        }
                }
		System.out.println(output);
		System.exit(2);
        }

}

