Prodi Sistem Informasi | Belajar HTML dan PHP | Skripsi SI
Pesantren Katabah
1000 Penghafal Quran
Pengobatan Ruqyah Mandiri
Sistem Informasi (S1)
Manajemen Informatika
Blog | Kontak | Siap Kerja | Sertifikat | PrivacyPolicy | Inggris Arab | Daftar Isi

Wednesday, March 19, 2014

Cara Membuat Tombol Cari dalam Tutorial Pemrograman Java dan Access

Database di Access
1.      Buat database dengan nama DatabaseMahasiswa2 di Microsoft Access dengan format *.mdb (2002-2003)
2.      Buat tabel berikut:
Field Name
Data Type
Field Size
nomor
Text
3
nama
Text
30

3.      Simpan dengan nama TableMhsCari
4.      Entri data berikut:
Nomor
Nama
101
Cendekia Nagara
102
Fatahillah
103
Ahmad



Membuat Datasource
1.      Start – Control Panel – Administrative Tools
2.      Klik dua kali Data Sources (ODBC)
3.      Add
4.      Klik Driver do Microsoft Access (*.mdb)
5.      Finish
6.      Ketik: DsMhsCari
7.      Select
8.      Cari database yang sudah dibuat di Access, contohnya DatabaseMhsCari
9.      Advanced
10.  Ketik login name: ikhbaar; password: ikhbaar (sesuai keinginan)
11.  OK – OK – OK

Script Koneksi di Netbeans
1.      Masuk ke NetBeans IDE 6.8
2.      File – New Project
3.      Categories: Java; Projects: Java Application
4.      Next
5.      Project Name: JavaApplicationMhsCari;
Project Location: D:\javalat\posting3\JavaAccessCari
6.      Hilangkan centang di Create Main Class
7.      Finish
8.      Di panel Projects
9.      Klik tanda + (expand) di JavaApplicationMhsCari
10.  Klik tanda + di Source Packages
11.  Klik kanan di <default package>
12.  New – Java Package
13.  Package Name: PackageMhsCari
14.  Finish
15.  Klik kanan PackageMhsCari di panel Projects
16.  New – Java Class
17.  Class Name: ClassMhsCari
18.  Finish
19.  Klik dua kali ClassMhsCari di panel Projects
20.  Ketik script koneksi berikut:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PackageMhsCari;

/**
 *
 * @author komtas
 */
import java.sql.*;
import javax.swing.*;

public class ClassMhsCari {
void testClassMhsCari()
{
try
{
String dbname="jdbc:odbc:DsMhsCari"; // nama ODBC
// ingat-ingat, CobaDatabase adalah nama ODBC kita yang udah kita atur sebelumnya
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbname,"ikhbaar","ikhbaar");
// user n passwordnya
// user database samakan dengan yang kita atur di ODBC tadi, yaitu
// 'ikhbaar', dengan password ' ikhbaar '
JOptionPane.showMessageDialog(null, "Koneksi untuk cari data berhasil","Insert Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Error:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
public static void main (String args[])
{
ClassMhsCari test = new ClassMhsCari();
test.testClassMhsCari();
System.exit(0);
}
}
21.  Simpan
22.  Run – Run File (untuk menjalankan program)
23.  Hasilnya akan muncul pesan: “Koneksi untuk cari data berhasil”

Desain Form Cari Data
1.      Klik kanan PackageMhsCari – New JFrame Form
2.      Class Name: JFrameMhsCari
3.      Finish
4.      Buat desain sebagai berikut:

5.      Gambar di atas ubah dengan ketentuan:
-          jLabel1®Text: No
-          jLabel2® Text: Nama
-          jTextField1® Text: (Kosongkan); Klik kanan – Change Variable Name: txtNo
-          jTextField2® Text: (Kosongkan); Klik kanan – Change Variable Name: txtNama
-          jButton1® Text: Cari; Klik kanan – Change Variable Name: btnCari
Hasilnya:


Script Tombol Cari
1.      Klik kanan tombol Cari
2.      Klik Events – Action – actionPerformed
3.      Ketik script tombol Cari berikut:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFrameMhsCari.java
 *
 * Created on Aug 28, 2012, 5:37:51 AM
 */

package PackageMhsCari;
import java.sql.*;
import javax.swing.*;

/**
 *
 * @author komtas
 */
public class JFrameMhsCari extends javax.swing.JFrame {

    /** Creates new form JFrameMhsCari */
    public JFrameMhsCari() {
        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">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtNo = new javax.swing.JTextField();
        txtNama = new javax.swing.JTextField();
        btnCari = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("No");

        jLabel2.setText("Nama");

        btnCari.setText("Cari");
        btnCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariActionPerformed(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()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtNo, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCari)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCari))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void btnCariActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
String no = txtNo.getText();
try
{
String dbname="jdbc:odbc:DsMhsCari";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbname,"ikhbaar","ikhbaar");
Statement statement = con.createStatement();
String sql="SELECT * FROM TableMhsCari WHERE nomor like '"+no+"'";
ResultSet rs = statement.executeQuery(sql);
if (rs.next())
{
txtNama.setText(rs.getString(2));
JOptionPane.showMessageDialog(null, "Nomor ditemukan","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Nomor tidak ditemukan ","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
statement.close();
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Error:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrameMhsCari().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnCari;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txtNama;
    private javax.swing.JTextField txtNo;
    // End of variables declaration

}

4.      Simpan
5.      Jalankan program (Run – Run Project)
6.      Akan tampil sebuah form seperti berikut:

7.      Ketik “101” di kotak “No” (Sesuaikan dengan tabel di database access)
8.      Klik tombol Cari
9.      Akan tampil pesan “Nomor ditemukan” dan nama “Cendekia Nagara” di kotak “Nama” seperti gambar berikut:


10.  Selesai

***

Sumber: Tutorial Geje - Koneksi Database Access dengan Java (Pake NetBeans 6.7.1). Nama File: 88155864-Goji-Koneksi-Java-Access-Odbc.
"Boleh Konsultasi Masuk Jurusan Sistem Informasi via IG atau Tiktok."
Tips Skripsi Program Studi Sistem Informasi

2 comments:

  1. terus terang ane nggak paham sob sama pemograman. Izin save artikelnya kalau suatu saat dibutuhkan ya sob. Makasih ulasannya

    ReplyDelete
    Replies
    1. Sama, saya juga sedang belajar nih. Silahkan di-share :)

      Delete