Database Mobil
Ini adalah source code untuk membuat aplikasi database mobil yang akan mengelola data-data dari sebuah mobil.....Check it out gan......
- /*
- * To change this template, choose Tools Templates
- * and open the template in the editor.
- */
- /*
- * CAR.java
- *
- * Created on Apr 27, 2012, 11:32:51 PM
- */
- /**
- *
- * @author Muh. Idil Haq Amir
- * @www.idil810.blogspot.com
- */
- import java.awt.Dimension;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- import javax.swing.JTable;
- public class Input_Data extends javax.swing.JFrame {
- private String database = "jdbc:mysql://localhost/5211100704";
- private String username = "root";
- private String password = "";
- private String SQL;
- private String IDMobil, NamaMobil, MerkMobil, JenisMobil, HargaMobil;
- private Connection con;
- private Statement stm;
- /** Creates new form CAR */
- public void connect(){
- try{
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection(database,username,password);
- stm = con.createStatement();
- System.out.println("Berhasil");
- }
- catch(Exception e){
- System.out.println("ERROR Connection");
- }
- }
- public Input_Data() {
- initComponents();
- connect();
- }
- public void insert() throws SQLException{
- try{
- IDMobil = text1.getText();
- NamaMobil = text2.getText();
- MerkMobil = text3.getText();
- JenisMobil = text4.getText();
- HargaMobil = text5.getText();
- SQL = "INSERT INTO mobil VALUES ("+IDMobil+",'"+NamaMobil+"','"+MerkMobil+"','"+JenisMobil+"','"+HargaMobil+"')";
- stm.executeUpdate(SQL);
- JOptionPane.showMessageDialog(null, "Data berhasil dimasukkan");
- }catch(Exception e){
- JOptionPane.showMessageDialog(null, "Data gagal dimasukkan");
- }
- }
- /** 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")
- //
- private void initComponents() {
- text2 = new javax.swing.JTextField();
- text3 = new javax.swing.JTextField();
- text4 = new javax.swing.JTextField();
- text5 = new javax.swing.JTextField();
- text1 = new javax.swing.JTextField();
- jLabel1 = new javax.swing.JLabel();
- Bsave = new javax.swing.JButton();
- BReset = new javax.swing.JButton();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- jLabel5 = new javax.swing.JLabel();
- jLabel6 = new javax.swing.JLabel();
- Panel = new javax.swing.JScrollPane();
- BView = new javax.swing.JButton();
- BDelete = new javax.swing.JButton();
- BRefresh = new javax.swing.JButton();
- BExit = new javax.swing.JButton();
- BCari = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
- jLabel1.setText("DAFTAR HARGA MOBIL");
- Bsave.setText("Save");
- Bsave.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- BsaveMouseClicked(evt);
- }
- });
- Bsave.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- BsaveActionPerformed(evt);
- }
- });
- BReset.setText("Reset");
- BReset.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- BResetMouseClicked(evt);
- }
- });
- jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
- jLabel2.setText("ID Mobil");
- jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
- jLabel3.setText("Nama Mobil");
- jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
- jLabel4.setText("Merk Mobil");
- jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
- jLabel5.setText("Jenis Mobil");
- jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12));
- jLabel6.setText("Harga Mobil");
- BView.setText("View");
- BView.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- BViewActionPerformed(evt);
- }
- });
- BDelete.setText("Delete");
- BDelete.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- BDeleteActionPerformed(evt);
- }
- });
- BRefresh.setText("Refresh");
- BRefresh.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- BRefreshActionPerformed(evt);
- }
- });
- BExit.setText("EXIT");
- BExit.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- BExitActionPerformed(evt);
- }
- });
- BCari.setText("Cari");
- BCari.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- BCariActionPerformed(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, false)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 157, Short.MAX_VALUE))
- .addComponent(text1, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(text3))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(text2))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(text5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
- .addComponent(text4)))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(BRefresh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(BCari, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(BDelete)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(BView)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(Bsave)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(BReset))
- .addComponent(Panel)
- .addComponent(BExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel2, jLabel3, jLabel4, jLabel5, jLabel6});
- layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {BDelete, BReset, BView, Bsave});
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(text1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(26, 26, 26)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(text2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel3))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel4)
- .addComponent(text3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel5)
- .addComponent(text4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel6)
- .addComponent(text5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(BReset)
- .addComponent(Bsave)
- .addComponent(BView)
- .addComponent(BDelete)
- .addComponent(BRefresh)
- .addComponent(BCari))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(Panel, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
- .addGap(11, 11, 11)
- .addComponent(BExit, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap())
- );
- pack();
- }//
- private void BsaveMouseClicked(java.awt.event.MouseEvent evt) {
- try {
- // TODO add your handling code here:
- insert();
- } catch (SQLException ex) {
- Logger.getLogger(Input_Data.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- private void BsaveActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- // TODO add your handling code here:
- insert();
- } catch (SQLException ex) {
- Logger.getLogger(Input_Data.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- private void BResetMouseClicked(java.awt.event.MouseEvent evt) {
- // TODO add your handling code here:
- reset();
- }
- private void BExitActionPerformed(java.awt.event.ActionEvent evt) {
- System.exit(0);
- }
- private void BViewActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- view();
- }
- private void BDeleteActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- delete();
- }
- private void BRefreshActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- refresh();
- }
- private void BCariActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- cari();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Input_Data().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton BCari;
- private javax.swing.JButton BDelete;
- private javax.swing.JButton BExit;
- private javax.swing.JButton BRefresh;
- private javax.swing.JButton BReset;
- private javax.swing.JButton BView;
- private javax.swing.JButton Bsave;
- private javax.swing.JScrollPane Panel;
- 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.JLabel jLabel6;
- private javax.swing.JTextField text1;
- private javax.swing.JTextField text2;
- private javax.swing.JTextField text3;
- private javax.swing.JTextField text4;
- private javax.swing.JTextField text5;
- // End of variables declaration
- private void reset() {
- text1.setText("");
- text2.setText("");
- text3.setText("");
- text4.setText("");
- text5.setText("");
- }
- private void view(){
- try{
- ArrayList array = new ArrayList();
- SQL = "SELECT * FROM MOBIL";
- ResultSet result = stm.executeQuery(SQL);
- while(result.next()){
- array.add(result.getString(1));
- array.add(result.getString(2));
- array.add(result.getString(3));
- array.add(result.getString(4));
- array.add(result.getString(5));
- String tabel[][] = new String [array.size()/5][5];
- int count = 0;
- for(int i=0;i for(int j=0;j<5;j++){
- tabel[i][j] = array.get(count);
- count++;
- }
- }
- String kolom[] = {"ID MOBIL","NAMA MOBIL","MERK MOBIL","JENIS MOBIL","HARGA MOBIL"};
- JTable jtabel = new javax.swing.JTable(tabel,kolom);
- Panel.setViewportView(jtabel);
- jtabel.setPreferredScrollableViewportSize(new Dimension(800,100));
- }
- }catch(Exception e){
- }
- }
- private void delete(){
- try{
- IDMobil = text1.getText();
- NamaMobil = text2.getText();
- MerkMobil = text3.getText();
- JenisMobil = text4.getText();
- HargaMobil = text5.getText();
- SQL = "DELETE FROM mobil WHERE IDMobil = '"+IDMobil+"' OR NamaMobil = '"+NamaMobil+"' OR MerkMobil = '"+MerkMobil+"' OR JenisMobil = '"+JenisMobil+"' OR HargaMobil = '"+HargaMobil+"'";
- stm.executeUpdate(SQL);
- JOptionPane.showMessageDialog(null,"Data Berhasil dihapus");
- }catch(Exception e){
- JOptionPane.showMessageDialog(null,"Data gagal dihapus!","ERROR",JOptionPane.WARNING_MESSAGE);
- }
- }
- private void refresh(){
- try{
- IDMobil = text1.getText();
- NamaMobil = text2.getText();
- MerkMobil = text3.getText();
- JenisMobil = text4.getText();
- HargaMobil = text5.getText();
- SQL = "UPDATE mobil SET MerkMobil = '"+MerkMobil+"', JenisMobil = '"+JenisMobil+"', HargaMobil = '"+HargaMobil+"' WHERE IDMobil = '"+IDMobil+"' AND NamaMobil = '"+NamaMobil+"'";
- stm.executeUpdate(SQL);
- JOptionPane.showMessageDialog(null,"Data Berhasil diupdate");
- }catch(Exception e){
- JOptionPane.showMessageDialog(null,"Data gagal diupdate!","ERROR",JOptionPane.WARNING_MESSAGE);
- }
- }
- }
0 comments: