import java.util.Calendar;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;

public class JdbcOrderDialog extends OrderDialog {

    public JdbcOrderDialog(JFrame owner) {
        super(owner);
    }

    protected ActionListener makeButtonHandler() {
        return new JdbcButtonHandler();
    }

    class JdbcButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            JButton button = (JButton) evt.getSource();
            String label = button.getText();
            if ("Ok".equals(label)) {
                try {
                   insertNewOrder();
                } catch( SQLException IOex ) {}
                  catch( IOException IOex ) {}
                dialogPanel.reset();
                setVisible(false);
            }
        }

        public void insertNewOrder() throws SQLException, IOException {
            System.out.print( "Loading JDBC OCI driver...\n\n\n" );
            try {
                Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
            } catch (ClassNotFoundException e) {
                System.exit( 1 );
            }
            String url = "jdbc:odbc:OrdersDriver";
            Connection conn = DriverManager.getConnection(url,"rpasenko","mpf8eub");
            Statement stmt = conn.createStatement();
            String creditCard = null;
            if (dialogPanel.visaBox.isSelected()) {
                creditCard = "Visa";
            } else if (dialogPanel.mcBox.isSelected()) {
                creditCard = "MasterCard";
            } else if ( dialogPanel.discoverBox.isSelected() ) {
                creditCard = "Discover";
            }
            Calendar cal = Calendar.getInstance();
            String orderNo = String.valueOf(cal.get(cal.MONTH)+1) +
                                            String.valueOf(cal.get(cal.DAY_OF_MONTH)) +
                                            String.valueOf(cal.get(cal.YEAR)) +
                                            String.valueOf(cal.get(cal.HOUR)) +
                                            String.valueOf(cal.get(cal.MINUTE)) +
                                            String.valueOf(cal.get(cal.SECOND));
            String insertString;
            insertString = "INSERT INTO Beetles VALUES( " +
                                        "'"   +   dialogPanel.nameField.getText()     +    "',"  +
                                        "'"   +   dialogPanel.addressField.getText()  +    "',"  +
                                        "'"   +   dialogPanel.cityField.getText()     +    "',"  +
                                        "'"   +   dialogPanel.stateField.getText()    +    "',"  +
                                        "'"   +   dialogPanel.zipField.getText()      +    "',"  +
                                        "'"   +   dialogPanel.emailField.getText()    +    "',"  +
                                        "'"   +   creditCard                          +    "',"  +
                                        "'"   +   orderNo                             +    "')";
            stmt.executeUpdate(insertString);
            stmt.close();
            conn.commit();
            conn.close();
        }
    }
}