Cari Blog Ini
Rabu, 24 Oktober 2018
Aplikasi Pencarian Data Mahasiswa
coding Aplikasi Pencarian Data Mahasiswa
coding Aplikasi Pencarian Data Mahasiswa
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
class Aplikasi_Cari_Sql extends JFrame
{
JLabel gambar = new JLabel(new ImageIcon("gambar/template.jpg"));
JLabel judul = new JLabel("Aplikasi Pencarian Data Mahasiswa");
JLabel garis = new JLabel ("=================================================================================");
JLabel namaku = new JLabel ("Creator By. Ramadhani,S.Kom");
JLabel Lcari = new JLabel ("Cari Data Berdasarkan :");
String [] cari = {"Pilih salah satu","NIM","Nama","Kelas","Kelompok","Tugas1","Tugas2","Tugas3","Tugas4","Tugas5","UTS","UAS","Nilai_Angka","Nilai_Huruf"};
JComboBox cbcari = new JComboBox(cari);
JLabel Ldata = new JLabel ("Masukan Data :");
JTextField txdata = new JTextField ();
JButton tblcari = new JButton ("Search");
JButton tblclear = new JButton ("Clear");
JButton tblLoad = new JButton ("Load Data");
JButton tblcetak = new JButton ("Print");
JButton tblexit = new JButton ("Exit");
DefaultTableModel model = new DefaultTableModel();
JTable Tabelcari = new JTable();
JScrollPane pane = new JScrollPane (Tabelcari);
Aplikasi_Cari_Sql()
{
setTitle("Aplikasi Pencarian Data Mahasiswa");
setLocation(0,0);
setSize(900, 500);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
void KomponenVisual()
{
getContentPane().setLayout(null);
getContentPane().add(judul);
judul.setBounds(300,10,300,20);
judul.setFont(new Font("Tempus Sans ITC",Font.BOLD,18));
getContentPane().add(namaku);
namaku.setBounds(10,10,200,20);
namaku.setEnabled(false);
getContentPane().add(garis);
garis.setBounds(200,30,590,20);
getContentPane().add(Lcari);
Lcari.setBounds(10,60,150,20);
getContentPane().add(cbcari);
cbcari.setBounds(180,60,150,20);
getContentPane().add(Ldata);
Ldata.setBounds(10,90,100,20);
getContentPane().add(txdata);
txdata.setBounds(180,90,150,20);
getContentPane().add(tblcari);
tblcari.setBounds(340,60,100,50);
tblcari.setHorizontalAlignment(SwingConstants.LEADING);
getContentPane().add(tblclear);
tblclear.setBounds(450,60,100,50);
tblclear.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add(tblLoad);
tblLoad.setBounds(560,60,100,50);
getContentPane().add(tblcetak);
tblcetak.setBounds(670,60,100,50);
getContentPane().add(tblexit);
tblexit.setBounds(780,60,100,50);
getContentPane().add(pane);
pane.setBounds(10,130,865,320);
Tabelcari.setModel(model); //untuk header kolom tabel
model.addColumn("NIM");
model.addColumn("Nama");
model.addColumn("Kelas");
model.addColumn("Kelompok");
model.addColumn("Tugas 1");
model.addColumn("Tugas 2");
model.addColumn("Tugas 3");
model.addColumn("Tugas 4");
model.addColumn("Tugas 5");
model.addColumn("UTS");
model.addColumn("UAS");
model.addColumn("Nilai Akhir");
model.addColumn("Nilai Huruf");
getContentPane().add(gambar);
gambar.setBounds(0,0,900,500);
setVisible(true);
loadData(); //untuk ambil data
}
void AksiReaksi()
{
tblLoad.addActionListener(new ActionListener() //untuk tombol load data
{
public void actionPerformed (ActionEvent e)
{
loadData(); //untuk ambil data
} } );
tblcari.addActionListener(new ActionListener() //untuk tombol cari
{
public void actionPerformed (ActionEvent e)
{
if(txdata.isEnabled()==true)
{
if(txdata.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Data belum dimasukkan");
}
else
{
String sPilihan = String.valueOf(cbcari.getSelectedItem());
String sData=txdata.getText();
proses_temukanData("select * from datanilai where "+sPilihan+" = '"+sData+"'");
}}
else
{
// JOptionPane.showMessageDialog(null,"maaf Data yang anda cari tidak ada");
proses_temukanData(" ");
}}});
tblclear.addActionListener(new ActionListener() //untuk hapus data
{
public void actionPerformed (ActionEvent e)
{
resetData();
}});
tblcetak.addActionListener(new ActionListener() //UNTUK PRINT
{
public void actionPerformed (ActionEvent e)
{
int y = 0;
Frame fr = new Frame();
PrintJob print = fr.getToolkit().getPrintJob(fr, "Prinnting", null, null);
if (print != null)
{
Graphics g = print.getGraphics();
if (g != null)
{
//untuk kop (header)
g.setFont(new Font("Dialog", 1, 11));
g.drawString("LAPORAN DATA NILAI MAHASISWA", 150, 40);
//untuk nama kolom tabel
String nim = model.getColumnName(0);
String nama = model.getColumnName(1);
String kelas = model.getColumnName(2);
String kuliah = model.getColumnName(3);
String tugas1 = model.getColumnName(4);
String tugas2 = model.getColumnName(5);
String tugas3 = model.getColumnName(6);
String tugas4 = model.getColumnName(7);
String tugas5 = model.getColumnName(8);
String uts = model.getColumnName(9);
String uas = model.getColumnName(10);
String akhir = model.getColumnName(11);
String huruf = model.getColumnName(12);
g.setFont(new Font("Dialog", 1, 8));
g.drawString(nim, 30, 70);
g.drawString(nama, 80, 70);
g.drawString(kelas, 130, 70);
g.drawString(kuliah, 160, 70);
g.drawString(tugas1, 240, 70);
g.drawString(tugas2, 280, 70);
g.drawString(tugas3, 320, 70);
g.drawString(tugas4, 360, 70);
g.drawString(tugas5, 400, 70);
g.drawString(uts, 440, 70);
g.drawString(uas, 470, 70);
g.drawString(akhir, 500, 70);
g.drawString(huruf, 530, 70);
g.drawLine(30, 73, 570, 73); //untuk garis
//untuk datanya tabel
int n = model.getRowCount();
for (int i = 0; i < n; i++)
{
int k = i + 1;
int j = 10 * k;
y = 73 + j;
g.setFont(new Font("Dialog", 0, 8)); //untuk buat tulisan
String data_nim = model.getValueAt(i, 0).toString();
String data_nama = model.getValueAt(i, 1).toString();
String data_kelas = model.getValueAt(i, 2).toString();
String data_kuliah = model.getValueAt(i, 3).toString();
String data_tugas1 = model.getValueAt(i, 4).toString();
String data_tugas2 = model.getValueAt(i, 5).toString();
String data_tugas3 = model.getValueAt(i, 6).toString();
String data_tugas4 = model.getValueAt(i, 7).toString();
String data_tugas5 = model.getValueAt(i, 8).toString();
String data_uts = model.getValueAt(i, 9).toString();
String data_uas = model.getValueAt(i, 10).toString();
String data_akhir = model.getValueAt(i, 11).toString();
String data_huruf = model.getValueAt(i, 12).toString();
g.drawString(data_nim, 30, y);
g.drawString(data_nama, 80, y);
g.drawString(data_kelas, 130, y);
g.drawString(data_kuliah, 160, y);
g.drawString(data_tugas1, 250, y);
g.drawString(data_tugas2, 290, y);
g.drawString(data_tugas3, 330, y);
g.drawString(data_tugas4, 370, y);
g.drawString(data_tugas5, 410, y);
g.drawString(data_uts, 445, y);
g.drawString(data_uas, 475, y);
g.drawString(data_akhir, 505, y);
g.drawString(data_huruf, 540, y);
}
}
print.end();
print.end();
} } } );
tblexit.addActionListener (new ActionListener() //untuk keluar
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}} );
}
//untuk bersihin layar
void resetData()
{
txdata.setText("");
}
//untuk proses temukan data
void proses_temukanData(String sql)
{
model.getDataVector().removeAllElements();
model.fireTableDataChanged();
try
{
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost/DataMahasiswa","root","root");
Statement stm=koneksi.createStatement();
ResultSet r=stm.executeQuery(sql);
while(r.next())
{
String nim = r.getString("nim");
String nama = r.getString("nama");
String kelas = r.getString("kelas");
String kelompok = r.getString("kelompok");
String tugas1 = r.getString("tugas1");
String tugas2 = r.getString("tugas2");
String tugas3 = r.getString("tugas3");
String tugas4 = r.getString("tugas4");
String tugas5 = r.getString("tugas5");
String uts = r.getString("uts");
String uas = r.getString("uas");
String Nilai_Angka = r.getString("Nilai_Angka");
String Nilai_Huruf = r.getString("Nilai_Huruf");
String[] dataBaris={nim,nama,kelas,kelompok,tugas1,tugas2,tugas3,tugas4,tugas5,uts,uas,Nilai_Angka,Nilai_Huruf};
model.addRow(dataBaris);
}
r.close();
stm.close();
}
catch(SQLException ext)
{
System.err.println("Pencarian Data Error :"+ext);
} }
void loadData() //untuk tampil data
{
model.getDataVector().removeAllElements();
model.fireTableDataChanged();
try
{
//Class.forName("com.mysql.jdbc.Driver");
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost/DataMahasiswa","root","root");
Statement stm=koneksi.createStatement();
String sql="select * from datanilai ";
ResultSet r=stm.executeQuery(sql);
while(r.next())
{
Object[]ob=new Object[14];
ob[0]=r.getString("nim");
ob[1]=r.getString("nama");
ob[2]=r.getString("kelas");
ob[3]=r.getString("kelompok");
ob[4]=r.getString("tugas1");
ob[5]=r.getString("tugas2");
ob[6]=r.getString("tugas3");
ob[7]=r.getString("tugas4");
ob[8]=r.getString("tugas5");
ob[9]=r.getString("uts");
ob[10]=r.getString("uas");
ob[11]=r.getString("Nilai_Angka");
ob[12]=r.getString("Nilai_Huruf");
model.addRow(ob);
}
r.close();
stm.close();
}
catch(SQLException e)
{
System.out.println("Load Data Error : "+e);
}
/*Tabelcari.addMouseListener(new MouseListener() //untuk tabel cari
{
public void mouseClicked(MouseEvent e)
{
int i=Tabelcari.getSelectedRow();
if(i==-1)
{
return;
} }
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}); */
}
public static void main(String[]args)
{
Aplikasi_Cari_Sql acs = new Aplikasi_Cari_Sql();
acs.KomponenVisual();
acs.AksiReaksi();
} }