/*
 * 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.
 */
package buoi3;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author student
 */
public class DVDCatalogForm extends javax.swing.JFrame {
    static String dbUrl = "jdbc:sqlserver://localhost:1433;databaseName=DVDLibrary;user=sa;password=P@ssword";
    private String action = new String();
    /**
     * Creates new form DVDCatalogForm
     */
    public DVDCatalogForm() {
        initComponents();
        this.cboCodeNo.setVisible(false);
    }

    /**
     * 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() {

        grpSubtitles = new javax.swing.ButtonGroup();
        optConfirm = new javax.swing.JOptionPane();
        lblDVDCodeNo = new javax.swing.JLabel();
        lblHeading = new javax.swing.JLabel();
        lblTitle = new javax.swing.JLabel();
        lblLang = new javax.swing.JLabel();
        lblPrice = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        cboLang = new javax.swing.JComboBox<>();
        updPrice = new javax.swing.JSpinner();
        lblDollars = new javax.swing.JLabel();
        paneSubtitles = new javax.swing.JPanel();
        radYes = new javax.swing.JRadioButton();
        radNo = new javax.swing.JRadioButton();
        paneButtons = new javax.swing.JPanel();
        btnAddNew = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        cboCodeNo = new javax.swing.JComboBox<>();
        txtCodeNo = new javax.swing.JTextField();

        optConfirm.setName(""); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("DVD Catalog");
        setResizable(false);
        setSize(new java.awt.Dimension(800, 600));

        lblDVDCodeNo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblDVDCodeNo.setText("DVD Code Number");

        lblHeading.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lblHeading.setText("DVD Catalog");

        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblTitle.setText("DVD Title");

        lblLang.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblLang.setText("Language");

        lblPrice.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblPrice.setText("Price");

        txtTitle.setEnabled(false);

        cboLang.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "English", "Vietnamese" }));
        cboLang.setEnabled(false);

        updPrice.setEnabled(false);

        lblDollars.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblDollars.setText("Dollars");

        paneSubtitles.setBorder(javax.swing.BorderFactory.createTitledBorder("Subtitles"));
        paneSubtitles.setEnabled(false);

        grpSubtitles.add(radYes);
        radYes.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        radYes.setText("Yes");
        radYes.setEnabled(false);

        grpSubtitles.add(radNo);
        radNo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        radNo.setText("No");
        radNo.setEnabled(false);

        javax.swing.GroupLayout paneSubtitlesLayout = new javax.swing.GroupLayout(paneSubtitles);
        paneSubtitles.setLayout(paneSubtitlesLayout);
        paneSubtitlesLayout.setHorizontalGroup(
            paneSubtitlesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneSubtitlesLayout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(radYes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addComponent(radNo)
                .addGap(30, 30, 30))
        );
        paneSubtitlesLayout.setVerticalGroup(
            paneSubtitlesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneSubtitlesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(paneSubtitlesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radYes)
                    .addComponent(radNo))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        btnAddNew.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnAddNew.setText("<html> Add<br>New</html>");
        btnAddNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddNewActionPerformed(evt);
            }
        });

        btnUpdate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnSave.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnSave.setText("Save");
        btnSave.setEnabled(false);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.setEnabled(false);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnExit.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout paneButtonsLayout = new javax.swing.GroupLayout(paneButtons);
        paneButtons.setLayout(paneButtonsLayout);
        paneButtonsLayout.setHorizontalGroup(
            paneButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneButtonsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAddNew, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExit)
                .addContainerGap())
        );
        paneButtonsLayout.setVerticalGroup(
            paneButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneButtonsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(paneButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(paneButtonsLayout.createSequentialGroup()
                        .addComponent(btnAddNew, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(btnExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        cboCodeNo.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                cboCodeNoComponentHidden(evt);
            }
            public void componentShown(java.awt.event.ComponentEvent evt) {
                cboCodeNoComponentShown(evt);
            }
        });
        cboCodeNo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboCodeNoActionPerformed(evt);
            }
        });
        jPanel1.add(cboCodeNo, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 10, 160, 30));

        txtCodeNo.setEditable(false);
        txtCodeNo.setEnabled(false);
        jPanel1.add(txtCodeNo, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 20, 154, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(paneButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblTitle)
                                    .addComponent(lblLang)
                                    .addComponent(lblDVDCodeNo))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(updPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(lblDollars))
                                        .addComponent(cboLang, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblPrice)
                                .addGap(64, 64, 64)
                                .addComponent(paneSubtitles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(143, 143, 143)
                        .addComponent(lblHeading)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblHeading)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDVDCodeNo, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTitle))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboLang, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblLang))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(updPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPrice)
                    .addComponent(lblDollars))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(paneSubtitles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(paneButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

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

    private void btnAddNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddNewActionPerformed
        // TODO add your handling code here:
        action = "INSERT";
        toggleInAction(true);
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            try (Connection con = DriverManager.getConnection(dbUrl)) {
                Statement s = con.createStatement();
                ResultSet rs = s.executeQuery("SELECT MAX(DVDCodeNo) FROM DVD");
                rs.next();
                txtCodeNo.setText(Integer.toString(rs.getInt(1) + 1));
            }
        }   catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(DVDCatalogForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnAddNewActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        cboCodeNo.setVisible(true);
        action = "UPDATE";
        toggleInAction(true);
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        cboCodeNo.setVisible(true);
        action = "DELETE";
        toggleInAction(true);
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        String query = new String();
        if (action.equals("INSERT")) {
            query = String.format("INSERT INTO DVD VALUES(%s,'%s','%s',%d,%s)", 
                                    txtCodeNo.getText(),
                                    txtTitle.getText(),
                                    cboLang.getSelectedItem(),
                                    (radYes.isSelected() ? 1 : 0),
                                    updPrice.getValue());
            System.out.println(query);
        }
        else if (action.equals("UPDATE")) {
            query = String.format("UPDATE DVD SET DVDTitle = '%s', "
                                    + "DVDLang = '%s', "
                                    + "DVDSubTitles = %d, "
                                    + "DVDPrice = %s WHERE DVDCodeNo = %s",
                                    txtTitle.getText(),
                                    cboLang.getSelectedItem(),
                                    (radYes.isSelected() ? 1 : 0),
                                    updPrice.getValue(),
                                    cboCodeNo.getSelectedItem());
        }
        else if (action.equals("DELETE")) {
            int response = optConfirm.showConfirmDialog(
              this, 
              "Are you sure you want to delete this?",
              "Confirmation", 
              javax.swing.JOptionPane.YES_NO_OPTION);
            
            if (response == javax.swing.JOptionPane.YES_NO_OPTION) {
                query = String.format("DELETE from DVD WHERE DVDCodeNo = %s", cboCodeNo.getSelectedItem());
            }
            else return;
        }

        try {    
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            try (Connection con = DriverManager.getConnection(dbUrl)) {
                Statement s = con.createStatement();
                int rows = s.executeUpdate(query);
                if (rows < 0) {
                    System.out.println("Execution failed.");
                }
                else System.out.println(rows + " rows affected.");
            }
        }   catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(DVDCatalogForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        resetForm();
    }//GEN-LAST:event_btnSaveActionPerformed

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

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btnExitActionPerformed

    private void cboCodeNoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboCodeNoActionPerformed
        // TODO add your handling code here:
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            try (Connection con = DriverManager.getConnection(dbUrl)) {
                Statement s = con.createStatement();
                ResultSet rs = s.executeQuery("SELECT * FROM DVD WHERE DVDCodeNo=" + cboCodeNo.getSelectedItem());
                while (rs.next()) {
                    txtTitle.setText(rs.getString("DVDTitle"));
                    cboLang.setSelectedItem(rs.getString("DVDLang"));
                    if (rs.getBoolean("DVDSubtitles")) {
                        grpSubtitles.setSelected(radYes.getModel(), true);
                    }
                    else grpSubtitles.setSelected(radNo.getModel(), true);
                    updPrice.setValue(rs.getInt("DVDPrice"));
                }
            }
        }   catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(DVDCatalogForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_cboCodeNoActionPerformed

    private void cboCodeNoComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_cboCodeNoComponentShown
        // TODO add your handling code here:
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            try (Connection con = DriverManager.getConnection(dbUrl)) {
                Statement s = con.createStatement();
                ResultSet rs = s.executeQuery("SELECT DVDCodeNo FROM DVD ORDER BY DVDCodeNo");
                while (rs.next()) {
                    cboCodeNo.addItem(rs.getString("DVDCodeNo"));
                }
            }
        }   catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(DVDCatalogForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_cboCodeNoComponentShown

    private void cboCodeNoComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_cboCodeNoComponentHidden
        // TODO add your handling code here:
        cboCodeNo.removeAllItems();
    }//GEN-LAST:event_cboCodeNoComponentHidden

    private void toggleInAction(boolean state) {
        btnAddNew.setEnabled(!state);
        btnUpdate.setEnabled(!state);
        btnDelete.setEnabled(!state);
        
        if (!action.equals("DELETE")) {
            txtCodeNo.setEnabled(state);
            txtTitle.setEnabled(state);
            cboLang.setEnabled(state);
            updPrice.setEnabled(state);
            radYes.setEnabled(state);
            radNo.setEnabled(state);
        }
        
        btnSave.setEnabled(state);
        btnCancel.setEnabled(state);
    }
    
    private void resetForm() {
        cboCodeNo.setVisible(false);
        txtCodeNo.setText("");
        txtTitle.setText("");
        toggleInAction(false);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DVDCatalogForm().setVisible(true);
            }
        });
        
        try {    
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            try (Connection con = DriverManager.getConnection(dbUrl)) {
                Statement s = con.createStatement();
                ResultSet rs = s.executeQuery("SELECT * FROM DVD");
                ResultSetMetaData rsmd = rs.getMetaData();
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    String name = rsmd.getColumnName(i);
                    int width = Integer.max(name.length(), rsmd.getPrecision(i));
                    System.out.format("%-" + width + "s\t", name);
                }
                System.out.println();
                while(rs.next()) {
                    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                        int width = Integer.max(rsmd.getColumnName(i).length(), rsmd.getPrecision(i));
                        System.out.format("%-" + width + "s\t", rs.getString(i));
                    }
                    System.out.println();
                }
                System.out.println();
            }
        }   catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(DVDCatalogForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddNew;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox<String> cboCodeNo;
    private javax.swing.JComboBox<String> cboLang;
    private javax.swing.ButtonGroup grpSubtitles;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblDVDCodeNo;
    private javax.swing.JLabel lblDollars;
    private javax.swing.JLabel lblHeading;
    private javax.swing.JLabel lblLang;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JOptionPane optConfirm;
    private javax.swing.JPanel paneButtons;
    private javax.swing.JPanel paneSubtitles;
    private javax.swing.JRadioButton radNo;
    private javax.swing.JRadioButton radYes;
    private javax.swing.JTextField txtCodeNo;
    private javax.swing.JTextField txtTitle;
    private javax.swing.JSpinner updPrice;
    // End of variables declaration//GEN-END:variables
}
