Pada modul ini kita akan mencoba membuat aplikasi input nilai mahasiswa dengan database MySQL.
- Untuk bisa menghubungkan database MySQL dengan Java kita perlu library yang namanya JDBC Driver. Silahkan download JDBC Driver dari internet lalu Copy dan Paste ke Folder C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext dan Folder C:\Program Files\Java\jre7\lib\ext.
- Buka Netbeans, klik kanan pada Libraries > Add Library > MySQL JDBC Driver.
- Sebelum kita menginput data ke dalam sebuah tabel, pastikan dulu anda sudah membuat database dan sebuah tabel. Pada project kali ini buatlah sebuah database dengan nama dbnilaimhs dan satu buah tabel dengan nilaimhs.
- Klik kanan pada project anda, pilih New > Java Class
- Tuliskan kode di bawah ini
package
bikininterface;
import
javax.swing.*;
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.event.*;
import
java.sql.*;
public
class Form extends JFrame{
Container konten = new Container();
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JLabel lblNIM = new JLabel("NIM :
");
JLabel lblNama = new JLabel ("Nama :
");
JLabel lblSemester = new JLabel
("Semester : ");
JLabel lblMtKul = new JLabel ("Mata
Kuliah : ");
JLabel lblBobot = new JLabel ("Bobot
SKS : ");
JLabel lblNilai = new JLabel ("Nilai :
");
JTextField TextNIM = new JTextField();
JTextField TextNama = new JTextField();
JTextField TextBobot = new JTextField();
JTextField TextMtKul = new JTextField();
JTextField TextNilai = new JTextField();
ButtonGroup grpSemester = new
ButtonGroup();
JRadioButton radioGanjil = new
JRadioButton("Ganjil");
JRadioButton radioGenap = new
JRadioButton("Genap");
JButton btnNew = new
JButton("Baru");
JButton btnKirim = new
JButton("Simpan");
JButton btnEdit = new
JButton("Edit");
JButton btnKeluar = new
JButton("Keluar");
Statement state ;
Connection connect ;
public Form(){
super("FORM INPUT NILAI
MAHASISWA");
this.setSize(500,300);
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
konten = getContentPane();
konten.setLayout(new
FlowLayout(FlowLayout.CENTER));
konten.add(panel);
konten.add(panel3);
panel.setLayout(new GridLayout(6,2));
panel.add(lblNIM);
panel.add(TextNIM);
panel.add(lblNama);
panel.add(TextNama);
panel.add(lblSemester);
panel2.setLayout(new GridLayout(1,2));
grpSemester.add(radioGanjil);
grpSemester.add(radioGenap);
panel2.add(radioGanjil);
panel2.add(radioGenap);
panel.add(panel2);
panel.add(lblMtKul);
panel.add(TextMtKul);
panel.add(lblBobot);
panel.add(TextBobot);
panel.add(lblNilai);
panel.add(TextNilai);
panel3.setLayout(new
GridLayout(1,3,5,5));
itemHandler itemHandler = new
itemHandler();
panel3.add(btnNew);
btnNew.addActionListener(itemHandler);
panel3.add(btnKirim);
btnKirim.addActionListener(itemHandler);
panel3.add(btnKeluar);
btnKeluar.addActionListener(itemHandler);
show();
}
private class itemHandler implements
ActionListener{
@Override
public void actionPerformed(ActionEvent
e){
Object obj=e.getSource();
if(obj==btnKirim){
String nim =
TextNIM.getText();
String nama =
TextNama.getText();
String semester = null;
if(radioGanjil.isSelected())
semester =
"Ganjil";
else
if(radioGenap.isSelected())
semester =
"Genap";
String bobot =
TextBobot.getText();
String mtKul = TextMtKul.getText();
String nilai =
TextNilai.getText();
int nBobot =
Integer.parseInt(bobot);
int nMtKul =
Integer.parseInt(nilai);
int jml = nBobot * nMtKul;
try {
connect =
DriverManager.getConnection("jdbc:mysql://localhost:3306/dbnilaimhs","root","");
System.out.println("Database ditemukan");
state =
connect.createStatement();
String sql = "insert
into inputnama
values('"+nim+"','"+nama+"','"+semester+"','"+mtKul+"','"+nBobot+"','"+nMtKul+"','"+jml+"')";
state.executeUpdate(sql);
}
catch (SQLException ex){
System.err.println("SQLException:"+ex.getMessage());
} // TODO add your handling code here:
}
if(obj == btnKeluar){
System.exit(0);
}
if(obj ==btnNew){
TextNIM.setText("");;
TextNama.setText("");;
TextMtKul.setText("");;
TextBobot.setText("");;
TextNilai.setText("");;
}
}
}
public static void main(String args[]){
Form tampil = new Form();
}
}
panel.add(panel2);
panel.add(lblMtKul);
panel.add(TextMtKul);
panel.add(lblBobot);
panel.add(TextBobot);
panel.add(lblNilai);
panel.add(TextNilai);
panel3.setLayout(new
GridLayout(1,3,5,5));
itemHandler itemHandler = new
itemHandler();
panel3.add(btnNew);
btnNew.addActionListener(itemHandler);
panel3.add(btnKirim);
btnKirim.addActionListener(itemHandler);
panel3.add(btnKeluar);
btnKeluar.addActionListener(itemHandler);
show();
}
private class itemHandler implements
ActionListener{
@Override
public void actionPerformed(ActionEvent
e){
Object obj=e.getSource();
if(obj==btnKirim){
String nim =
TextNIM.getText();
String nama =
TextNama.getText();
String semester = null;
if(radioGanjil.isSelected())
semester =
"Ganjil";
else
if(radioGenap.isSelected())
semester =
"Genap";
String bobot =
TextBobot.getText();
String mtKul =
TextMtKul.getText();
String nilai =
TextNilai.getText();
int nBobot =
Integer.parseInt(bobot);
int nMtKul =
Integer.parseInt(nilai);
int jml = nBobot * nMtKul;
try {
connect =
DriverManager.getConnection("jdbc:mysql://localhost:3306/dbnilaimhs","root","");
System.out.println("Database ditemukan");
state = connect.createStatement();
String sql = "insert
into inputnama
values('"+nim+"','"+nama+"','"+semester+"','"+mtKul+"','"+nBobot+"','"+nMtKul+"','"+jml+"')";
state.executeUpdate(sql);
}
catch (SQLException ex){
System.err.println("SQLException:"+ex.getMessage());
} // TODO add your handling code here:
}
if(obj == btnKeluar){
System.exit(0);
}
if(obj ==btnNew){
TextNIM.setText("");;
TextNama.setText("");;
TextMtKul.setText("");;
TextBobot.setText("");;
TextNilai.setText("");;
}
}
}
public static void main(String args[]){
Form tampil = new Form();
}
}
TextNama.setText("");;
TextMtKul.setText("");;
TextBobot.setText("");;
TextNilai.setText("");;
}
}
}
public static void main(String args[]){
Form tampil = new Form();
}
}
- Simpan dan jalankan program dengan Shift + F6