HW3.java Source code

Java tutorial

Introduction

Here is the source code for HW3.java

Source

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashMap;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author nsankhe
 */
public class HW3 extends javax.swing.JFrame {

    /**
     * Creates new form HW3
     */
    final int MAX_MAP_LIST_COUNT = 50;
    final int MAX_ATTR_LIST_COUNT = 50;
    private HashMap<JCheckBox, String>[] mapList;
    private HashMap<JCheckBox, String>[] AttrList;
    private ArrayList<String> attrNames;
    private ArrayList<String> subCatNames;
    private ArrayList<String> selectMainCat;
    private ArrayList<String> selectSubCat;
    private ArrayList<String> selectedAttrList;
    int mapListCount, attrListCount;
    private Object selectedBid;

    public HW3() {
        this.mapList = new HashMap[MAX_MAP_LIST_COUNT];
        this.AttrList = new HashMap[MAX_ATTR_LIST_COUNT];
        this.selectedAttrList = new ArrayList<String>();
        this.attrNames = new ArrayList<String>();
        this.subCatNames = new ArrayList<String>();
        this.selectMainCat = new ArrayList<String>();
        this.selectSubCat = new ArrayList<String>();
        this.selectedBid = null;
        mapListCount = 0;
        attrListCount = 0;
        initComponents();
        initComboBoxes();
    }

    ;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        topPanel = new javax.swing.JPanel();
        mainCategoryPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        subCategoryPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        attrPanel = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        dayLabel = new javax.swing.JLabel();
        dayComboBox = new javax.swing.JComboBox();
        fromLabel = new javax.swing.JLabel();
        fromComboBox = new javax.swing.JComboBox();
        toLabel = new javax.swing.JLabel();
        toComboBox = new javax.swing.JComboBox();
        searchLabel = new javax.swing.JLabel();
        searchComboBox = new javax.swing.JComboBox();
        searchButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        cityComboBox = new javax.swing.JComboBox();
        stateComboBox = new javax.swing.JComboBox();
        zipComboBox = new javax.swing.JComboBox();
        cityLabel = new javax.swing.JLabel();
        stateLabel = new javax.swing.JLabel();
        zipLabel = new javax.swing.JLabel();
        clearButton = new javax.swing.JButton();
        openCloseComboBox = new javax.swing.JComboBox();
        openCloseLabel = new javax.swing.JLabel();
        reviewPanel = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        backButton = new javax.swing.JButton();
        displayPiechartButton = new javax.swing.JButton();
        displayBarchartButton = new javax.swing.JButton();
        startButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("YELP SEARCH");
        setBackground(new java.awt.Color(255, 255, 102));
        setBounds(new java.awt.Rectangle(0, 0, 1000, 800));
        setPreferredSize(new java.awt.Dimension(950, 710));
        setResizable(false);

        topPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(
                javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 4),
                new javax.swing.border.LineBorder(java.awt.Color.red, 2, true)));
        topPanel.setPreferredSize(new java.awt.Dimension(930, 700));
        topPanel.setVisible(false);

        mainCategoryPanel.setBackground(new java.awt.Color(204, 204, 255));
        mainCategoryPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.black, null));
        mainCategoryPanel.setPreferredSize(new java.awt.Dimension(200, 300));

        javax.swing.GroupLayout mainCategoryPanelLayout = new javax.swing.GroupLayout(mainCategoryPanel);
        mainCategoryPanel.setLayout(mainCategoryPanelLayout);
        mainCategoryPanelLayout.setHorizontalGroup(mainCategoryPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 171, Short.MAX_VALUE));
        mainCategoryPanelLayout.setVerticalGroup(mainCategoryPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 496, Short.MAX_VALUE));

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane2.setPreferredSize(new java.awt.Dimension(131, 280));

        subCategoryPanel.setBackground(new java.awt.Color(204, 255, 255));
        subCategoryPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.black, null));

        javax.swing.GroupLayout subCategoryPanelLayout = new javax.swing.GroupLayout(subCategoryPanel);
        subCategoryPanel.setLayout(subCategoryPanelLayout);
        subCategoryPanelLayout.setHorizontalGroup(subCategoryPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        subCategoryPanelLayout.setVerticalGroup(subCategoryPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));

        jScrollPane2.setViewportView(subCategoryPanel);

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jScrollPane1.setPreferredSize(new java.awt.Dimension(131, 280));

        attrPanel.setBackground(new java.awt.Color(255, 255, 204));
        attrPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.black, null));

        javax.swing.GroupLayout attrPanelLayout = new javax.swing.GroupLayout(attrPanel);
        attrPanel.setLayout(attrPanelLayout);
        attrPanelLayout.setHorizontalGroup(attrPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        attrPanelLayout.setVerticalGroup(attrPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));

        jScrollPane1.setViewportView(attrPanel);

        jScrollPane5.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane5.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane5.setPreferredSize(new java.awt.Dimension(131, 280));

        jTable2.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.black, null));
        jTable2.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {},
                new String[] { "Business_Name", "City", "State", "Stars", "Business_ID", "Zip" }));
        jTable2.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable2MousePressed(evt);
            }
        });
        jScrollPane5.setViewportView(jTable2);

        dayLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 12)); // NOI18N
        dayLabel.setForeground(java.awt.Color.red);
        dayLabel.setText("Day of the week");
        dayLabel.setToolTipText("");

        dayComboBox.setBackground(java.awt.Color.magenta);
        dayComboBox.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }));
        dayComboBox.setSelectedIndex(-1);
        dayComboBox.setToolTipText("Please select the day");
        dayComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dayComboBoxActionPerformed(evt);
            }
        });

        fromLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 12)); // NOI18N
        fromLabel.setForeground(java.awt.Color.red);
        fromLabel.setText("From:");

        fromComboBox.setBackground(java.awt.Color.magenta);
        fromComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00:00", "01:00", "02:00",
                "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00",
                "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00" }));
        fromComboBox.setSelectedIndex(-1);
        fromComboBox.setToolTipText("Please select the start time");

        toLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 12)); // NOI18N
        toLabel.setForeground(java.awt.Color.red);
        toLabel.setText("To:");

        toComboBox.setBackground(java.awt.Color.magenta);
        toComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00:00", "01:00", "02:00", "03:00",
                "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00",
                "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00" }));
        toComboBox.setSelectedIndex(-1);
        toComboBox.setToolTipText("Please select the end time");

        searchLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 12)); // NOI18N
        searchLabel.setForeground(java.awt.Color.red);
        searchLabel.setText("Search For");

        searchComboBox.setBackground(java.awt.Color.magenta);
        searchComboBox.setModel(
                new javax.swing.DefaultComboBoxModel(new String[] { "Any attributes", "All attributes" }));
        searchComboBox.setSelectedIndex(-1);
        searchComboBox.setToolTipText("Please select the attributes");

        searchButton.setBackground(new java.awt.Color(255, 255, 0));
        searchButton.setFont(new java.awt.Font("DejaVu Sans", 1, 12)); // NOI18N
        searchButton.setForeground(java.awt.Color.blue);
        searchButton.setText("SEARCH");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        closeButton.setBackground(new java.awt.Color(102, 255, 102));
        closeButton.setFont(new java.awt.Font("DejaVu Sans", 1, 12)); // NOI18N
        closeButton.setForeground(java.awt.Color.blue);
        closeButton.setText("CLOSE");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        cityComboBox.setBackground(java.awt.Color.magenta);
        cityComboBox.setToolTipText("Please select city");

        stateComboBox.setBackground(java.awt.Color.magenta);
        stateComboBox.setToolTipText("Please select state");
        stateComboBox.setSelectedIndex(-1);

        zipComboBox.setBackground(java.awt.Color.magenta);
        zipComboBox.setToolTipText("Please select zip");
        zipComboBox.setSelectedIndex(-1);

        cityLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        cityLabel.setForeground(java.awt.Color.red);
        cityLabel.setText("City");

        stateLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        stateLabel.setForeground(java.awt.Color.red);
        stateLabel.setText("State");

        zipLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        zipLabel.setForeground(java.awt.Color.red);
        zipLabel.setText("Zip");

        clearButton.setBackground(new java.awt.Color(255, 255, 51));
        clearButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        clearButton.setForeground(java.awt.Color.blue);
        clearButton.setText("CLEAR COMBO_BOXES");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        openCloseComboBox.setBackground(java.awt.Color.magenta);
        openCloseComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "open", "close" }));
        openCloseComboBox.setSelectedIndex(-1);

        openCloseLabel.setForeground(java.awt.Color.red);
        openCloseLabel.setText("OPEN OR CLOSE day");

        javax.swing.GroupLayout topPanelLayout = new javax.swing.GroupLayout(topPanel);
        topPanel.setLayout(topPanelLayout);
        topPanelLayout.setHorizontalGroup(topPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(topPanelLayout.createSequentialGroup().addGap(16, 16, 16).addGroup(topPanelLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(topPanelLayout.createSequentialGroup()
                                .addComponent(mainCategoryPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 175,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 217,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 271,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                topPanelLayout.createSequentialGroup().addGroup(topPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(topPanelLayout.createSequentialGroup().addGroup(topPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(dayLabel)
                                                .addComponent(dayComboBox, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        98, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(18, 18, 18)
                                                .addGroup(topPanelLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(fromLabel).addComponent(fromComboBox,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 89,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(18, 18, 18)
                                                .addGroup(topPanelLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(topPanelLayout.createSequentialGroup()
                                                                .addGap(1, 1, 1).addComponent(toLabel))
                                                        .addComponent(toComboBox,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 88,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGroup(topPanelLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(topPanelLayout.createSequentialGroup()
                                                                .addGap(10, 10, 10).addComponent(searchComboBox,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 104,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                                topPanelLayout.createSequentialGroup()
                                                                        .addGap(18, 18, 18)
                                                                        .addComponent(searchLabel,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                96,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(topPanelLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(cityComboBox, 0, 95, Short.MAX_VALUE)
                                                        .addGroup(topPanelLayout.createSequentialGroup()
                                                                .addComponent(cityLabel)
                                                                .addGap(0, 0, Short.MAX_VALUE)))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(topPanelLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addGroup(topPanelLayout.createSequentialGroup()
                                                                .addComponent(stateLabel).addGap(18, 18, 18))
                                                        .addGroup(topPanelLayout.createSequentialGroup()
                                                                .addComponent(stateComboBox, 0, 54, Short.MAX_VALUE)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                                .addGroup(topPanelLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(zipComboBox,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 81,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(zipLabel))
                                                .addGap(18, 18, 18).addComponent(searchButton)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(closeButton))
                                        .addGroup(topPanelLayout.createSequentialGroup().addGap(10, 10, 10)
                                                .addComponent(openCloseLabel).addGap(18, 18, 18)
                                                .addComponent(openCloseComboBox,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 79,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(clearButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        160, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(17, 17, 17)))));
        topPanelLayout
                .setVerticalGroup(topPanelLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(topPanelLayout
                                .createSequentialGroup().addGroup(topPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(topPanelLayout.createSequentialGroup().addGap(17, 17, 17)
                                                .addComponent(mainCategoryPanel,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 500,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(topPanelLayout
                                                .createSequentialGroup().addContainerGap().addGroup(topPanelLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jScrollPane2,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 500,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jScrollPane1,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 500,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(
                                                                jScrollPane5,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 500,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addGroup(topPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(topPanelLayout.createSequentialGroup()
                                                .addComponent(dayLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(2, 2, 2).addComponent(dayComboBox,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 29,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(topPanelLayout.createSequentialGroup().addGap(5, 5, 5)
                                                .addGroup(topPanelLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(topPanelLayout.createSequentialGroup()
                                                                .addComponent(fromLabel).addGap(2, 2, 2)
                                                                .addComponent(fromComboBox,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 29,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(topPanelLayout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                        .addComponent(openCloseComboBox,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(openCloseLabel))
                                                                .addGap(5, 5, 5))
                                                        .addGroup(topPanelLayout.createSequentialGroup()
                                                                .addGroup(topPanelLayout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                        .addComponent(searchButton,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                38,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(closeButton,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                38,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(clearButton,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE, 38,
                                                                        Short.MAX_VALUE))
                                                        .addGroup(topPanelLayout.createSequentialGroup()
                                                                .addGroup(topPanelLayout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addGroup(topPanelLayout
                                                                                .createSequentialGroup()
                                                                                .addComponent(toLabel)
                                                                                .addGap(2, 2, 2)
                                                                                .addGroup(topPanelLayout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                        .addComponent(toComboBox,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                29,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                        .addComponent(
                                                                                                searchComboBox,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                29,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                        .addComponent(cityComboBox,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                29,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                        .addComponent(stateComboBox,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                29,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                        .addComponent(zipComboBox,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                29,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                                        .addGroup(topPanelLayout
                                                                                .createParallelGroup(
                                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                .addComponent(searchLabel)
                                                                                .addComponent(cityLabel)
                                                                                .addComponent(stateLabel)
                                                                                .addComponent(zipLabel)))
                                                                .addGap(0, 0, Short.MAX_VALUE)))))));

        reviewPanel.setVisible(false);
        reviewPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(
                javax.swing.BorderFactory.createLineBorder(java.awt.Color.red, 4),
                javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2)));
        reviewPanel.setPreferredSize(new java.awt.Dimension(800, 600));

        jTable1.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.black, null));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {}, new String[] { "Review Date",
                "Stars", "Review Text", "User ID", "Useful Votes", "Cool Votes", "Funny Votes" }));
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTable1.setPreferredSize(new java.awt.Dimension(800, 600));
        jScrollPane3.setViewportView(jTable1);
        jScrollPane3.setVisible(false);

        backButton.setBackground(new java.awt.Color(255, 255, 0));
        backButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        backButton.setText("BACK");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        displayPiechartButton.setBackground(new java.awt.Color(255, 255, 51));
        displayPiechartButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        displayPiechartButton.setText("DISPLAY PIECHART FOR REVIEW STAR");
        displayPiechartButton.setToolTipText("Please select this to display piechart of review stars.");
        displayPiechartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                displayPiechartButtonActionPerformed(evt);
            }
        });

        displayBarchartButton.setBackground(new java.awt.Color(255, 255, 51));
        displayBarchartButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        displayBarchartButton.setText("BARGRAPH TOP 5 USEFUL REVIEWS");
        displayBarchartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                displayBarchartButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout reviewPanelLayout = new javax.swing.GroupLayout(reviewPanel);
        reviewPanel.setLayout(reviewPanelLayout);
        reviewPanelLayout.setHorizontalGroup(reviewPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(reviewPanelLayout.createSequentialGroup()
                        .addGroup(reviewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 703,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(reviewPanelLayout.createSequentialGroup()
                                        .addComponent(displayPiechartButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                269, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(displayBarchartButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(backButton)))
                        .addGap(20, 20, 20)));
        reviewPanelLayout.setVerticalGroup(reviewPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(reviewPanelLayout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 392,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(reviewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(displayBarchartButton, javax.swing.GroupLayout.Alignment.TRAILING,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 42,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(reviewPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(displayPiechartButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(20, 20, 20)));

        startButton.setBackground(new java.awt.Color(255, 255, 51));
        startButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        startButton.setForeground(new java.awt.Color(51, 51, 255));
        startButton.setText("START");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addGap(10, 10, 10).addComponent(
                                                topPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 900,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup().addGap(61, 61, 61).addComponent(
                                                startButton, javax.swing.GroupLayout.PREFERRED_SIZE, 686,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap())
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addContainerGap(118, Short.MAX_VALUE)
                                        .addComponent(reviewPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 726,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addContainerGap(156, Short.MAX_VALUE))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(topPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 616,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(21, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        layout.createSequentialGroup().addContainerGap(42, Short.MAX_VALUE)
                                .addComponent(reviewPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(41, Short.MAX_VALUE))));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        // TODO add your handling code here
        try {
            displayMainCategory();
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
        }
        topPanel.setVisible(true);
    }//GEN-LAST:event_startButtonActionPerformed

    public static void deleteAllRows(final DefaultTableModel model) {
        for (int i = model.getRowCount() - 1; i >= 0; i--) {
            model.removeRow(i);
        }
    }

    private void initComboBoxes() {
        Connection con = null;
        ResultSet result;
        try {
            con = openConnection();
            result = queryForStates(con);
            if (result != null) {
                displayStates(result);
                stateComboBox.setSelectedIndex(-1);
            }
            result = queryCity(con);
            if (result != null) {
                displayCity(result);
                cityComboBox.setSelectedIndex(-1);
            }
            result = queryZip(con);
            if (result != null) {
                displayZip(result);
                zipComboBox.setSelectedIndex(-1);
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.err.println(e.getMessage());
        } finally {
            closeConnection(con);
        }
    }

    private ResultSet queryForStates(Connection con) throws SQLException {
        Statement stmt = con.createStatement();
        return stmt.executeQuery("SELECT DISTINCT state FROM yelp_business ORDER BY state");
    }

    private ResultSet queryCity(Connection con) throws SQLException {
        Statement stmt = con.createStatement();
        return stmt.executeQuery("SELECT DISTINCT city FROM yelp_business ORDER BY city");
    }

    private ResultSet queryZip(Connection con) throws SQLException {
        Statement stmt = con.createStatement();
        return stmt.executeQuery("SELECT DISTINCT zip FROM yelp_business ORDER BY zip");
    }

    private void displayStates(ResultSet res) throws SQLException {
        while (res.next()) {
            stateComboBox.addItem(res.getString(1));
        }
    }

    private void displayCity(ResultSet res) throws SQLException {
        while (res.next()) {
            cityComboBox.addItem(res.getString(1));
        }
    }

    private void displayZip(ResultSet res) throws SQLException {
        while (res.next()) {
            zipComboBox.addItem(res.getString(1));
        }
    }

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        // TODO add your handling code here:
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        if (model.getRowCount() > 0) {
            deleteAllRows(model);
        }
        selectedBid = null;
        reviewPanel.setVisible(false);
        topPanel.setVisible(true);
    }//GEN-LAST:event_backButtonActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        // TODO add your handling code here:
        setVisible(false); //you can't see me!
        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    private String checkAndReplace(String str) {
        if (str.contains("'")) {
            str = str.replace("'", "''");
        }
        if (str.contains("&")) {
            str = str.replace("&", "'||'&'||'");
        }
        return str;
    }

    private String queryConstruction(String query, String choice) {
        int size_selectMainCat = selectMainCat.size();
        int size_selectSubCat = selectSubCat.size();
        int size_selectAttrList = selectedAttrList.size();
        String str = null;
        query = "SELECT DISTINCT temp.business_id, temp.name, temp.stars, temp.state, temp.city, temp.zip"
                + " FROM ( "
                + " SELECT b.business_id, b.zip, b.name, b.city, b.state, b.stars, s.main_cat_name, s.sub_cat_name, h.day, h.open_time, h.close_time"
                + " FROM yelp_business b " + " INNER JOIN hours h " + " ON b.business_id = h.business_id "
                + " INNER JOIN sub_category s "
                + " ON s.business_id = h.business_id AND s.business_id = b.business_id) temp " + " WHERE ";
        if (size_selectMainCat > 0 && size_selectSubCat <= 0 && size_selectAttrList <= 0) {
            str = checkAndReplace(selectMainCat.get(0));
            query = query + "(temp.main_cat_name='" + str + "'";
            for (int i = 1; i < size_selectMainCat; i++) {
                str = checkAndReplace(selectMainCat.get(i));
                query = query + " OR temp.main_cat_name='" + str + "'";
            }
            query = query + ")";
        } else if (size_selectMainCat > 0 && size_selectSubCat > 0 && size_selectAttrList <= 0) {
            str = checkAndReplace(selectMainCat.get(0));
            query = query + "(temp.main_cat_name='" + str + "'";
            for (int i = 1; i < size_selectMainCat; i++) {
                str = checkAndReplace(selectMainCat.get(i));
                query = query + " OR temp.main_cat_name='" + str + "'";
            }
            str = checkAndReplace(selectSubCat.get(0));
            query = query + ") AND (temp.sub_cat_name='" + str + "'";
            for (int i = 1; i < size_selectSubCat; i++) {
                str = checkAndReplace(selectSubCat.get(i));
                query = query + " OR temp.sub_cat_name='" + str + "'";
            }
            query = query + ")";
        } else if (size_selectMainCat > 0 && size_selectSubCat > 0 && size_selectAttrList > 0) {
            str = checkAndReplace(selectedAttrList.get(0));
            query = "SELECT DISTINCT temp.business_id, temp.name, temp.stars, temp.state, temp.city, temp.zip"
                    + " FROM ("
                    + " SELECT b.business_id, b.zip, b.name, b.city, b.state, b.stars, s.main_cat_name, s.sub_cat_name, h.day, h.open_time, h.close_time"
                    + " FROM yelp_business b" + " INNER JOIN" + " (SELECT business_id" + " FROM attributes"
                    + " WHERE attr_name_val='" + str + "'";

            for (int i = 1; i < size_selectAttrList; i++) {
                str = checkAndReplace(selectedAttrList.get(i));
                query = query + choice + " SELECT business_id" + " FROM attributes" + " WHERE attr_name_val='" + str
                        + "'";
            }
            query = query + ") temp1";
            query = query + " ON b.business_id = temp1.business_id" + " INNER JOIN hours h"
                    + " ON h.business_id = temp1.business_id AND b.business_id = h.business_id"
                    + " INNER JOIN sub_category s"
                    + " ON s.business_id = h.business_id AND s.business_id = temp1.business_id AND s.business_id = b.business_id) temp";
            str = checkAndReplace(selectMainCat.get(0));
            query = query + " WHERE (temp.main_cat_name = '" + str + "'";
            for (int i = 1; i < size_selectMainCat; i++) {
                str = checkAndReplace(selectMainCat.get(i));
                query = query + " OR temp.main_cat_name='" + str + "'";
            }
            str = checkAndReplace(selectSubCat.get(0));
            query = query + ") AND (temp.sub_cat_name ='" + str + "'";
            for (int i = 1; i < size_selectSubCat; i++) {
                str = checkAndReplace(selectSubCat.get(i));
                query = query + " OR temp.sub_cat_name='" + str + "'";
            }
            query = query + ")";

        }
        return query;
    }

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        // TODO add your handling code here:
        clearBusinessTable();

        String query = "";
        String allQuery = "";
        String closeQuery = "";
        //"SELECT DISTINCT business_id, name, city, state, stars FROM yelp_business WHERE business_id IN (SELECT DISTINCT business_id FROM sub_category WHERE ";

        float open_time = 0, close_time = 0;
        String type = null, day = null, open = null, close = null, city = null, state = null, zip = null;
        int openORclose = 0;
        if (searchComboBox.getSelectedIndex() != -1) {
            type = (String) searchComboBox.getSelectedItem();
        }
        String choice;
        if (type != null && type.equals("All attributes")) {
            choice = " INTERSECT";
        } else {
            choice = " UNION";
        }
        query = queryConstruction(query, choice);

        if (openCloseComboBox.getSelectedIndex() != -1) {
            String option = (String) openCloseComboBox.getSelectedItem();
            if (option.equals("open")) {
                openORclose = 1;
            } else if (option.equals("close")) {
                openORclose = 2;
            }
        }

        if (cityComboBox.getSelectedIndex() != -1) {
            city = (String) cityComboBox.getSelectedItem();
            if (city != null) {
                query = query + " AND temp.city='" + city + "'";
            }
        }

        if (stateComboBox.getSelectedIndex() != -1) {
            state = (String) stateComboBox.getSelectedItem();
            if (state != null) {
                query = query + " AND temp.state='" + state + "'";
            }
        }

        if (zipComboBox.getSelectedIndex() != -1) {
            zip = (String) zipComboBox.getSelectedItem();
            if (zip != null) {
                query = query + " AND temp.zip='" + zip + "'";
            }
        }

        allQuery = query;

        if (fromComboBox.getSelectedIndex() != -1) {
            open = (String) fromComboBox.getSelectedItem();
        }

        if (open != null && open.contains(":")) {
            open = open.replace(":", ".");
            open_time = Float.parseFloat(open);
        }
        if (toComboBox.getSelectedIndex() != -1) {
            close = (String) toComboBox.getSelectedItem();
        }
        if (close != null && close.contains(":")) {
            close = close.replace(":", ".");
            close_time = Float.parseFloat(close);
        }

        if (dayComboBox.getSelectedIndex() != -1) {
            day = (String) dayComboBox.getSelectedItem();
            if (day != null) {
                query = query + " AND temp.day ='" + day + "'";
            }
        }

        if (open_time != 0 && close_time != 0) {
            query = query + " AND temp.open_time BETWEEN " + open_time + " AND " + close_time;
        }

        if (openORclose == 2) {
            closeQuery = allQuery + " MINUS " + query;
            query = closeQuery;
        }
        /*if(day != null && type != null && city != null && state != null && zip != null && close_time != 0 && open_time != 0)
         System.out.println(day + " "+ type + " " + state + " "+ city + " "+zip + " " + open_time + " "+close_time);
         */
        //displayAll();
        try {
            //System.out.println(query);
            BusinessConnection(query);
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }

    }//GEN-LAST:event_searchButtonActionPerformed

    /* private void displayAll() {
     printArrayList(selectMainCat);
     printArrayList(selectSubCat);
     printArrayList(selectedAttrList);
     }
        
     private void printArrayList(ArrayList<String> list) {
     for (String item : list) {
     System.out.print(item + " ");
     }
     System.out.println();
     }*/

    private void dayComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dayComboBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_dayComboBoxActionPerformed

    private void jTable2MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MousePressed
        // TODO add your handling code here:
        int selectedRowIndex = jTable2.getSelectedRow();
        selectedBid = (Object) jTable2.getModel().getValueAt(selectedRowIndex, 4);
        getReviews((String) selectedBid);
    }//GEN-LAST:event_jTable2MousePressed

    private void piechartValues(int[] val) {
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        int rowCount = model.getRowCount();
        String temp;
        for (int i = 0; i < rowCount; i++) {
            temp = (String) model.getValueAt(i, 1);
            switch (temp) {
            case "1":
                val[0] += 1;
                break;
            case "2":
                val[1] += 1;
                break;
            case "3":
                val[2] += 2;
                break;
            case "4":
                val[3] += 1;
                break;
            case "5":
                val[4] += 1;
                break;
            }
        }
    }

    private void displayPiechartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_displayPiechartButtonActionPerformed
        // TODO add your handling code here:
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        int val[] = new int[5];
        piechartValues(val);
        if (val[0] > 0) {
            pieDataset.setValue("1 Star=" + val[0], new Integer(val[0]));
        }
        if (val[1] > 0) {
            pieDataset.setValue("2 Star=" + val[1], new Integer(val[1]));
        }
        if (val[2] > 0) {
            pieDataset.setValue("3 Star=" + val[2], new Integer(val[2]));
        }
        if (val[3] > 0) {
            pieDataset.setValue("4 Star=" + val[3], new Integer(val[3]));
        }
        if (val[4] > 0) {
            pieDataset.setValue("5 Star=" + val[4], new Integer(val[4]));
        }
        int total = val[0] + val[1] + val[2] + val[3] + val[4];
        JFreeChart chart = ChartFactory.createPieChart("Piechart\n Total Reviews : " + total, pieDataset, true,
                true, true);
        PiePlot P = (PiePlot) chart.getPlot();
        ChartFrame frame = new ChartFrame("Pie Chart", chart);
        frame.setVisible(true);
        frame.setSize(500, 500);
    }//GEN-LAST:event_displayPiechartButtonActionPerformed

    private void displayBarchartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_displayBarchartButtonActionPerformed
        // TODO add your handling code here:

        Connection con = null;
        ResultSet result;
        try {
            con = openConnection();
            result = queryForBarChart(con);
            if (result != null) {
                drawBarChart(result);
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.err.println(e.getMessage());
        } finally {
            closeConnection(con);
        }
    }//GEN-LAST:event_displayBarchartButtonActionPerformed

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        // TODO add your handling code here:
        dayComboBox.setSelectedIndex(-1);
        fromComboBox.setSelectedIndex(-1);
        toComboBox.setSelectedIndex(-1);
        searchComboBox.setSelectedIndex(-1);
        cityComboBox.setSelectedIndex(-1);
        stateComboBox.setSelectedIndex(-1);
        zipComboBox.setSelectedIndex(-1);
        openCloseComboBox.setSelectedIndex(-1);
    }//GEN-LAST:event_clearButtonActionPerformed

    private ResultSet queryForBarChart(Connection con) throws SQLException {
        Statement stmt = con.createStatement();
        return stmt.executeQuery(
                "SELECT * FROM ( SELECT r.useful, u.name FROM yelp_user u, yelp_review r WHERE r.user_id = u.user_id AND r.business_id ='"
                        + selectedBid + "' ORDER BY r.useful DESC) WHERE ROWNUM <= 5");
    }

    private void drawBarChart(ResultSet result) throws SQLException {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        while (result.next()) {
            dataset.setValue(result.getInt(1), "Reviews", result.getString(2));
        }
        JFreeChart chart = ChartFactory.createBarChart("TOP 5 Useful Votes Reviews", "USER", "USEFUL REVIEWS",
                dataset, PlotOrientation.VERTICAL, false, true, false);
        CategoryPlot p = chart.getCategoryPlot();
        p.setRangeGridlinePaint(Color.BLACK);
        ChartFrame frame = new ChartFrame("Bar Chart", chart);
        frame.setVisible(true);
        frame.setSize(450, 350);
    }

    private void getReviews(String bid) {
        Connection con = null;
        ResultSet result;
        try {
            con = openConnection();
            result = queryReviews(con, bid);
            if (result != null) {
                displayReviewTable(result);
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.err.println(e.getMessage());
        } finally {
            closeConnection(con);
        }
    }

    private void displayReviewTable(ResultSet result) throws SQLException {
        TableColumnModel tableModel = jTable1.getColumnModel();
        tableModel.getColumn(2).setPreferredWidth(300);
        tableModel.getColumn(3).setPreferredWidth(200);
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();

        while (result.next()) {
            String date = result.getDate(7) + "";
            String stars = result.getInt(5) + "";
            String text = result.getString(6);
            String user = result.getString(4);
            String useful = result.getInt(2) + "";
            String cool = result.getInt(3) + "";
            String funny = result.getInt(1) + "";
            model.addRow(new Object[] { date, stars, text, user, useful, cool, funny });
            //System.out.println(date + " "+ stars + " "+ text + " "+ user + " "+ useful + " " + cool + " "+ funny);
        }
        topPanel.setVisible(false);
        //reviewPanel.repaint();
        jScrollPane3.setVisible(true);
        jTable1.setVisible(true);
        reviewPanel.setVisible(true);
        if (jTable1.getRowCount() == 0) {
            JOptionPane.showMessageDialog(reviewPanel, "NO Reviews tO DISPLAY", "Message",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private ResultSet queryReviews(Connection con, String bid) throws SQLException {
        Statement stmt = con.createStatement();
        return stmt.executeQuery(
                "SELECT r.funny, r.useful, r.cool, u.name ,r.stars, r.review_text, r.review_date FROM yelp_review r, yelp_user u WHERE r.user_id = u.user_id AND business_id='"
                        + bid + "' ORDER BY review_date");
    }

    private void displayBusinessTable(ResultSet result) throws SQLException {
        TableColumnModel tableModel = jTable2.getColumnModel();
        tableModel.getColumn(0).setPreferredWidth(200);
        tableModel.getColumn(4).setPreferredWidth(200);
        DefaultTableModel model = (DefaultTableModel) jTable2.getModel();

        while (result.next()) {
            String bid = result.getString(1);
            String bname = result.getString(2);
            String bcity = result.getString(5);
            String bstate = result.getString(4);
            String bstars = result.getFloat(3) + "";
            String bzip = result.getString(6);
            model.addRow(new Object[] { bname, bcity, bstate, bstars, bid, bzip });
            //System.out.println(bid + "  " + bname + bcity + "  " + bstate + "  " + bstars);
        }
        jTable2.repaint();
        if (jTable2.getRowCount() == 0) {
            JOptionPane.showMessageDialog(topPanel, "NO DATA TO DISPLAY", "Message",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private void BusinessConnection(String query) throws SQLException {
        Connection con = null;
        ResultSet result;
        //System.out.println(query);
        try {
            con = openConnection();
            result = queryForBusinessList(con, query);
            if (result != null) {
                displayBusinessTable(result);
            }
        } catch (SQLException | ClassNotFoundException e) {
            System.err.println("Error while querying (4tables) database " + e.getMessage());
        } finally {
            closeConnection(con);
        }
    }

    private ResultSet queryForBusinessList(Connection con, String query) throws SQLException {
        Statement stmt = con.createStatement();
        //System.out.println(query);
        return stmt.executeQuery(query + "ORDER BY state, city");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(HW3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(HW3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(HW3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HW3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HW3().setVisible(true);
            }
        });
    }

    private ResultSet searchAllTuples(Connection con) throws SQLException {
        Statement stmt = con.createStatement();
        return stmt.executeQuery("SELECT DISTINCT cat_name FROM category ORDER BY cat_name");
    }

    private ResultSet searchSubCats(Connection con, String catName) throws SQLException {
        Statement stmt = con.createStatement();
        return stmt.executeQuery(
                "SELECT DISTINCT sub_cat_name FROM sub_category WHERE main_cat_name ='" + catName + "'");
    }

    private void displayMainCategory() throws SQLException {
        Connection con = null;
        ResultSet result;
        try {
            con = openConnection();
            result = searchAllTuples(con);
            showMainCategory(result);
        } catch (SQLException | ClassNotFoundException e) {
            System.err.println(e.getMessage());
        } finally {
            closeConnection(con);
        }
    }

    private ResultSet searchAttr(Connection con, String subCatName) throws SQLException {
        Statement stmt = con.createStatement();
        if (subCatName.contains("'")) {
            subCatName = subCatName.replace("'", "''");
        }
        if (subCatName.contains("&")) {
            subCatName = subCatName.replace("&", "'||'&'||'");
        }
        return stmt.executeQuery(
                "SELECT DISTINCT attr_name_val FROM attributes WHERE business_id IN (SELECT business_id FROM sub_category WHERE sub_cat_name='"
                        + subCatName + "')");
    }

    private void showAttributes(ResultSet result, HashMap<JCheckBox, String> returnList, String subCatName)
            throws SQLException {
        String attr_name_val, attr_value;
        while (result.next()) {
            attr_name_val = result.getString(1);
            // attr_value = result.getString(2);
            JCheckBox check;
            //if (!attr_value.equals("T")) {
            check = new JCheckBox(attr_name_val, false);
            //} else {
            //    check = new JCheckBox(attr_name, false);
            // }
            returnList.put(check, subCatName);
        }
    }

    private void displayAttr(String subCatName, HashMap<JCheckBox, String> returnList) {
        Connection con = null;
        ResultSet res;
        try {
            con = openConnection();
            res = searchAttr(con, subCatName);
            showAttributes(res, returnList, subCatName);
        } catch (SQLException | ClassNotFoundException e) {
            System.err.println(e.getMessage());
        } finally {
            closeConnection(con);
        }
    }

    private void showSubCat(String catName, HashMap<JCheckBox, String> list) {
        Connection connect = null;
        ResultSet res;
        try {
            connect = openConnection();
            res = searchSubCats(connect, catName);
            String name;
            while (res.next()) {
                name = res.getString(1);
                JCheckBox check = new JCheckBox(name, false);
                list.put(check, catName);
            }
        } catch (SQLException | ClassNotFoundException ex) {
            System.err.println(ex.getMessage());
        } finally {
            closeConnection(connect);
        }
    }

    private void showMainCategory(ResultSet result) throws SQLException {
        int i = 1;
        while (result.next()) {
            String catName = result.getString(1);
            JCheckBox check = new JCheckBox(catName, false);
            mainCategoryPanel.add(check);
            check.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    HashMap<JCheckBox, String> returnList = new HashMap<JCheckBox, String>();
                    if (check.isSelected()) {
                        selectMainCat.add(catName);
                        showSubCat(catName, returnList);
                        mapList[mapListCount] = returnList;
                        finalDisplaySubCategory(catName);
                    } else {
                        clearBusinessTable();
                        selectMainCat.remove(catName);
                        finalRemoveSubCategory(catName);
                    }
                }
            });
            i = i + 1;
        }
        mainCategoryPanel.setLayout(new GridLayout(i, 1));
        mainCategoryPanel.repaint();
        setFocusable(true);
        pack();
    }

    private void finalRemoveSubCategory(String catName) {
        for (HashMap map : mapList) {
            if (map != null) {
                Iterator it = map.keySet().iterator();
                while (it.hasNext()) {
                    JCheckBox jcheck = (JCheckBox) it.next();
                    //System.out.println(jcheck);
                    String s = jcheck.getText();
                    if (map.get(jcheck).equals(catName)) {
                        if (jcheck.isSelected()) {
                            finalAttributeRemove(s);
                        }
                        int index = subCatNames.indexOf(s);
                        if (index != -1) {
                            subCatNames.remove(index);
                        }
                        int id = selectSubCat.indexOf(s);
                        if (id != -1) {
                            selectSubCat.remove(id);
                        }
                        subCategoryPanel.remove(jcheck);
                    }
                }
                map = null;
            }
        }
        subCategoryPanel.revalidate();
        subCategoryPanel.repaint();
    }

    private void clearBusinessTable() {
        DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
        if (model.getRowCount() > 0) {
            deleteAllRows(model);
        }
    }

    private void finalDisplaySubCategory(String catName) {
        for (HashMap map : mapList) {
            if (map != null) {
                Iterator it = map.keySet().iterator();
                while (it.hasNext()) {
                    JCheckBox jcheck = (JCheckBox) it.next();
                    String s = jcheck.getText();
                    boolean flag = checkAlredyExists(s, subCatNames);
                    if (map.get(jcheck).equals(catName) && flag == false) {
                        subCatNames.add(s);
                        jcheck.setSelected(false);
                        subCategoryPanel.add(jcheck);
                        jcheck.addItemListener(new ItemListener() {
                            @Override
                            public void itemStateChanged(ItemEvent e) {
                                String subCatName = null;
                                HashMap<JCheckBox, String> returnList = new HashMap<JCheckBox, String>();
                                if (jcheck.isSelected()) {
                                    subCatName = jcheck.getText();
                                    //System.out.println(subCatName);
                                    selectSubCat.add(subCatName);
                                    displayAttr(subCatName, returnList);
                                    AttrList[attrListCount] = returnList;
                                    finalAttributeDisplay(subCatName);
                                } else {
                                    subCatName = jcheck.getText();
                                    selectSubCat.remove(subCatName);
                                    clearBusinessTable();
                                    finalAttributeRemove(subCatName);
                                }
                            }
                        });
                    }
                }
            }
        }
        subCategoryPanel.setLayout(new GridLayout(0, 1));
        subCategoryPanel.revalidate();
        subCategoryPanel.repaint();
        pack();
        subCategoryPanel.setVisible(true);
        if (mapListCount == MAX_MAP_LIST_COUNT - 1) {
            mapListCount = 0;
        } else {
            mapListCount++;
        }
    }

    private void finalAttributeRemove(String subCatName) {
        for (HashMap hmap : AttrList) {
            if (hmap != null) {
                Iterator iter = hmap.keySet().iterator();
                while (iter.hasNext()) {
                    JCheckBox jcheck = (JCheckBox) iter.next();
                    if (hmap.get(jcheck).equals(subCatName)) {
                        //System.out.println("$$$ " +jcheck.getText());
                        String s = jcheck.getText();
                        int index = attrNames.indexOf(s);
                        if (index != -1) {
                            attrNames.remove(index);
                        }
                        index = selectedAttrList.indexOf(s);
                        if (index != -1) {
                            selectedAttrList.remove(index);
                        }
                        attrPanel.remove(jcheck);
                    }
                }
                //hmap.clear();
                hmap = null;
            }
        }
        attrPanel.revalidate();
        attrPanel.repaint();
    }

    private boolean checkAlredyExists(String name, ArrayList<String> list) {
        if (list.size() > 0) {
            for (String item : list) {
                if (name.equals(item)) {
                    return true;
                }
            }
        }
        return false;
    }

    private void finalAttributeDisplay(String subCatName) {
        for (HashMap hmap : AttrList) {
            if (hmap != null) {
                Iterator iter = hmap.keySet().iterator();
                boolean flag;
                while (iter.hasNext()) {
                    JCheckBox jcheck = (JCheckBox) iter.next();
                    String attr = jcheck.getText();
                    flag = checkAlredyExists(attr, attrNames);
                    if (hmap.get(jcheck) == subCatName && flag == false) {
                        attrNames.add(attr);
                        jcheck.setSelected(false);
                        attrPanel.add(jcheck);
                        jcheck.addItemListener(new ItemListener() {

                            @Override
                            public void itemStateChanged(ItemEvent e) {
                                String str = jcheck.getText();
                                if (jcheck.isSelected()) {
                                    if (!checkAlredyExists(str, selectedAttrList)) {
                                        selectedAttrList.add(str);
                                    }
                                } else {
                                    clearBusinessTable();
                                    selectedAttrList.remove(str);
                                }
                            }
                        });
                    }
                }
            }
        }
        attrPanel.setLayout(new GridLayout(0, 1));
        attrPanel.revalidate();
        attrPanel.repaint();
        pack();
        attrPanel.setVisible(true);
        if (attrListCount == MAX_ATTR_LIST_COUNT - 1) {
            attrListCount = 0;
        } else {
            attrListCount++;
        }
    }

    private Connection openConnection() throws SQLException, ClassNotFoundException {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        String userName = "Scott";
        String dbURL = "jdbc:oracle:thin:@localhost:1521:orcl";
        String pass = "Najuka123";
        return DriverManager.getConnection(dbURL, userName, pass);
    }

    private void closeConnection(Connection con) {
        try {
            con.close();
        } catch (SQLException e) {
            System.err.println("Cannot close connection: " + e.getMessage());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel attrPanel;
    private javax.swing.JButton backButton;
    private javax.swing.JComboBox cityComboBox;
    private javax.swing.JLabel cityLabel;
    private javax.swing.JButton clearButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JComboBox dayComboBox;
    private javax.swing.JLabel dayLabel;
    private javax.swing.JButton displayBarchartButton;
    private javax.swing.JButton displayPiechartButton;
    private javax.swing.JComboBox fromComboBox;
    private javax.swing.JLabel fromLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JPanel mainCategoryPanel;
    private javax.swing.JComboBox openCloseComboBox;
    private javax.swing.JLabel openCloseLabel;
    private javax.swing.JPanel reviewPanel;
    private javax.swing.JButton searchButton;
    private javax.swing.JComboBox searchComboBox;
    private javax.swing.JLabel searchLabel;
    private javax.swing.JButton startButton;
    private javax.swing.JComboBox stateComboBox;
    private javax.swing.JLabel stateLabel;
    private javax.swing.JPanel subCategoryPanel;
    private javax.swing.JComboBox toComboBox;
    private javax.swing.JLabel toLabel;
    private javax.swing.JPanel topPanel;
    private javax.swing.JComboBox zipComboBox;
    private javax.swing.JLabel zipLabel;
    // End of variables declaration//GEN-END:variables
}