//$Id$
package com.adventnet.zoho.client.report.jdbc.v2;

import java.sql.*;
import java.util.Properties;

public class DataSample
{
    public static void main(String args[])
    {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try
        {
            Class.forName("com.zoho.analytics.jdbc.ZohoAnalyticsDriver");
            Properties conProps = new Properties();

            // ZohoAnalytics OAuth authentication parameters
            conProps.put("CLIENT_ID","1000.XXXXXXXXXX");
            conProps.put("CLIENT_SECRET","XXXXXXXXXXXXXXXXXXXX");
            conProps.put("REFRESH_TOKEN","1000.XXXXXXXXXX.XXXXXXXXXX");

            // Uncomment this in case you need proxy settings
            /*
            // Proxy host name to connect the internet
            conProps.put("PROXYSERVER","proxy_hostname");
            // Proxy port to connect the internet
            conProps.put("PROXYPORT","proxy_port");
            // Proxy username to connect the internet
            conProps.put("PROXYUSERNAME","proxy_username");
            // Proxy password to connect the internet
            conProps.put("PROXYPASSWORD","proxy_password");
            */

            con = DriverManager.getConnection("jdbc:zohoanalytics://<Domain_Name>/<Org_ID>/<Workspace_Name>",conProps);
            //con = DriverManager.getConnection("jdbc:zohoanalytics://analyticsapi.zoho.com/15068809/SalesWorkspace",conProps);
            stmt = con.createStatement();
            String sql ="select * from \"Sales-Data\"";
            rs = stmt.executeQuery(sql);
            while (rs.next())
            {
                Date date = rs.getDate("Date");
                String region = rs.getString("Region");
                System.out.println(date.toString() + "," + region);
            }
        }
        catch(SQLException se)
        {
            System.out.println("SQLException: " + se.getMessage());
            System.out.println("Zoho Analytics Error code: " + se.getErrorCode());
        }
        catch(Exception e)
        {
            System.out.println("Exception: " + e.getMessage());
        }
        finally
        {
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException sqlEx)
                {
                    System.out.println(sqlEx.getMessage());
                }
            }
            if(stmt != null)
            {
                try
                {
                    stmt.close();
                }
                catch (SQLException sqlEx)
                {
                    System.out.println(sqlEx.getMessage());
                }
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch (SQLException sqlEx)
                {
                    System.out.println(sqlEx.getMessage());
                }
            }
        }
    }
}