TUGAS AKHIR OOP,,,
simpan,edit,hapus dan keluar
1. syintak di java
import java.sql.*;
import javax.swing.*;
import javax.swing.JTable.*;
public class FrameMenu extends javax.swing.JFrame {
String user = "root";
String host = "localhost";
String db = "mhs";
String site="";
private Object lapor;
public FrameMenu() {
initComponents();
}
private void tampilDATA(){
String[] columnNames={"BP","NAMA","JURUSAN","FAKULTAS","NILAI UTS","NILAI UAS","NILAI HURUF"};
JTable table=new JTable(getData(),columnNames);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
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();
jLabel7 = new javax.swing.JLabel();
tbp = new javax.swing.JTextField();
tnm = new javax.swing.JTextField();
tjuru = new javax.swing.JTextField();
tfak = new javax.swing.JTextField();
tuts = new javax.swing.JTextField();
tuas = new javax.swing.JTextField();
thrf = new javax.swing.JTextField();
bsimpan = new javax.swing.JButton();
bedit = new javax.swing.JButton();
bhapus = new javax.swing.JButton();
bkeluar = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTable2 = new javax.swing.JTable();
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("NO BP");
jLabel2.setText("NAMA");
jLabel3.setText("JURUSAN");
jLabel4.setText("FAKULTAS");
jLabel5.setText("NILAI UTS");
jLabel6.setText("NILAI UAS");
jLabel7.setText("NILAI HURUF");
tbp.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
tbpKeyPressed(evt);
}
});
tuas.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
tuasKeyPressed(evt);
}
});
bsimpan.setText("SIMPAN");
bsimpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bsimpanActionPerformed(evt);
}
});
bedit.setText("EDIT");
bedit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
beditActionPerformed(evt);
}
});
bhapus.setText("HAPUS");
bhapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bhapusActionPerformed(evt);
}
});
bkeluar.setText("KELUAR");
bkeluar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bkeluarActionPerformed(evt);
}
});
jTable2.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null}
},
new String [] {
"BP", "NAMA", "UTS", "UAS", "HURUF"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jScrollPane2.setViewportView(jTable2);
jTable2.getColumnModel().getColumn(0).setHeaderValue("BP");
jTable2.getColumnModel().getColumn(1).setHeaderValue("NAMA");
jTable2.getColumnModel().getColumn(2).setHeaderValue("UTS");
jTable2.getColumnModel().getColumn(3).setHeaderValue("UAS");
jTable2.getColumnModel().getColumn(4).setHeaderValue("HURUF");
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)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 639, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(85, 85, 85)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tfak)
.addComponent(tjuru)
.addComponent(tnm)
.addComponent(tbp, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel5)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 171, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(thrf)
.addComponent(tuas, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(tuts, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))))
.addGap(179, 179, 179)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(bhapus, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(bedit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(bsimpan, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
.addComponent(bkeluar))
.addGap(317, 317, 317))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(tbp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bsimpan))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(tnm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bedit))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(tjuru, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bhapus))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(tfak, 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(jLabel5)
.addComponent(tuts, 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(jLabel6)
.addComponent(tuas, 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(jLabel7)
.addComponent(thrf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
.addComponent(bkeluar)
.addGap(29, 29, 29)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30))
);
pack();
}// </editor-fold>
private void bkeluarActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private void bhapusActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
site="jdbc:mysql://" + host + "/" + db + "?user=" + user;
Connection koneksi=DriverManager.getConnection(site);
Statement st=koneksi.createStatement();
int psn=st.executeUpdate("delete from data "+" where bp="+"'"+tbp.getText()+"'");
if (psn>0){
JOptionPane.showMessageDialog(this, "DATA BERHASIL DI HAPUS");
bersih();
}
st.close();
koneksi.close();
}
catch(SQLException e){
JOptionPane.showMessageDialog(this,"GAGAL HAPUS" + e);
}
catch (ClassNotFoundException e){
JOptionPane.showMessageDialog(this,"jdbc Driver tidak ditemukan");
}
tampilDATA();
}
private void beditActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
site="jdbc:mysql://" + host + "/" + db + "?user=" + user;
Connection koneksi=DriverManager.getConnection(site);
Statement st=koneksi.createStatement();
int psn=st.executeUpdate("update data "+" " +
"set nm="+"'"+tnm.getText()+"',"+
"juru="+"'"+tjuru.getText()+"',"+
"faku="+"'"+tfak.getText()+"',"+
"uts="+"'"+tuts.getText()+"'," +
"uas="+"'"+tuas.getText()+"'," +
"huruf="+"'"+thrf.getText()+"'"+
"where bp="+"'"+tbp.getText()+"'");
if (psn>0){
JOptionPane.showMessageDialog(this, "DATA BERHASIL DI EDIT");
bersih();
}
st.close();
koneksi.close();
}
catch(SQLException e){
JOptionPane.showMessageDialog(this,"GAGAL EDIT" + e);
}
catch (ClassNotFoundException e){
JOptionPane.showMessageDialog(this,"jdbc Driver tidak ditemukan");
}
tampilDATA();
}
private void tbpKeyPressed(java.awt.event.KeyEvent evt) {
int ASCII=evt.getKeyCode();
if(ASCII==10)
{
try{
Class.forName("com.mysql.jdbc.Driver");
site="jdbc:mysql://" + host + "/" + db + "?user=" + user;
Connection koneksi=DriverManager.getConnection(site);
Statement st=koneksi.createStatement();
ResultSet tampil = st.executeQuery("select * from data where bp='"+tbp.getText()+"'");
while(tampil.next())
{
tnm.setText(tampil.getString("nm"));
tjuru.setText(tampil.getString("juru"));
tfak.setText(tampil.getString("faku"));
tuts.setText(tampil.getString("uts"));
tuas.setText(tampil.getString("uas"));
thrf.setText(tampil.getString("huruf"));
tampil.close();
st.close();
}
}
catch(SQLException e){}
catch(ClassNotFoundException e){}
tuts.requestFocus();
}
}
private void bsimpanActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
site="jdbc:mysql://" + host + "/" + db + "?user=" + user;
Connection koneksi=DriverManager.getConnection(site);
Statement st=koneksi.createStatement();
int psn=st.executeUpdate("insert into data"+"(bp,nm,juru,faku,uts,uas,huruf) values" +
"("+"'"+tbp.getText()+"'," +
" "+"'"+tnm.getText()+"',"+
"'"+"'"+tjuru.getText()+"',"+
"'"+"'"+tfak.getText()+"'," +
"'"+"'"+tuts.getText()+"',"+
"'"+"'"+tuas.getText()+"',"+
"'"+"'"+thrf.getText()+"')");
if (psn>0){
JOptionPane.showMessageDialog(this, "DATA BEHASIL DISIMPAN");
bersih();
}
st.close();
koneksi.close();
}
catch(SQLException e){
JOptionPane.showMessageDialog(this,"GAGAL MENYIMPAN" + e);
}
catch (ClassNotFoundException e){
JOptionPane.showMessageDialog(this,"jdbc Driver tidak ditemukan");
}
tampilDATA();
}
private void tuasKeyPressed(java.awt.event.KeyEvent evt) {
int ASCII=evt.getKeyCode();
if(ASCII==10)
{
int a = Integer.parseInt(tuts.getText());
int b = Integer.parseInt(tuas.getText());
String huruf="";
int jml = (a + b)/2;
if (jml>=80){
huruf="A";
}else if (a>=65){
huruf="B";
}else if (jml>=55){
huruf="C";
}else if (jml>=45){
huruf="D";
}else{
huruf="E";
}
thrf.setText(huruf);
}
}
private Object[][] getData(){
Object[][] data=null;
try{
Class.forName("com.mysql.jdbc.Driver");
site="jdbc:mysql://" + host + "/" + db + "?user=" + user;
Connection koneksi=DriverManager.getConnection(site);
Statement st=koneksi.createStatement();
ResultSet hasil=st.executeQuery("Select bp,nm,juru,faku,huruf from data");
hasil.last();
int rowCount =hasil.getRow();
hasil.beforeFirst();
data= new Object[rowCount][5];
int no=-1;
while (hasil.next()){
no=no+1;
data[no][0]=hasil.getString(1);
data[no][1]=hasil.getString(2);
data[no][2]=hasil.getString(3);
data[no][3]=hasil.getString(4);
data[no][4]=hasil.getString(5);
data[no][5]=hasil.getString(6);
data[no][6]=hasil.getString(7);
}
st.close();
}
catch (SQLException e){}
catch (ClassNotFoundException e){}
return data;
}
private void bersih(){
tbp.setText("");
tnm.setText("");
tjuru.setText("");
tfak.setText("");
tuts.setText("");
tuas.setText("");
thrf.setText("");
tbp.requestFocus();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameMenu().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bedit;
private javax.swing.JButton bhapus;
private javax.swing.JButton bkeluar;
private javax.swing.JButton bsimpan;
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.JLabel jLabel7;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable jTable1;
private javax.swing.JTable jTable2;
private javax.swing.JTextField tbp;
private javax.swing.JTextField tfak;
private javax.swing.JTextField thrf;
private javax.swing.JTextField tjuru;
private javax.swing.JTextField tnm;
private javax.swing.JTextField tuas;
private javax.swing.JTextField tuts;
// End of variables declaration
}
2.syintak SQL
CREATE TABLE `data` (
`bp` VARCHAR(14) NOT NULL,
`nm` VARCHAR(30) NOT NULL,
`juru` VARCHAR(20) NOT NULL,
`faku` VARCHAR(20) NOT NULL,
`uts` INT(5) DEFAULT '0' NOT NULL,
`uas` INT(5) DEFAULT '0' NOT NULL,
`huruf` CHAR(3) NOT NULL,
PRIMARY KEY (`bp`)
);