import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.table.TableColumn; import net.proteanit.sql.DbUtils;  /*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */  /**  *  * @author Anam  */ public class holidayAdmin extends JPanel implements ActionListener {         //JPanel holidaysPanel = new JPanel();          JPanel holidaysPanel = new JPanel();     JPanel panel = new JPanel();          JPanel panel2 = new JPanel();     JPanel panel3 = new JPanel();     JPanel panel4 = new JPanel();     JPanel panel5 = new JPanel();     //these are the userFrame panels     JPanel panel11 = new JPanel();     JPanel panel12 = new JPanel();     JPanel panel13 = new JPanel();     private JScrollPane userjsp;     // This shows the positioning of the label and textField together group     // This creates the labels (text)     JLabel txtCountry = new JLabel("Country");     JTextField country = new JTextField(10);     JLabel txtRegion = new JLabel("Region");     JTextField region = new JTextField(10);     JLabel txtResort = new JLabel("Resort");     JTextField resort = new JTextField(10);     JLabel txtDuration = new JLabel("Duration");     JTextField duration = new JTextField(10);     JLabel txtAccommodation = new JLabel("Accommodation");     JTextField accommodation = new JTextField(10);     JLabel txtBoardBases = new JLabel("Board-Bases");     JTextField boardBases = new JTextField(10);     JLabel txtHolidayType = new JLabel("Holiday Type");     JTextField holidayType = new JTextField(10);     JLabel txtHolidaySubtype = new JLabel("Holiday Subtype");     JTextField holidaySubtype = new JTextField(10);     JLabel txtRatings = new JLabel("Ratings");     JTextField ratings = new JTextField(15);     JLabel txtDate = new JLabel("Date");     JTextField date = new JTextField(10);     JLabel txtWeather = new JLabel("Weather");     JTextField weather = new JTextField(10);     JLabel txtPrice = new JLabel("Price");     JTextField price = new JTextField(10);     JLabel txtAdult = new JLabel("Adult");     JTextField adult = new JTextField(10);     JLabel txtChildren = new JLabel("Children");     JTextField children = new JTextField(10);     private JTable tableHolidays;          private JButton save;     private JButton deleteHolidays;          private JButton addHoliday;     ///Connection for database     Connection conn = null;     ResultSet rs = null;     PreparedStatement pst = null;          public holidayAdmin(){         ///setSize(2000,1000);         ///panel.setSize(200,300);         conn = javaconnect.ConnecrDb();                  save = new JButton(" Save ");         save.addActionListener(new saveListener());         addMouseListener(new MouseListnerHolidayTable());          addHoliday = new JButton(" Add ");          deleteHolidays = new JButton(" Delete ");         deleteHolidays.addActionListener(new deleteListener());                          ////co-ordinates for bounds          txtCountry.setBounds(10, 50, 10, 20);         country.setBounds(20, 50, 10, 20);         panel.add(txtCountry);         panel.add(country);          txtRegion.setBounds(10, 80, 10, 40);         region.setBounds(20, 80, 10, 40);         panel.add(txtRegion);         panel.add(region);          txtResort.setBounds(10, 100, 10, 40);         resort.setBounds(20, 100, 10, 40);         panel.add(txtResort);         panel.add(resort);          txtDuration.setBounds(10, 150, 10, 40);         duration.setBounds(20, 150, 10, 40);         panel.add(txtDuration);         panel.add(duration);          txtAccommodation.setBounds(10, 120, 10, 40);         accommodation.setBounds(20, 120, 10, 40);         panel.add(txtAccommodation);         panel.add(accommodation);          ////////////////////////         txtBoardBases.setBounds(10, 50, 10, 20);         boardBases.setBounds(20, 50, 10, 20);         panel2.add(txtBoardBases);         panel2.add(boardBases);          txtHolidayType.setBounds(10, 80, 10, 40);         holidayType.setBounds(20, 80, 10, 40);         panel2.add(txtHolidayType);         panel2.add(holidayType);          txtHolidaySubtype.setBounds(10, 100, 10, 40);         holidaySubtype.setBounds(20, 100, 10, 40);         panel2.add(txtHolidaySubtype);         panel2.add(holidaySubtype);          txtRatings.setBounds(10, 120, 10, 40);         ratings.setBounds(20, 120, 10, 40);         panel2.add(txtRatings);         panel2.add(ratings);          txtDate.setBounds(10, 150, 10, 40);         date.setBounds(20, 150, 10, 40);         panel2.add(txtDate);         panel2.add(date);         ////////////////////          txtWeather.setBounds(10, 50, 10, 20);         weather.setBounds(20, 50, 10, 20);         panel3.add(txtWeather);         panel3.add(weather);          txtPrice.setBounds(10, 80, 10, 40);         price.setBounds(20, 80, 10, 40);         panel3.add(txtPrice);         panel3.add(price);          txtAdult.setBounds(10, 100, 10, 40);         adult.setBounds(20, 100, 10, 40);         panel3.add(txtAdult);         panel3.add(adult);          txtChildren.setBounds(10, 120, 10, 40);         children.setBounds(20, 120, 10, 40);         panel3.add(txtChildren);         panel3.add(children);           //user.add(panel12);         /// this is the table of the data base          panel5.add(save);         panel5.add(addHoliday);         panel5.add(deleteHolidays);          // This adds it to the frame         add(panel);         //holidaysPanel.add(panel2);         //holidaysPanel.add(panel3);        // holidaysPanel.add(panel4);        // holidaysPanel.add(panel5);         add(holidaysPanel);        // add(save);        // add(addHoliday);         //add(deleteHolidays);          // This adds it to the frame                            //add the userFrame pannels to the userFrame JTab                  updateHolidayTable();              }               public void updateHolidayTable() {          try {              String sql = "select * from Holiday";             pst = conn.prepareStatement(sql);             rs = pst.executeQuery();             tableHolidays = new JTable();             tableHolidays.setPreferredScrollableViewportSize(new Dimension(600, 90));             tableHolidays.setFillsViewportHeight(true);             ///table.setBounds(40, 40, 40, 40);             JScrollPane jps = new JScrollPane(tableHolidays);                          holidaysPanel.add(jps);             tableHolidays.setModel(DbUtils.resultSetToTableModel(rs));             TableColumn column = null;             ///adjusts specifc widths of columns             column = tableHolidays.getColumnModel().getColumn(0);             column.setPreferredWidth(200); //sport column is bigger             column = tableHolidays.getColumnModel().getColumn(1);             column.setPreferredWidth(200); //sport column is bigger         } catch (Exception e) {             JOptionPane.showMessageDialog(null, e);         }     }      @Override     public void actionPerformed(ActionEvent e) {            }     private class saveListener implements ActionListener {          public void actionPerformed(ActionEvent e) {   // this gets the selected character.              ///String sql ="Insert into Empoyeeinfo (Empoyeeid,name,surname,age,image,gander) values (?,?,?,?,?,?)";             String sql = "Insert into Holiday (Resort,Duration,Accomodation) values (?,?,?)";             //,Board Arrangements,Holiday Type,Holiday Subtype,Star Ratings,N.O of Beds,Weather,Date,Price             try {                 /// names of columns should be here as well as the same amount of question marks                  pst = conn.prepareStatement(sql);                 //need to edit the these columns                 pst.setString(1, country.getText());                 pst.setString(2, region.getText());                 pst.setString(3, resort.getText());                // pst.setString(4, duration.getText());                 //pst.setString(5, accommodation.getText());                 //pst.setString(6, boardBases.getText());                 //pst.setString(7, holidayType.getText());                 //pst.setString(8, holidaySubtype.getText());                 //pst.setString(9, ratings.getText());                 //pst.setString(10, date.getText());                 //pst.setString(11, weather.getText());                 //pst.setString(12, price.getText());                 //pst.setString(13, adult.getText());                 //pst.setString(14, children.getText());                   pst.execute();                 JOptionPane.showMessageDialog(null, "Saved");                } catch (Exception e1) {                 JOptionPane.showMessageDialog(null, e1);             }             updateHolidayTable();          }     }      private class deleteListener implements ActionListener {          public void actionPerformed(ActionEvent e) {             //if (e.getSource() == deleteHolidays) {             String sql = "delete from Holiday where rowid=?";              try {                 pst = conn.prepareStatement(sql);                 pst.setString(1, country.getText());                 pst.setString(2, region.getText());                 pst.setString(3, resort.getText());                 //pst.setString(4, duration.getText());                // pst.setString(5, accommodation.getText());                // pst.setString(6, boardBases.getText());                 //pst.setString(7, holidayType.getText());                 //pst.setString(8, holidaySubtype.getText());                // pst.setString(9, ratings.getText());               //  pst.setString(10, date.getText());                // pst.setString(11, weather.getText());                // pst.setString(12, price.getText());                 //pst.setString(13, adult.getText());                 //pst.setString(14, children.getText());                                  pst.execute();                                  JOptionPane.showMessageDialog(null, "you have deleted");             } catch (SQLException s) {                 JOptionPane.showMessageDialog(null, s);             }             finally                  {                     try                      {                          rs.close();                          pst.close();                      } catch (SQLException ex)                      {                         JOptionPane.showMessageDialog(null, ex);                     }                 }                          updateHolidayTable();                   }     }      private class MouseListnerHolidayTable implements MouseListener{            public void mouseClicked(MouseEvent evt) {                ///String blah = evt.toString();                                  /// names of columns should be here as well as the same amount of question marks                  int row = tableHolidays.getSelectedRow();                                  System.out.println(row);                 String tableClick = (tableHolidays.getModel().getValueAt(row, 1).toString());                try {                    ///query from database                 String sql ="select * from Holiday where Country-Region='"+tableClick+"' ";                //connection                 pst = conn.prepareStatement(sql);                 rs = pst.executeQuery();                                  if (rs.next()) {                     String add1 = rs.getString("Country-Region");                     System.out.println(add1);                     country.setText(add1);                      String add2 = rs.getString("Resort");                     resort.setText(add2);                      String add3 = rs.getString("Duration");                     duration.setText(add3);                      String add4 = rs.getString("Accomodation");                     accommodation.setText(add4);                      //String add5 = rs.getString("Board Arrangements");                     ///boardBases.setText(add5);                      //String add6 = rs.getString("Holiday Type");                     //holidayType.setText(add6);                      //String add7 = rs.getString("Holiday Subtype");                     //holidaySubtype.setText(add7);                      //String add8 = rs.getString("Star Rating");                     //ratings.setText(add8);                      //String add9 = rs.getString("N.O of Beds");                    // bed.setText(add9);                      //String add10 = rs.getString("Weather");                    // weather.setText(add10);                      //String add11 = rs.getString("Date");                     //date.setText(add11);                      //String add12 = rs.getString("Price");                     //price.setText(add12);                  }                 ///JOptionPane.showMessageDialog(null, "Deleted");               } catch (Exception e) {                 JOptionPane.showMessageDialog(null, e);             }             updateHolidayTable();         }                  @Override         public void mousePressed(MouseEvent e) {             throw new UnsupportedOperationException("Not supported yet.");         }                @Override         public void mouseReleased(MouseEvent e) {                      }                @Override         public void mouseEntered(MouseEvent e) {                      }              @Override         public void mouseExited(MouseEvent e) {                      }   } }