Advertisement
Guest User

Main_Window.java

a guest
Jan 26th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.56 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package primakaramovies;
  7.  
  8. import java.io.InputStream;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.text.ParseException;
  16. import java.text.SimpleDateFormat;
  17. import java.util.ArrayList;
  18. import java.util.Date;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.table.DefaultTableModel;
  23.  
  24. public class Main_Window extends javax.swing.JFrame {
  25.  
  26.     /**
  27.      * Creates new form Main_Window
  28.      */
  29.     public Main_Window() {
  30.         initComponents();
  31.         edtId.setVisible(false);
  32.         defaultInputs();
  33.         Show_Movies_In_JTable();
  34.     }
  35.  
  36.     /**
  37.      * This method is called from within the constructor to initialize the form.
  38.      * WARNING: Do NOT modify this code. The content of this method is always
  39.      * regenerated by the Form Editor.
  40.      */
  41.     @SuppressWarnings("unchecked")
  42.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  43.     private void initComponents() {
  44.  
  45.         jLabel1 = new javax.swing.JLabel();
  46.         jLabel2 = new javax.swing.JLabel();
  47.         jLabel3 = new javax.swing.JLabel();
  48.         edtJudul = new javax.swing.JTextField();
  49.         jLabel4 = new javax.swing.JLabel();
  50.         edtGenre = new javax.swing.JTextField();
  51.         jLabel5 = new javax.swing.JLabel();
  52.         jScrollPane1 = new javax.swing.JScrollPane();
  53.         edtSinopsis = new javax.swing.JTextArea();
  54.         btnSubmit = new javax.swing.JButton();
  55.         jScrollPane2 = new javax.swing.JScrollPane();
  56.         edtTable = new javax.swing.JTable();
  57.         btnClear = new javax.swing.JButton();
  58.         edtId = new javax.swing.JTextField();
  59.         btnUpdate1 = new javax.swing.JButton();
  60.         btnDelete = new javax.swing.JButton();
  61.  
  62.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  63.  
  64.         jLabel2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
  65.         jLabel2.setText("Primakara Movies");
  66.  
  67.         jLabel3.setText("Judul");
  68.  
  69.         jLabel4.setText("Genre");
  70.  
  71.         jLabel5.setText("Sinopsis");
  72.  
  73.         edtSinopsis.setColumns(20);
  74.         edtSinopsis.setRows(5);
  75.         jScrollPane1.setViewportView(edtSinopsis);
  76.  
  77.         btnSubmit.setText("Save");
  78.         btnSubmit.addActionListener(new java.awt.event.ActionListener() {
  79.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  80.                 btnSubmitActionPerformed(evt);
  81.             }
  82.         });
  83.  
  84.         edtTable.setModel(new javax.swing.table.DefaultTableModel(
  85.             new Object [][] {
  86.                 {null, null, null, null},
  87.                 {null, null, null, null},
  88.                 {null, null, null, null},
  89.                 {null, null, null, null}
  90.             },
  91.             new String [] {
  92.                 "id", "Judul Film", "Genre Film", "Sinopsis"
  93.             }
  94.         ));
  95.         edtTable.addMouseListener(new java.awt.event.MouseAdapter() {
  96.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  97.                 edtTableMouseClicked(evt);
  98.             }
  99.         });
  100.         jScrollPane2.setViewportView(edtTable);
  101.  
  102.         btnClear.setText("Clear");
  103.         btnClear.addActionListener(new java.awt.event.ActionListener() {
  104.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  105.                 btnClearActionPerformed(evt);
  106.             }
  107.         });
  108.  
  109.         edtId.setEditable(false);
  110.         edtId.setText("id");
  111.  
  112.         btnUpdate1.setText("Update");
  113.         btnUpdate1.addActionListener(new java.awt.event.ActionListener() {
  114.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  115.                 btnUpdate1ActionPerformed(evt);
  116.             }
  117.         });
  118.  
  119.         btnDelete.setText("Delete");
  120.         btnDelete.addActionListener(new java.awt.event.ActionListener() {
  121.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  122.                 btnDeleteActionPerformed(evt);
  123.             }
  124.         });
  125.  
  126.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  127.         getContentPane().setLayout(layout);
  128.         layout.setHorizontalGroup(
  129.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  130.             .addGroup(layout.createSequentialGroup()
  131.                 .addContainerGap()
  132.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  133.                     .addComponent(jScrollPane1)
  134.                     .addGroup(layout.createSequentialGroup()
  135.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  136.                             .addComponent(jLabel1)
  137.                             .addComponent(jLabel3)
  138.                             .addComponent(jLabel5)
  139.                             .addGroup(layout.createSequentialGroup()
  140.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  141.                                     .addComponent(edtId)
  142.                                     .addComponent(edtJudul, javax.swing.GroupLayout.Alignment.LEADING)
  143.                                     .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
  144.                                     .addComponent(edtGenre, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE))
  145.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  146.                                 .addComponent(jLabel2))
  147.                             .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  148.                         .addGap(0, 4, Short.MAX_VALUE))
  149.                     .addGroup(layout.createSequentialGroup()
  150.                         .addComponent(btnSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
  151.                         .addGap(18, 18, 18)
  152.                         .addComponent(btnUpdate1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
  153.                         .addGap(18, 18, 18)
  154.                         .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
  155.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  156.                         .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
  157.                 .addContainerGap())
  158.         );
  159.         layout.setVerticalGroup(
  160.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  161.             .addGroup(layout.createSequentialGroup()
  162.                 .addContainerGap()
  163.                 .addComponent(jLabel1)
  164.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  165.                     .addGroup(layout.createSequentialGroup()
  166.                         .addGap(12, 12, 12)
  167.                         .addComponent(jLabel2)
  168.                         .addGap(25, 25, 25))
  169.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  170.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  171.                         .addComponent(edtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  172.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
  173.                 .addComponent(jLabel3)
  174.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  175.                 .addComponent(edtJudul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  176.                 .addGap(18, 18, 18)
  177.                 .addComponent(jLabel4)
  178.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  179.                 .addComponent(edtGenre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  180.                 .addGap(18, 18, 18)
  181.                 .addComponent(jLabel5)
  182.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  183.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  184.                 .addGap(18, 18, 18)
  185.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  186.                     .addComponent(btnSubmit)
  187.                     .addComponent(btnClear)
  188.                     .addComponent(btnUpdate1)
  189.                     .addComponent(btnDelete))
  190.                 .addGap(18, 18, 18)
  191.                 .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
  192.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  193.         );
  194.  
  195.         pack();
  196.     }// </editor-fold>                        
  197.    
  198.     public Connection getConnection()
  199.     {
  200.         Connection con = null;
  201.        
  202.         try {
  203.             con = DriverManager.getConnection("jdbc:mysql://localhost/primakara_movies","root","");
  204.             return con;
  205.         } catch (SQLException ex) {
  206.             Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
  207.             return null;
  208.         }
  209.     }
  210.    
  211.     public void defaultInputs(){
  212.         edtId.setText(null);
  213.         edtJudul.setText(null);
  214.         edtGenre.setText(null);
  215.         edtSinopsis.setText(null);
  216.     }
  217.    
  218.     public ArrayList<Movies> getMovieList()
  219.     {
  220.             ArrayList<Movies> movieList  = new ArrayList<Movies>();
  221.             Connection con = getConnection();
  222.             String query = "SELECT * FROM movies";
  223.            
  224.             Statement st;
  225.             ResultSet rs;
  226.            
  227.         try {
  228.            
  229.             st = con.createStatement();
  230.             rs = st.executeQuery(query);
  231.             Movies mv;
  232.            
  233.             while(rs.next())
  234.             {
  235.                 mv = new Movies(rs.getInt("id"),rs.getString("judul"),rs.getString("genre"),rs.getString("sinopsis_singkat"));
  236.                 movieList.add(mv);
  237.             }
  238.            
  239.         } catch (SQLException ex) {
  240.             Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
  241.         }
  242.        
  243.         return movieList;
  244.                
  245.     }
  246.    
  247.     public void Show_Movies_In_JTable()
  248.     {
  249.         ArrayList<Movies> list = getMovieList();
  250.         DefaultTableModel model = (DefaultTableModel)edtTable.getModel();
  251.         // clear jtable content
  252.         model.setRowCount(0);
  253.         Object[] row = new Object[4];
  254.         for(int i = 0; i < list.size(); i++)
  255.         {
  256.             row[0] = list.get(i).getId();
  257.             row[1] = list.get(i).getJudul();
  258.             row[2] = list.get(i).getGenre();
  259.             row[3] = list.get(i).getSinopsis();
  260.            
  261.             model.addRow(row);
  262.         }
  263.     }
  264.    
  265.     public boolean checkInputs()
  266.     {
  267.         if (
  268.                 edtJudul.getText().isEmpty()
  269.            || edtGenre.getText().isEmpty()
  270.            || edtSinopsis.getText().isEmpty()
  271.             )
  272.         {
  273.             return false;
  274.         }else{
  275.             return true;
  276.         }
  277.     }
  278.    
  279.     public void sendDataToInputs(int index)
  280.     {
  281.         edtId.setText(Integer.toString(getMovieList().get(index).getId()));
  282.         edtJudul.setText(getMovieList().get(index).getJudul());
  283.         edtGenre.setText(getMovieList().get(index).getGenre());
  284.         edtSinopsis.setText(getMovieList().get(index).getSinopsis());
  285.     }
  286.    
  287.     private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {                                          
  288.         if(checkInputs()){
  289.             try{
  290.                 Connection con = getConnection();
  291.                 PreparedStatement ps = con.prepareStatement("INSERT INTO movies(judul,genre,sinopsis_singkat)"
  292.                         + "values(?,?,?) ");
  293.                 ps.setString(1, edtJudul.getText());
  294.                 ps.setString(2, edtGenre.getText());
  295.                 ps.setString(3, edtSinopsis.getText());
  296.                 ps.executeUpdate();
  297.                 Show_Movies_In_JTable();
  298.                
  299.                 JOptionPane.showMessageDialog(null, "Film berhasil di simpan!");
  300.                 defaultInputs();
  301.             }catch(SQLException ex){
  302.                 JOptionPane.showMessageDialog(null, "Whoops terjadi kesalahan. Full log: "+ex);
  303.             }
  304.         }else{
  305.             JOptionPane.showMessageDialog(null, "Harap isi semua isian terlebih dahulu!");
  306.         }
  307.     }                                        
  308.  
  309.     private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                        
  310.         defaultInputs();
  311.     }                                        
  312.  
  313.     private void edtTableMouseClicked(java.awt.event.MouseEvent evt) {                                      
  314.         int index = edtTable.getSelectedRow();
  315.         sendDataToInputs(index);
  316.     }                                    
  317.  
  318.     private void btnUpdate1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  319.         if(checkInputs() && !edtId.getText().isEmpty())
  320.         {
  321.             String UpdateQuery = null;
  322.             PreparedStatement ps = null;
  323.             Connection con = getConnection();
  324.            
  325.             try{
  326.                 UpdateQuery = "UPDATE movies SET judul = ?, genre = ?, sinopsis_singkat = ? WHERE id = ?";
  327.                 ps = con.prepareStatement(UpdateQuery);
  328.                 ps.setString(1, edtJudul.getText());
  329.                 ps.setString(2, edtGenre.getText());
  330.                 ps.setString(3, edtSinopsis.getText());
  331.                 ps.setString(4, edtId.getText());
  332.                 ps.executeUpdate();
  333.                 Show_Movies_In_JTable();
  334.                
  335.                 JOptionPane.showMessageDialog(null, "Film telah di ubah!");
  336.                 defaultInputs();
  337.             }catch(SQLException ex){
  338.                 JOptionPane.showMessageDialog(null, "Whoops terjadi kesalahan. Full log: "+ex);
  339.             }
  340.         }else{
  341.             if(edtId.getText().isEmpty()){
  342.                 JOptionPane.showMessageDialog(null, "Silahkan pilih terlebih dahulu data yang ingin di edit!");
  343.             }else{
  344.                 JOptionPane.showMessageDialog(null, "Harap isi semua isian terlebih dahulu!");
  345.             }
  346.         }
  347.     }                                          
  348.  
  349.     private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
  350.         if(!edtId.getText().isEmpty())
  351.         {
  352.             String UpdateQuery = null;
  353.             PreparedStatement ps = null;
  354.             Connection con = getConnection();
  355.            
  356.             try{
  357.                 UpdateQuery = "DELETE FROM movies WHERE id = ?";
  358.                 ps = con.prepareStatement(UpdateQuery);
  359.                 ps.setString(1, edtId.getText());
  360.                 ps.executeUpdate();
  361.                 Show_Movies_In_JTable();
  362.                
  363.                 JOptionPane.showMessageDialog(null, "Film telah berhasil di hapus");
  364.                 defaultInputs();
  365.             }catch(SQLException ex){
  366.                 JOptionPane.showMessageDialog(null, "Whoops terjadi kesalahan. Full log: "+ex);
  367.             }
  368.         }else{
  369.             JOptionPane.showMessageDialog(null, "Silahkan pilih data yang ingin di delete!");
  370.         }
  371.     }                                        
  372.  
  373.     /**
  374.      * @param args the command line arguments
  375.      */
  376.     public static void main(String args[]) {
  377.         /* Set the Nimbus look and feel */
  378.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  379.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  380.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  381.          */
  382.         try {
  383.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  384.                 if ("Nimbus".equals(info.getName())) {
  385.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  386.                     break;
  387.                 }
  388.             }
  389.         } catch (ClassNotFoundException ex) {
  390.             java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  391.         } catch (InstantiationException ex) {
  392.             java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  393.         } catch (IllegalAccessException ex) {
  394.             java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  395.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  396.             java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  397.         }
  398.         //</editor-fold>
  399.  
  400.         /* Create and display the form */
  401.         java.awt.EventQueue.invokeLater(new Runnable() {
  402.             public void run() {
  403.                 new Main_Window().setVisible(true);
  404.             }
  405.         });
  406.     }
  407.  
  408.     // Variables declaration - do not modify                    
  409.     private javax.swing.JButton btnClear;
  410.     private javax.swing.JButton btnDelete;
  411.     private javax.swing.JButton btnSubmit;
  412.     private javax.swing.JButton btnUpdate1;
  413.     private javax.swing.JTextField edtGenre;
  414.     private javax.swing.JTextField edtId;
  415.     private javax.swing.JTextField edtJudul;
  416.     private javax.swing.JTextArea edtSinopsis;
  417.     private javax.swing.JTable edtTable;
  418.     private javax.swing.JLabel jLabel1;
  419.     private javax.swing.JLabel jLabel2;
  420.     private javax.swing.JLabel jLabel3;
  421.     private javax.swing.JLabel jLabel4;
  422.     private javax.swing.JLabel jLabel5;
  423.     private javax.swing.JScrollPane jScrollPane1;
  424.     private javax.swing.JScrollPane jScrollPane2;
  425.     // End of variables declaration                  
  426. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement