Searching GUI dan MySql “AND / OR”


Teman-teman sekarang saya kasih coding buat serching atau pencarian yah… hmmmm tapi ini searcing bukan sembarang searcing loh, ini searcing pake GUI trus konek database juga. Jika tulisan sebelummnya data dari database disimpan dulu di Array, yang ini beda.. karena datanya langsung dari databasenya…

Ayo disimak coding berikut ini:

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Cursor;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.*;
import java.sql.*;
import java.util.*;

public class searchpetugas extends JFrame
{
Object[][] datapetugas=new Object[30][5];
String[] judulpetugas={“ID PETUGAS”,”NAMA PETUGAS”,”ALAMAT PETUGAS”,”JABATAN”,”HARI KERJA”};
JTable tabelpetugas = new JTable(datapetugas,judulpetugas);
JScrollPane scrolltabelpetugas;
JPanel panellogin = new JPanel();
String [] pilih = {“AND”,”OR”};
JComboBox AndOr = new JComboBox(pilih);
JButton cari=new JButton(“CARI”);
JLabel cariid= new JLabel(“CARI (ID)”);
JLabel carinama = new JLabel(“CARI (NAMA)”);
JLabel carialamat = new JLabel(“CARI (ALAMAT)”);
JTextField categori=new JTextField();
JTextField categorinama = new JTextField();
JTextField categorialamat = new JTextField();
JButton cancel=new JButton(“CANCEL”);

Connection con;
String url,db,driver,user;
public Statement stat;

public void connect()
{
con=null;
url=”jdbc:mysql://localhost:3306/”;
db=”tugasbesardb”;
driver=”com.mysql.jdbc.Driver”;
try
{
Class.forName(driver);
}
catch(ClassNotFoundException cnfe)
{
System.err.println(“Class driver tidak ada : “+cnfe.getMessage());
}
}

public searchpetugas()
{
setTitle(“SEARCH PETUGAS”);
setLocation(450, 150);
setSize(700,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

void Komponensearchpetugas ()
{
getContentPane().setLayout(null);

getContentPane().add(AndOr);
AndOr.setBounds(160, 30, 150, 30);
getContentPane().add(cari);
cari.setBounds(160, 190, 150, 30);
getContentPane().add(cariid);
cariid.setBounds(30, 70, 100, 30);
getContentPane().add(carinama);
carinama.setBounds(30, 110, 120, 30);
getContentPane().add(carialamat);
carialamat.setBounds(30, 150, 120, 30);
getContentPane().add(categori);
categori.setBounds(160, 70, 150, 30);
getContentPane().add(categorinama);
categorinama.setBounds(160, 110, 150, 30);
getContentPane().add(categorialamat);
categorialamat.setBounds(160, 150, 150, 30);
getContentPane().add(cancel);
cancel.setBounds(560, 420, 100, 30);

getContentPane().add(panellogin);
panellogin.setBounds(30, 230, 630, 150);
panellogin.setBackground(Color.white);

setVisible(true);
}

void aksisearchpetugas()
{
cari.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if((boolean)AndOr.getSelectedItem().equals(“AND”)){
String cat1=categori.getText();
String cat2=categorinama.getText();
String cat3=categorialamat.getText();

int proses=0;
connect();
try {
String bb=(String)tabelpetugas.getValueAt(proses, 0);
con=(Connection) DriverManager.getConnection(url+db,”root”,””);
Statement statement = (Statement) con.createStatement();
String sql=”SELECT * FROM karyawan WHERE (Id_Petugas like ‘”+cat1+”%’and NAMA_PETUGAS like ‘”+cat2+”%’ and ALAMAT_PETUGAS like'”+cat3+”%’);”;
ResultSet kl=statement.executeQuery(sql);
while(kl.next())
{
if (tabelpetugas.getValueAt(proses, 1)==bb)
{
tabelpetugas.setValueAt(kl.getString(1),proses,0);
tabelpetugas.setValueAt(kl.getString(2),proses,1);
tabelpetugas.setValueAt(kl.getString(3),proses,2);
tabelpetugas.setValueAt(kl.getString(4),proses,3);
tabelpetugas.setValueAt(kl.getString(5),proses,4);
}
else
{
proses ++;
tabelpetugas.setValueAt(kl.getString(1),proses,0);
tabelpetugas.setValueAt(kl.getString(2),proses,1);
tabelpetugas.setValueAt(kl.getString(3),proses,2);
tabelpetugas.setValueAt(kl.getString(4),proses,3);
tabelpetugas.setValueAt(kl.getString(5),proses,4);

}
}

scrolltabelpetugas=new JScrollPane(tabelpetugas,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrolltabelpetugas.setLocation(0,0);
scrolltabelpetugas.setSize(new Dimension(630,300));
panellogin.add(scrolltabelpetugas);
}
catch (Exception x)
{
JOptionPane.showMessageDialog(null, “Eror : “+x,”Warning”,JOptionPane.WARNING_MESSAGE);
}
}
else if((boolean)AndOr.getSelectedItem().equals(“OR”)){
String cat1=categori.getText();
String cat2=categorinama.getText();
String cat3=categorialamat.getText();
int proses=0;
connect();
try {
String bb=(String)tabelpetugas.getValueAt(proses, 0);
con=(Connection) DriverManager.getConnection(url+db,”root”,””);
Statement statement = (Statement) con.createStatement();
String sql=”SELECT * FROM karyawan WHERE (Id_Petugas like ‘”+cat1+”%’or NAMA_PETUGAS like ‘”+cat2+”%’ or ALAMAT_PETUGAS like'”+cat3+”%’);”;
ResultSet kl=statement.executeQuery(sql);
while(kl.next())
{
if (tabelpetugas.getValueAt(proses, 1)==bb)
{
tabelpetugas.setValueAt(kl.getString(1),proses,0);
tabelpetugas.setValueAt(kl.getString(2),proses,1);
tabelpetugas.setValueAt(kl.getString(3),proses,2);
tabelpetugas.setValueAt(kl.getString(4),proses,3);
tabelpetugas.setValueAt(kl.getString(5),proses,4);
}
else
{
proses ++;
tabelpetugas.setValueAt(kl.getString(1),proses,0);
tabelpetugas.setValueAt(kl.getString(2),proses,1);
tabelpetugas.setValueAt(kl.getString(3),proses,2);
tabelpetugas.setValueAt(kl.getString(4),proses,3);
tabelpetugas.setValueAt(kl.getString(5),proses,4);

}
}

scrolltabelpetugas=new JScrollPane(tabelpetugas,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrolltabelpetugas.setLocation(0,0);
scrolltabelpetugas.setSize(new Dimension(630,300));
panellogin.add(scrolltabelpetugas);
}
catch (Exception x)
{
JOptionPane.showMessageDialog(null, “Eror : “+x,”Warning”,JOptionPane.WARNING_MESSAGE);
}
}
}
});

cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

dispose();

}
});

}

public static void main(String[] args)
{

searchpetugas l = new searchpetugas ();
l.Komponensearchpetugas();
l.aksisearchpetugas();;

}
}

Ini hasilnya:

Beli T-Shirt, Raglan dan Kids T-Shirt Original Ocean Seven

  1. T-Shirt, Raglan dan Kids T-Shirt Anime, Manga, Gamers dan Cartoon World (OceanSeven)
  2. T-Shirt, Raglan dan Kids T-Shirt Footbal Series (OceanSeven)
  3. T-Shirt, Raglan dan Kids T-Shirt Music Series (OceanSeven)
  4. T-Shirt, Raglan dan Kids T-Shirt OceanSeven – Custom dan Basic Tees (OceanSeven)
  5. T-Shirt, Raglan dan Kids T-Shirt OceanSeven -Kids Collections (OceanSeven)
  6. T-Shirt, Raglan dan Kids T-Shirt OceanSeven -Signature Serues (OceanSeven)
  7. T-Shirt, Raglan dan Kids T-Shirt  Social Media dan Photography (OceanSeven)
  8. T-Shirt, Raglan dan Kids T-Shirt Sport, Automotive dan Adventure (OceanSeven)
  9. T-Shirt, Raglan dan Kids T-Shirt Superheroes dan Movies (OceanSeven)

2 pemikiran pada “Searching GUI dan MySql “AND / OR”

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s