Database Mobil


Ini adalah source code untuk membuat aplikasi database mobil yang akan mengelola data-data dari sebuah mobil.....Check it out gan......


  1. /* 
  2.  
  3.  * To change this template, choose Tools Templates 
  4.  
  5.  * and open the template in the editor. 
  6.  
  7.  */  
  8.   
  9. /* 
  10.  
  11.  * CAR.java 
  12.  
  13.  * 
  14.  
  15.  * Created on Apr 27, 2012, 11:32:51 PM 
  16.  
  17.  */  
  18.   
  19. /** 
  20.  
  21.  * 
  22.  
  23.  * @author Muh. Idil Haq Amir 
  24.  
  25.  * @www.idil810.blogspot.com 
  26.  
  27.  */  
  28.   
  29. import java.awt.Dimension;  
  30.   
  31. import java.sql.*;  
  32.   
  33. import java.util.ArrayList;  
  34.   
  35. import java.util.logging.Level;  
  36.   
  37. import java.util.logging.Logger;  
  38.   
  39. import javax.swing.JOptionPane;  
  40.   
  41. import javax.swing.JTable;  
  42.   
  43. public class Input_Data extends javax.swing.JFrame {  
  44.   
  45.     private String database = "jdbc:mysql://localhost/5211100704";  
  46.   
  47.     private String username = "root";  
  48.   
  49.     private String password = "";  
  50.   
  51.     private String SQL;  
  52.   
  53.     private String IDMobil, NamaMobil, MerkMobil, JenisMobil, HargaMobil;  
  54.   
  55.     private Connection con;  
  56.   
  57.     private Statement stm;  
  58.   
  59.     /** Creates new form CAR */  
  60.   
  61.     public void connect(){  
  62.   
  63.         try{  
  64.   
  65.             Class.forName("com.mysql.jdbc.Driver");  
  66.   
  67.             con = DriverManager.getConnection(database,username,password);  
  68.   
  69.             stm = con.createStatement();  
  70.   
  71.             System.out.println("Berhasil");  
  72.   
  73.         }  
  74.   
  75.         catch(Exception e){  
  76.   
  77.             System.out.println("ERROR Connection");  
  78.   
  79.         }  
  80.   
  81.     }  
  82.   
  83.     public Input_Data() {  
  84.   
  85.         initComponents();  
  86.   
  87.         connect();  
  88.   
  89.     }  
  90.   
  91.     public void insert() throws SQLException{  
  92.   
  93.         try{  
  94.   
  95.         IDMobil = text1.getText();  
  96.   
  97.         NamaMobil = text2.getText();  
  98.   
  99.         MerkMobil = text3.getText();  
  100.   
  101.         JenisMobil = text4.getText();  
  102.   
  103.         HargaMobil = text5.getText();  
  104.   
  105.          
  106.   
  107.         SQL = "INSERT INTO mobil VALUES ("+IDMobil+",'"+NamaMobil+"','"+MerkMobil+"','"+JenisMobil+"','"+HargaMobil+"')";  
  108.   
  109.             stm.executeUpdate(SQL);  
  110.   
  111.             JOptionPane.showMessageDialog(null"Data berhasil dimasukkan");  
  112.   
  113.         }catch(Exception e){  
  114.   
  115.             JOptionPane.showMessageDialog(null"Data gagal dimasukkan");  
  116.   
  117.         }  
  118.   
  119.     }  
  120.   
  121.     /** This method is called from within the constructor to 
  122.  
  123.      * initialize the form. 
  124.  
  125.      * WARNING: Do NOT modify this code. The content of this method is 
  126.  
  127.      * always regenerated by the Form Editor. 
  128.  
  129.      */  
  130.   
  131.     @SuppressWarnings("unchecked")  
  132.   
  133.     //                            
  134.     private void initComponents() {  
  135.   
  136.         text2 = new javax.swing.JTextField();  
  137.         text3 = new javax.swing.JTextField();  
  138.         text4 = new javax.swing.JTextField();  
  139.         text5 = new javax.swing.JTextField();  
  140.         text1 = new javax.swing.JTextField();  
  141.         jLabel1 = new javax.swing.JLabel();  
  142.         Bsave = new javax.swing.JButton();  
  143.         BReset = new javax.swing.JButton();  
  144.         jLabel2 = new javax.swing.JLabel();  
  145.         jLabel3 = new javax.swing.JLabel();  
  146.         jLabel4 = new javax.swing.JLabel();  
  147.         jLabel5 = new javax.swing.JLabel();  
  148.         jLabel6 = new javax.swing.JLabel();  
  149.         Panel = new javax.swing.JScrollPane();  
  150.         BView = new javax.swing.JButton();  
  151.         BDelete = new javax.swing.JButton();  
  152.         BRefresh = new javax.swing.JButton();  
  153.         BExit = new javax.swing.JButton();  
  154.         BCari = new javax.swing.JButton();  
  155.   
  156.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
  157.   
  158.         jLabel1.setFont(new java.awt.Font("Tahoma"118));  
  159.         jLabel1.setText("DAFTAR HARGA MOBIL");  
  160.   
  161.         Bsave.setText("Save");  
  162.         Bsave.addMouseListener(new java.awt.event.MouseAdapter() {  
  163.             public void mouseClicked(java.awt.event.MouseEvent evt) {  
  164.                 BsaveMouseClicked(evt);  
  165.             }  
  166.         });  
  167.         Bsave.addActionListener(new java.awt.event.ActionListener() {  
  168.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  169.                 BsaveActionPerformed(evt);  
  170.             }  
  171.         });  
  172.   
  173.         BReset.setText("Reset");  
  174.         BReset.addMouseListener(new java.awt.event.MouseAdapter() {  
  175.             public void mouseClicked(java.awt.event.MouseEvent evt) {  
  176.                 BResetMouseClicked(evt);  
  177.             }  
  178.         });  
  179.   
  180.         jLabel2.setFont(new java.awt.Font("Tahoma"112));  
  181.         jLabel2.setText("ID Mobil");  
  182.   
  183.         jLabel3.setFont(new java.awt.Font("Tahoma"112));  
  184.         jLabel3.setText("Nama Mobil");  
  185.   
  186.         jLabel4.setFont(new java.awt.Font("Tahoma"112));  
  187.         jLabel4.setText("Merk Mobil");  
  188.   
  189.         jLabel5.setFont(new java.awt.Font("Tahoma"112));  
  190.         jLabel5.setText("Jenis Mobil");  
  191.   
  192.         jLabel6.setFont(new java.awt.Font("Tahoma"112));  
  193.         jLabel6.setText("Harga Mobil");  
  194.   
  195.         BView.setText("View");  
  196.         BView.addActionListener(new java.awt.event.ActionListener() {  
  197.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  198.                 BViewActionPerformed(evt);  
  199.             }  
  200.         });  
  201.   
  202.         BDelete.setText("Delete");  
  203.         BDelete.addActionListener(new java.awt.event.ActionListener() {  
  204.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  205.                 BDeleteActionPerformed(evt);  
  206.             }  
  207.         });  
  208.   
  209.         BRefresh.setText("Refresh");  
  210.         BRefresh.addActionListener(new java.awt.event.ActionListener() {  
  211.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  212.                 BRefreshActionPerformed(evt);  
  213.             }  
  214.         });  
  215.   
  216.         BExit.setText("EXIT");  
  217.         BExit.addActionListener(new java.awt.event.ActionListener() {  
  218.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  219.                 BExitActionPerformed(evt);  
  220.             }  
  221.         });  
  222.   
  223.         BCari.setText("Cari");  
  224.         BCari.addActionListener(new java.awt.event.ActionListener() {  
  225.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  226.                 BCariActionPerformed(evt);  
  227.             }  
  228.         });  
  229.   
  230.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
  231.         getContentPane().setLayout(layout);  
  232.         layout.setHorizontalGroup(  
  233.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  234.             .addGroup(layout.createSequentialGroup()  
  235.                 .addContainerGap()  
  236.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)  
  237.                     .addGroup(layout.createSequentialGroup()  
  238.                         .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)  
  239.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)  
  240.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  241.                             .addGroup(layout.createSequentialGroup()  
  242.                                 .addComponent(jLabel1)  
  243.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 157, Short.MAX_VALUE))  
  244.                             .addComponent(text1, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)))  
  245.                     .addGroup(layout.createSequentialGroup()  
  246.                         .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)  
  247.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)  
  248.                         .addComponent(text3))  
  249.                     .addGroup(layout.createSequentialGroup()  
  250.                         .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)  
  251.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)  
  252.                         .addComponent(text2))  
  253.                     .addGroup(layout.createSequentialGroup()  
  254.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  255.                             .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)  
  256.                             .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))  
  257.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)  
  258.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  259.                             .addComponent(text5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)  
  260.                             .addComponent(text4)))  
  261.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()  
  262.                         .addComponent(BRefresh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)  
  263.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  264.                         .addComponent(BCari, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)  
  265.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  266.                         .addComponent(BDelete)  
  267.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  268.                         .addComponent(BView)  
  269.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  270.                         .addComponent(Bsave)  
  271.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  272.                         .addComponent(BReset))  
  273.                     .addComponent(Panel)  
  274.                     .addComponent(BExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))  
  275.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))  
  276.         );  
  277.   
  278.         layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel2, jLabel3, jLabel4, jLabel5, jLabel6});  
  279.   
  280.         layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {BDelete, BReset, BView, Bsave});  
  281.   
  282.         layout.setVerticalGroup(  
  283.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  284.             .addGroup(layout.createSequentialGroup()  
  285.                 .addContainerGap()  
  286.                 .addComponent(jLabel1)  
  287.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  288.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  289.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
  290.                         .addComponent(jLabel2)  
  291.                         .addComponent(text1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))  
  292.                     .addGroup(layout.createSequentialGroup()  
  293.                         .addGap(262626)  
  294.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
  295.                             .addComponent(text2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)  
  296.                             .addComponent(jLabel3))  
  297.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  298.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
  299.                             .addComponent(jLabel4)  
  300.                             .addComponent(text3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))  
  301.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  302.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
  303.                             .addComponent(jLabel5)  
  304.                             .addComponent(text4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))  
  305.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  306.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
  307.                             .addComponent(jLabel6)  
  308.                             .addComponent(text5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))  
  309.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  310.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
  311.                     .addComponent(BReset)  
  312.                     .addComponent(Bsave)  
  313.                     .addComponent(BView)  
  314.                     .addComponent(BDelete)  
  315.                     .addComponent(BRefresh)  
  316.                     .addComponent(BCari))  
  317.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)  
  318.                 .addComponent(Panel, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)  
  319.                 .addGap(111111)  
  320.                 .addComponent(BExit, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)  
  321.                 .addContainerGap())  
  322.         );  
  323.   
  324.         pack();  
  325.     }//                          
  326.   
  327.     private void BsaveMouseClicked(java.awt.event.MouseEvent evt) {                                    
  328.   
  329.         try {  
  330.   
  331.             // TODO add your handling code here:  
  332.   
  333.             insert();  
  334.   
  335.         } catch (SQLException ex) {  
  336.   
  337.             Logger.getLogger(Input_Data.class.getName()).log(Level.SEVERE, null, ex);  
  338.   
  339.         }  
  340.   
  341.     }                                   
  342.   
  343.     private void BsaveActionPerformed(java.awt.event.ActionEvent evt) {                                       
  344.   
  345.         try {  
  346.   
  347.             // TODO add your handling code here:  
  348.   
  349.             insert();  
  350.   
  351.         } catch (SQLException ex) {  
  352.   
  353.             Logger.getLogger(Input_Data.class.getName()).log(Level.SEVERE, null, ex);  
  354.   
  355.         }  
  356.   
  357.     }                                      
  358.   
  359.     private void BResetMouseClicked(java.awt.event.MouseEvent evt) {                                     
  360.   
  361.         // TODO add your handling code here:  
  362.   
  363.         reset();  
  364.   
  365.     }                                    
  366.   
  367. private void BExitActionPerformed(java.awt.event.ActionEvent evt) {                                       
  368.   
  369.     System.exit(0);     
  370.   
  371. }                                      
  372.   
  373. private void BViewActionPerformed(java.awt.event.ActionEvent evt) {                                       
  374.   
  375. // TODO add your handling code here:  
  376.   
  377.     view();  
  378.   
  379. }                                      
  380.   
  381. private void BDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                         
  382.   
  383. // TODO add your handling code here:  
  384.   
  385.     delete();  
  386.   
  387. }                                        
  388.   
  389. private void BRefreshActionPerformed(java.awt.event.ActionEvent evt) {                                          
  390.   
  391. // TODO add your handling code here:  
  392.   
  393.     refresh();  
  394.   
  395. }                                         
  396.   
  397. private void BCariActionPerformed(java.awt.event.ActionEvent evt) {                                       
  398.   
  399. // TODO add your handling code here:  
  400.   
  401.     cari();  
  402.   
  403. }                                      
  404.   
  405.     /** 
  406.  
  407.     * @param args the command line arguments 
  408.  
  409.     */  
  410.   
  411.     public static void main(String args[]) {  
  412.   
  413.         java.awt.EventQueue.invokeLater(new Runnable() {  
  414.   
  415.             public void run() {  
  416.   
  417.                 new Input_Data().setVisible(true);  
  418.   
  419.             }  
  420.   
  421.         });  
  422.   
  423.     }  
  424.   
  425.     // Variables declaration - do not modify                      
  426.   
  427.     private javax.swing.JButton BCari;  
  428.   
  429.     private javax.swing.JButton BDelete;  
  430.   
  431.     private javax.swing.JButton BExit;  
  432.   
  433.     private javax.swing.JButton BRefresh;  
  434.   
  435.     private javax.swing.JButton BReset;  
  436.   
  437.     private javax.swing.JButton BView;  
  438.   
  439.     private javax.swing.JButton Bsave;  
  440.   
  441.     private javax.swing.JScrollPane Panel;  
  442.   
  443.     private javax.swing.JLabel jLabel1;  
  444.   
  445.     private javax.swing.JLabel jLabel2;  
  446.   
  447.     private javax.swing.JLabel jLabel3;  
  448.   
  449.     private javax.swing.JLabel jLabel4;  
  450.   
  451.     private javax.swing.JLabel jLabel5;  
  452.   
  453.     private javax.swing.JLabel jLabel6;  
  454.   
  455.     private javax.swing.JTextField text1;  
  456.   
  457.     private javax.swing.JTextField text2;  
  458.   
  459.     private javax.swing.JTextField text3;  
  460.   
  461.     private javax.swing.JTextField text4;  
  462.   
  463.     private javax.swing.JTextField text5;  
  464.   
  465.     // End of variables declaration                    
  466.   
  467.     private void reset() {  
  468.   
  469.         text1.setText("");  
  470.   
  471.         text2.setText("");  
  472.   
  473.         text3.setText("");  
  474.   
  475.         text4.setText("");  
  476.   
  477.         text5.setText("");  
  478.   
  479.     }  
  480.   
  481.      
  482.   
  483.     private void view(){  
  484.   
  485.         try{  
  486.   
  487.             ArrayList array = new ArrayList();  
  488.         SQL = "SELECT * FROM MOBIL";  
  489.         ResultSet result = stm.executeQuery(SQL);  
  490.         while(result.next()){  
  491.             array.add(result.getString(1));  
  492.             array.add(result.getString(2));  
  493.             array.add(result.getString(3));  
  494.             array.add(result.getString(4));  
  495.             array.add(result.getString(5));  
  496.         String tabel[][] = new String [array.size()/5][5];  
  497.         int count = 0;  
  498.         for(int i=0;i             for(int j=0;j<5;j++){  
  499.                 tabel[i][j] = array.get(count);  
  500.                 count++;  
  501.             }  
  502.         }  
  503.          
  504.         String kolom[] = {"ID MOBIL","NAMA MOBIL","MERK MOBIL","JENIS MOBIL","HARGA MOBIL"};  
  505.         JTable jtabel = new javax.swing.JTable(tabel,kolom);  
  506.         Panel.setViewportView(jtabel);  
  507.         jtabel.setPreferredScrollableViewportSize(new Dimension(800,100));  
  508.              
  509.         }  
  510.         }catch(Exception e){  
  511.              
  512.         }  
  513.     }  
  514.      
  515.     private void delete(){  
  516.         try{  
  517.             IDMobil = text1.getText();  
  518.             NamaMobil = text2.getText();  
  519.             MerkMobil = text3.getText();  
  520.             JenisMobil = text4.getText();  
  521.             HargaMobil = text5.getText();  
  522.   
  523.             SQL = "DELETE FROM mobil WHERE IDMobil = '"+IDMobil+"' OR NamaMobil = '"+NamaMobil+"' OR MerkMobil = '"+MerkMobil+"' OR JenisMobil = '"+JenisMobil+"' OR HargaMobil = '"+HargaMobil+"'";  
  524.             stm.executeUpdate(SQL);  
  525.             JOptionPane.showMessageDialog(null,"Data Berhasil dihapus");  
  526.         }catch(Exception e){  
  527.             JOptionPane.showMessageDialog(null,"Data gagal dihapus!","ERROR",JOptionPane.WARNING_MESSAGE);  
  528.         }  
  529.     }  
  530.      
  531.     private void refresh(){  
  532.         try{  
  533.             IDMobil = text1.getText();  
  534.             NamaMobil = text2.getText();  
  535.             MerkMobil = text3.getText();  
  536.             JenisMobil = text4.getText();  
  537.             HargaMobil = text5.getText();  
  538.   
  539.             SQL = "UPDATE mobil SET MerkMobil = '"+MerkMobil+"', JenisMobil = '"+JenisMobil+"', HargaMobil = '"+HargaMobil+"' WHERE IDMobil = '"+IDMobil+"' AND NamaMobil = '"+NamaMobil+"'";  
  540.             stm.executeUpdate(SQL);  
  541.             JOptionPane.showMessageDialog(null,"Data Berhasil diupdate");  
  542.         }catch(Exception e){  
  543.             JOptionPane.showMessageDialog(null,"Data gagal diupdate!","ERROR",JOptionPane.WARNING_MESSAGE);  
  544.         }  
  545.     }     
  546.    }   

0 comments: