Advertisement
stevennathaniel

Update Tabel PostgreSQL Menggunakan PreparedStatement

Jan 15th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.34 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 latihan12;
  7.  
  8.  
  9. import com.toedter.calendar.JDateChooser;
  10.  
  11. import javax.swing.JTable;
  12.  
  13. import java.sql.Connection;
  14.  
  15. import java.sql.DriverManager;
  16.  
  17. import java.sql.ResultSet;
  18.  
  19. import java.sql.ResultSetMetaData;
  20.  
  21. import java.sql.SQLException;
  22.  
  23. import java.sql.Statement;
  24.  
  25. import java.util.logging.Logger;
  26.  
  27. import java.util.logging.Level;
  28.  
  29. import javax.swing.table.DefaultTableModel;
  30.  
  31. import java.text.DateFormat;
  32.  
  33. import java.text.SimpleDateFormat;
  34.  
  35. import java.util.Calendar;
  36.  
  37. import java.util.*;
  38.  
  39. import java.util.Date;
  40.  
  41. import java.sql.*;
  42.  
  43. import java.util.Locale;
  44.  
  45. import java.util.Vector;
  46.  
  47. import javax.swing.JOptionPane;
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. /**
  55.  *
  56.  * @author steven
  57.  */
  58. public class FrameInputUpdate8 extends javax.swing.JFrame {
  59.  
  60.     /**
  61.      * Creates new form FrameInputUpdate8
  62.      */
  63.     public FrameInputUpdate8() {
  64.        
  65.         initComponents();
  66.        
  67.        
  68.        
  69.        
  70.        
  71.        
  72.     }
  73.  
  74.     /**
  75.      * This method is called from within the constructor to initialize the form.
  76.      * WARNING: Do NOT modify this code. The content of this method is always
  77.      * regenerated by the Form Editor.
  78.      */
  79.     @SuppressWarnings("unchecked")
  80.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  81.     private void initComponents() {
  82.  
  83.         jTextField1 = new javax.swing.JTextField();
  84.         jLabel1 = new javax.swing.JLabel();
  85.         jLabel2 = new javax.swing.JLabel();
  86.         jLabel3 = new javax.swing.JLabel();
  87.         jDateChooser1 = new com.toedter.calendar.JDateChooser();
  88.         jDateChooser2 = new com.toedter.calendar.JDateChooser();
  89.         jButton1 = new javax.swing.JButton();
  90.  
  91.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  92.  
  93.         jTextField1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  94.  
  95.         jLabel1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  96.         jLabel1.setText("ID Tanggal");
  97.  
  98.         jLabel2.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  99.         jLabel2.setText("Tanggal Mulai");
  100.  
  101.         jLabel3.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  102.         jLabel3.setText("Tanggal Berakhir");
  103.  
  104.         jDateChooser1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  105.  
  106.         jDateChooser2.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  107.  
  108.         jButton1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  109.         jButton1.setText("Update");
  110.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  111.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  112.                 jButton1ActionPerformed(evt);
  113.             }
  114.         });
  115.  
  116.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  117.         getContentPane().setLayout(layout);
  118.         layout.setHorizontalGroup(
  119.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  120.             .addGroup(layout.createSequentialGroup()
  121.                 .addContainerGap()
  122.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  123.                     .addComponent(jLabel1)
  124.                     .addComponent(jLabel2)
  125.                     .addComponent(jLabel3))
  126.                 .addGap(25, 25, 25)
  127.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  128.                     .addGroup(layout.createSequentialGroup()
  129.                         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
  130.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  131.                         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
  132.                         .addGap(0, 0, Short.MAX_VALUE))
  133.                     .addComponent(jDateChooser1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  134.                     .addComponent(jDateChooser2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  135.                 .addContainerGap())
  136.         );
  137.         layout.setVerticalGroup(
  138.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  139.             .addGroup(layout.createSequentialGroup()
  140.                 .addGap(11, 11, 11)
  141.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  142.                     .addGroup(layout.createSequentialGroup()
  143.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  144.                             .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
  145.                             .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
  146.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  147.                         .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  148.                     .addGroup(layout.createSequentialGroup()
  149.                         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
  150.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  151.                         .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
  152.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  153.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  154.                     .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
  155.                     .addComponent(jDateChooser2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
  156.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  157.         );
  158.  
  159.         pack();
  160.     }// </editor-fold>                        
  161.  
  162.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  163.         // TODO add your handling code here:
  164.        
  165.         String idTanggal = jTextField1.getText();
  166.        
  167.        
  168.         Integer idTanggal2 = Integer.valueOf(idTanggal);
  169.        
  170.        
  171.        
  172.         java.sql.Date tanggalMulai = new java.sql.Date(jDateChooser1.getDate().getTime());
  173.        
  174.         java.sql.Date tanggalBerakhir = new java.sql.Date(jDateChooser2.getDate().getTime());
  175.        
  176.        
  177.         Connection koneksi = null;
  178.        
  179.         // Statement stmt = null;
  180.        
  181.         PreparedStatement PSUpdate = null;
  182.        
  183.         String updateData = "UPDATE penanggalan SET tanggalmulai = ?, tanggalberakhir = ? WHERE idtanggal= ?";
  184.        
  185.        
  186.         String url = "jdbc:postgresql://localhost:5432/pdam";
  187.        
  188.         String user = "steven";
  189.        
  190.         String password = "kucing";
  191.        
  192.        
  193.         try {
  194.            
  195.            
  196.             koneksi = DriverManager.getConnection(url,user,password);
  197.            
  198.             PSUpdate = koneksi.prepareStatement(updateData);
  199.            
  200.             PSUpdate.setDate(1, tanggalMulai);
  201.            
  202.             PSUpdate.setDate(2, tanggalBerakhir);
  203.            
  204.             PSUpdate.setInt(3, idTanggal2);
  205.            
  206.            
  207.             PSUpdate.executeUpdate();
  208.            
  209.            
  210.             koneksi.close();
  211.            
  212.        
  213.            
  214.        
  215.        
  216.        
  217.     } catch (SQLException ex) {
  218.    
  219.    
  220.     ex.printStackTrace();
  221.    
  222.    
  223.            
  224.            
  225.            
  226.            
  227.            
  228.         }
  229.        
  230.        
  231.        
  232.        
  233.     }                                        
  234.  
  235.     /**
  236.      * @param args the command line arguments
  237.      */
  238.     public static void main(String args[]) {
  239.         /* Set the Nimbus look and feel */
  240.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  241.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  242.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  243.          */
  244.         try {
  245.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  246.                 if ("Nimbus".equals(info.getName())) {
  247.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  248.                     break;
  249.                 }
  250.             }
  251.         } catch (ClassNotFoundException ex) {
  252.             java.util.logging.Logger.getLogger(FrameInputUpdate8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  253.         } catch (InstantiationException ex) {
  254.             java.util.logging.Logger.getLogger(FrameInputUpdate8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  255.         } catch (IllegalAccessException ex) {
  256.             java.util.logging.Logger.getLogger(FrameInputUpdate8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  257.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  258.             java.util.logging.Logger.getLogger(FrameInputUpdate8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  259.         }
  260.         //</editor-fold>
  261.  
  262.         /* Create and display the form */
  263.         java.awt.EventQueue.invokeLater(new Runnable() {
  264.             public void run() {
  265.                 new FrameInputUpdate8().setVisible(true);
  266.             }
  267.         });
  268.     }
  269.  
  270.     // Variables declaration - do not modify                    
  271.     private javax.swing.JButton jButton1;
  272.     private com.toedter.calendar.JDateChooser jDateChooser1;
  273.     private com.toedter.calendar.JDateChooser jDateChooser2;
  274.     private javax.swing.JLabel jLabel1;
  275.     private javax.swing.JLabel jLabel2;
  276.     private javax.swing.JLabel jLabel3;
  277.     private javax.swing.JTextField jTextField1;
  278.     // End of variables declaration                  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement