Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 primakaramovies;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- import javax.swing.table.DefaultTableModel;
- public class Main_Window extends javax.swing.JFrame {
- /**
- * Creates new form Main_Window
- */
- public Main_Window() {
- initComponents();
- edtId.setVisible(false);
- defaultInputs();
- Show_Movies_In_JTable();
- }
- /**
- * 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">
- private void initComponents() {
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- edtJudul = new javax.swing.JTextField();
- jLabel4 = new javax.swing.JLabel();
- edtGenre = new javax.swing.JTextField();
- jLabel5 = new javax.swing.JLabel();
- jScrollPane1 = new javax.swing.JScrollPane();
- edtSinopsis = new javax.swing.JTextArea();
- btnSubmit = new javax.swing.JButton();
- jScrollPane2 = new javax.swing.JScrollPane();
- edtTable = new javax.swing.JTable();
- btnClear = new javax.swing.JButton();
- edtId = new javax.swing.JTextField();
- btnUpdate1 = new javax.swing.JButton();
- btnDelete = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jLabel2.setText("Primakara Movies");
- jLabel3.setText("Judul");
- jLabel4.setText("Genre");
- jLabel5.setText("Sinopsis");
- edtSinopsis.setColumns(20);
- edtSinopsis.setRows(5);
- jScrollPane1.setViewportView(edtSinopsis);
- btnSubmit.setText("Save");
- btnSubmit.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnSubmitActionPerformed(evt);
- }
- });
- edtTable.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- {null, null, null, null},
- {null, null, null, null},
- {null, null, null, null},
- {null, null, null, null}
- },
- new String [] {
- "id", "Judul Film", "Genre Film", "Sinopsis"
- }
- ));
- edtTable.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- edtTableMouseClicked(evt);
- }
- });
- jScrollPane2.setViewportView(edtTable);
- btnClear.setText("Clear");
- btnClear.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnClearActionPerformed(evt);
- }
- });
- edtId.setEditable(false);
- edtId.setText("id");
- btnUpdate1.setText("Update");
- btnUpdate1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnUpdate1ActionPerformed(evt);
- }
- });
- btnDelete.setText("Delete");
- btnDelete.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnDeleteActionPerformed(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()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel1)
- .addComponent(jLabel3)
- .addComponent(jLabel5)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addComponent(edtId)
- .addComponent(edtJudul, javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(edtGenre, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel2))
- .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(0, 4, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(btnSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(btnUpdate1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(12, 12, 12)
- .addComponent(jLabel2)
- .addGap(25, 25, 25))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(edtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
- .addComponent(jLabel3)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(edtJudul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(jLabel4)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(edtGenre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(jLabel5)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(btnSubmit)
- .addComponent(btnClear)
- .addComponent(btnUpdate1)
- .addComponent(btnDelete))
- .addGap(18, 18, 18)
- .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- public Connection getConnection()
- {
- Connection con = null;
- try {
- con = DriverManager.getConnection("jdbc:mysql://localhost/primakara_movies","root","");
- return con;
- } catch (SQLException ex) {
- Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
- return null;
- }
- }
- public void defaultInputs(){
- edtId.setText(null);
- edtJudul.setText(null);
- edtGenre.setText(null);
- edtSinopsis.setText(null);
- }
- public ArrayList<Movies> getMovieList()
- {
- ArrayList<Movies> movieList = new ArrayList<Movies>();
- Connection con = getConnection();
- String query = "SELECT * FROM movies";
- Statement st;
- ResultSet rs;
- try {
- st = con.createStatement();
- rs = st.executeQuery(query);
- Movies mv;
- while(rs.next())
- {
- mv = new Movies(rs.getInt("id"),rs.getString("judul"),rs.getString("genre"),rs.getString("sinopsis_singkat"));
- movieList.add(mv);
- }
- } catch (SQLException ex) {
- Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
- }
- return movieList;
- }
- public void Show_Movies_In_JTable()
- {
- ArrayList<Movies> list = getMovieList();
- DefaultTableModel model = (DefaultTableModel)edtTable.getModel();
- // clear jtable content
- model.setRowCount(0);
- Object[] row = new Object[4];
- for(int i = 0; i < list.size(); i++)
- {
- row[0] = list.get(i).getId();
- row[1] = list.get(i).getJudul();
- row[2] = list.get(i).getGenre();
- row[3] = list.get(i).getSinopsis();
- model.addRow(row);
- }
- }
- public boolean checkInputs()
- {
- if (
- edtJudul.getText().isEmpty()
- || edtGenre.getText().isEmpty()
- || edtSinopsis.getText().isEmpty()
- )
- {
- return false;
- }else{
- return true;
- }
- }
- public void sendDataToInputs(int index)
- {
- edtId.setText(Integer.toString(getMovieList().get(index).getId()));
- edtJudul.setText(getMovieList().get(index).getJudul());
- edtGenre.setText(getMovieList().get(index).getGenre());
- edtSinopsis.setText(getMovieList().get(index).getSinopsis());
- }
- private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {
- if(checkInputs()){
- try{
- Connection con = getConnection();
- PreparedStatement ps = con.prepareStatement("INSERT INTO movies(judul,genre,sinopsis_singkat)"
- + "values(?,?,?) ");
- ps.setString(1, edtJudul.getText());
- ps.setString(2, edtGenre.getText());
- ps.setString(3, edtSinopsis.getText());
- ps.executeUpdate();
- Show_Movies_In_JTable();
- JOptionPane.showMessageDialog(null, "Film berhasil di simpan!");
- defaultInputs();
- }catch(SQLException ex){
- JOptionPane.showMessageDialog(null, "Whoops terjadi kesalahan. Full log: "+ex);
- }
- }else{
- JOptionPane.showMessageDialog(null, "Harap isi semua isian terlebih dahulu!");
- }
- }
- private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
- defaultInputs();
- }
- private void edtTableMouseClicked(java.awt.event.MouseEvent evt) {
- int index = edtTable.getSelectedRow();
- sendDataToInputs(index);
- }
- private void btnUpdate1ActionPerformed(java.awt.event.ActionEvent evt) {
- if(checkInputs() && !edtId.getText().isEmpty())
- {
- String UpdateQuery = null;
- PreparedStatement ps = null;
- Connection con = getConnection();
- try{
- UpdateQuery = "UPDATE movies SET judul = ?, genre = ?, sinopsis_singkat = ? WHERE id = ?";
- ps = con.prepareStatement(UpdateQuery);
- ps.setString(1, edtJudul.getText());
- ps.setString(2, edtGenre.getText());
- ps.setString(3, edtSinopsis.getText());
- ps.setString(4, edtId.getText());
- ps.executeUpdate();
- Show_Movies_In_JTable();
- JOptionPane.showMessageDialog(null, "Film telah di ubah!");
- defaultInputs();
- }catch(SQLException ex){
- JOptionPane.showMessageDialog(null, "Whoops terjadi kesalahan. Full log: "+ex);
- }
- }else{
- if(edtId.getText().isEmpty()){
- JOptionPane.showMessageDialog(null, "Silahkan pilih terlebih dahulu data yang ingin di edit!");
- }else{
- JOptionPane.showMessageDialog(null, "Harap isi semua isian terlebih dahulu!");
- }
- }
- }
- private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
- if(!edtId.getText().isEmpty())
- {
- String UpdateQuery = null;
- PreparedStatement ps = null;
- Connection con = getConnection();
- try{
- UpdateQuery = "DELETE FROM movies WHERE id = ?";
- ps = con.prepareStatement(UpdateQuery);
- ps.setString(1, edtId.getText());
- ps.executeUpdate();
- Show_Movies_In_JTable();
- JOptionPane.showMessageDialog(null, "Film telah berhasil di hapus");
- defaultInputs();
- }catch(SQLException ex){
- JOptionPane.showMessageDialog(null, "Whoops terjadi kesalahan. Full log: "+ex);
- }
- }else{
- JOptionPane.showMessageDialog(null, "Silahkan pilih data yang ingin di delete!");
- }
- }
- /**
- * @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(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Main_Window.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 Main_Window().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btnClear;
- private javax.swing.JButton btnDelete;
- private javax.swing.JButton btnSubmit;
- private javax.swing.JButton btnUpdate1;
- private javax.swing.JTextField edtGenre;
- private javax.swing.JTextField edtId;
- private javax.swing.JTextField edtJudul;
- private javax.swing.JTextArea edtSinopsis;
- private javax.swing.JTable edtTable;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JScrollPane jScrollPane2;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement