belajar java dan tutorial netbean

tutorial java, contoh aplikasi dengan netbean

a

Total Tayangan Halaman

Diberdayakan oleh Blogger.

input output data


setalah kita belajar database ACCESS dan membuat ODBC. kita buat contoh input output database mahasiswa dengan membuat koneksi:

package koneksi;
import java.awt.HeadlessException;
import java.sql.*;
import javax.swing.*;
/**
*
* @author ttg
*/


public class koneksi {
void testKoneksi()
{
try
{
String dbname="jdbc:odbc:CobaDataBase"; // nama ODBC
// ingat-ingat, CobaDatabase adalah nama ODBC kita yang udah kitaatur sebelumnya
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            try (Connection con = DriverManager.getConnection(dbname,"admin","admin")) {
                JOptionPane.showMessageDialog(null, "Koneksi berhasil","Insert Data",JOptionPane.INFORMATION_MESSAGE);
            }
}
catch ( ClassNotFoundException | SQLException | HeadlessException e)
{
JOptionPane.showMessageDialog(null, "Eror: "+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
public static void main (String args[])
{
koneksi test = new koneksi();
test.testKoneksi();
System.exit(0);
}
}

kemudian untuk tampilannya menambahkan NEWFRAME

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package koneksi;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author USER
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
    }

    /**
     * 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtnim = new javax.swing.JTextField();
        txtnama = new javax.swing.JTextField();
        txtalamat = new javax.swing.JTextField();
        btcari = new javax.swing.JButton();
        bttambah = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("NIM");

        jLabel2.setText("NAMA");

        jLabel3.setText("ALAMAT");

        btcari.setText("CARI");
        btcari.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btcariMouseClicked(evt);
            }
        });
        btcari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btcariActionPerformed(evt);
            }
        });

        bttambah.setText("TAMBAH");
        bttambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttambahActionPerformed(evt);
            }
        });

        jButton3.setText("UBAH");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("HAPUS");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtalamat, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtnama)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(txtnim, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                                    .addGap(27, 27, 27)
                                    .addComponent(btcari)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(bttambah)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)
                        .addGap(34, 34, 34)
                        .addComponent(jButton4)))
                .addContainerGap(84, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtnim, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btcari))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtnama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtalamat, 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(bttambah)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(112, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bttambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttambahActionPerformed
        // TODO add your handling code here:
        
String nim = txtnim.getText();
String nama = txtnama.getText();
String alamat = txtalamat.getText();
try
{
String dbname="jdbc:odbc:CobaDataBase";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            try (Connection con = DriverManager.getConnection(dbname,"admin","admin")) {
                try (java.sql.Statement statement = con.createStatement()) {
                    String sql="insert into mahasiswa values('"+nim+"','"+nama+"','"+alamat+"');";
                    statement.executeUpdate(sql);
                }
                JOptionPane.showMessageDialog(null, "Data berhasildimasukkan..","Insert Data",JOptionPane.INFORMATION_MESSAGE);
            }
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}
    }//GEN-LAST:event_bttambahActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
 // TODO add your handling code here:
    String nim = txtnim.getText();
    {
String dbname="jdbc:odbc:CobaDataBase";
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
try (Connection con = DriverManager.getConnection(dbname,"admin","admin")) {
                try (java.sql.Statement statement = con.createStatement())
{String sql="DELETE FROM mahasiswa WHERE NIM LIKE '"+nim+"'";
statement.executeUpdate(sql);
statement.close();
txtnim.setText("");
txtnama.setText("");
txtalamat.setText("");}
JOptionPane.showMessageDialog(null, "Data berhasil dihapus..","InsertData",JOptionPane.INFORMATION_MESSAGE);
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}
}

}//GEN-LAST:event_jButton4ActionPerformed

    private void btcariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btcariActionPerformed
        // TODO add your handling code here:
        String nim = txtnim.getText();
try
{
String dbname="jdbc:odbc:CobaDataBase";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            try (Connection con = DriverManager.getConnection(dbname,"admin","admin")) {
                java.sql.Statement statement = con.createStatement();
                String sql="SELECT * FROM mahasiswa WHERE NIM like'"+nim+"'";
                ResultSet rs = statement.executeQuery(sql);
                if (rs.next())
                {
                txtnama.setText(rs.getString(2));
                txtalamat.setText(rs.getString(3));
                JOptionPane.showMessageDialog(null, "Data ditemukan","InsertData",JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                JOptionPane.showMessageDialog(null, "Data tidak ditemukan ","InsertData",JOptionPane.INFORMATION_MESSAGE);
                }
            }
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
        
}
        
    }//GEN-LAST:event_btcariActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
String nim = txtnim.getText();
String nama = txtnama.getText();
String alamat = txtalamat.getText();

try

{
    String dbname="jdbc:odbc:CobaDataBase";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

               try (Connection con = DriverManager.getConnection(dbname,"admin","admin")) {
                try (java.sql.Statement statement = con.createStatement()){
                 String sql="UPDATE mahasiswa SET NAMA='"+nama+"', ALAMAT='"+alamat+"'WHERE NIM LIKE '"+nim+"'";

statement.executeUpdate(sql);
statement.close();}
JOptionPane.showMessageDialog(null, "Data berhasil diedit..","InsertData",JOptionPane.INFORMATION_MESSAGE);
con.close();
               }}


               

               catch (Exception e) {
JOptionPane.showMessageDialog(null, "Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}

   
    }//GEN-LAST:event_jButton3ActionPerformed

    private void btcariMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btcariMouseClicked

    }//GEN-LAST:event_btcariMouseClicked

    /**
     * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btcari;
    private javax.swing.JButton bttambah;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField txtalamat;
    private javax.swing.JTextField txtnama;
    private javax.swing.JTextField txtnim;
    // End of variables declaration//GEN-END:variables

}

maka tampilannya:
input output data
input output data




Anda baru saja membaca artikel yang berkategori database / study kasus dengan judul input output data. Anda bisa bookmark halaman ini dengan URL https://mbahjava.blogspot.com/2014/01/input-output-data.html. Terima kasih!
Ditulis oleh: Unknown - Minggu, 26 Januari 2014

Belum ada komentar untuk "input output data"

Posting Komentar

Catatan: Hanya anggota dari blog ini yang dapat mengirim komentar.