Contoh Program: insert.java
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.sql.*;
public class insert implements ActionListener {
JLabel labelNama = new JLabel(“nama:”);
JTextField fieldNama = new JTextField(10);
JLabel labelNim = new JLabel(“nim:”);
JTextField fieldNim = new JTextField(10);
JLabel labelProdi = new JLabel(“prodi:”);
JTextField fieldProdi = new JTextField(10);
JLabel labelAlamat = new JLabel(“alamat:”);
JTextField fieldAlamat = new JTextField(10);
JLabel labelKota = new JLabel(“kota:”);
JTextField fieldKota = new JTextField(10);
JButton tombolSimpan = new JButton(“simpan”);
JButton tombolBatal = new JButton(“batal”);
JPanel panel = new JPanel();
insert(){
GridLayout gl = new GridLayout(6,2);
panel.setLayout(gl);
panel.add(labelNim);
panel.add(fieldNim);
panel.add(labelNama);
panel.add(fieldNama);
panel.add(labelProdi);
panel.add(fieldProdi);
panel.add(labelAlamat);
panel.add(fieldAlamat);
panel.add(labelKota);
panel.add(fieldKota);
panel.add(tombolSimpan);
panel.add(tombolBatal);
tombolSimpan.addActionListener(this);
tombolBatal.addActionListener(this);
JFrame frame = new JFrame(“input data”);
frame.setContentPane(panel);
frame.setSize(400,200);
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
public void actionPerformed(ActionEvent event) {
try {
if(event.getSource()==tombolSimpan) {
String nama = fieldNama.getText();
String nim = fieldNim.getText();
String prodi = fieldProdi.getText();
String alamat = fieldAlamat.getText();
String kota = fieldKota.getText();
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
“root”,”t3l0g0dh0g”);
System.out.println(“koneksi sukses…”);
Statement statement = koneksi.createStatement();
String sql = “INSERT INTO mahasiswa values (‘”+nim+”‘,’”+nama+”‘,’”+prodi+”‘,’”+alamat+”‘,’”+kota+”‘);”;
statement.executeUpdate(sql);
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
statement.close();
koneksi.close();
JOptionPane.showMessageDialog(panel, “data sukses tersimpan…”);
}
}
catch(Exception e) {
System.out.println(“salah”);
}
if(event.getSource()==tombolBatal){
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
}
}
public static void main(String[] args) {
insert insertRecord = new insert();
}
}
Output program:
tampilan program insert.java
Contoh program: update.java
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.sql.*;
public class update implements ActionListener {
JTextField fieldCari = new JTextField(10);
JButton tombolCari = new JButton(“cari”);
JLabel labelNama = new JLabel(“nama:”);
JTextField fieldNama = new JTextField(10);
JLabel labelNim = new JLabel(“nim:”);
JTextField fieldNim = new JTextField(10);
JLabel labelProdi = new JLabel(“prodi:”);
JTextField fieldProdi = new JTextField(10);
JLabel labelAlamat = new JLabel(“alamat:”);
JTextField fieldAlamat = new JTextField(10);
JLabel labelKota = new JLabel(“kota:”);
JTextField fieldKota = new JTextField(10);
JButton tombolUpdate = new JButton(“update”);
JButton tombolBatal = new JButton(“batal”);
JPanel panel = new JPanel();
update(){
GridLayout gl = new GridLayout(7,2);
panel.setLayout(gl);
panel.add(fieldCari);
panel.add(tombolCari);
panel.add(labelNim);
panel.add(fieldNim);
panel.add(labelNama);
panel.add(fieldNama);
panel.add(labelProdi);
panel.add(fieldProdi);
panel.add(labelAlamat);
panel.add(fieldAlamat);
panel.add(labelKota);
panel.add(fieldKota);
panel.add(tombolUpdate);
panel.add(tombolBatal);
tombolCari.addActionListener(this);
tombolUpdate.addActionListener(this);
tombolBatal.addActionListener(this);
JFrame frame = new JFrame(“update data”);
frame.setContentPane(panel);
frame.setSize(400,200);
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
public void actionPerformed(ActionEvent event) {
try {
if(event.getSource() == tombolUpdate) {
String nim = fieldNim.getText();
String nama = fieldNama.getText();
String prodi = fieldProdi.getText();
String alamat = fieldAlamat.getText();
String kota = fieldKota.getText();
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
“root”,”t3l0g0dh0g”);
System.out.println(“koneksi sukses…”);
Statement statement = koneksi.createStatement();
String sql= “UPDATE mahasiswa set nama=’”+nama+”‘,prodi=’”+prodi+”‘,alamat=’”+alamat+”‘,kota=’”+kota+”‘ WHERE nim=’”+nim+”‘”;
statement.executeUpdate(sql);
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
statement.close();
koneksi.close();
JOptionPane.showMessageDialog(panel, “data telah ter-update…”);
}
else if(event.getSource() == tombolBatal) {
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
}
else if(event.getSource() == tombolCari) {
String cari = fieldCari.getText();
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
“root”,”t3l0g0dh0g”);
System.out.println(“koneksi sukses…”);
Statement statement = koneksi.createStatement();
String sql = “SELECT * FROM mahasiswa WHERE nim LIKE ‘”+cari+”‘”;
ResultSet rs = statement.executeQuery(sql);
if(rs.next()) {
fieldNim.setText(rs.getString(1));
fieldNama.setText(rs.getString(2));
fieldProdi.setText(rs.getString(3));
fieldAlamat.setText(rs.getString(4));
fieldKota.setText(rs.getString(5));
} else {
JOptionPane.showMessageDialog(panel, “nim ” + cari + ” tidak ditemukan”);
}
statement.close();
koneksi.close();
}
}
catch(Exception e) {
System.out.println(“terjadi kesalahan”);
}
}
public static void main(String[] args) {
update deleteRecord = new update();
}
}
Output program:
tampilan program update.java
Contoh program: delete.java
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.sql.*;
public class delete implements ActionListener {
JTextField fieldCari = new JTextField(10);
JButton tombolCari = new JButton(“cari”);
JLabel labelNama = new JLabel(“nama:”);
JTextField fieldNama = new JTextField(10);
JLabel labelNim = new JLabel(“nim:”);
JTextField fieldNim = new JTextField(10);
JLabel labelProdi = new JLabel(“prodi:”);
JTextField fieldProdi = new JTextField(10);
JLabel labelAlamat = new JLabel(“alamat:”);
JTextField fieldAlamat = new JTextField(10);
JLabel labelKota = new JLabel(“kota:”);
JTextField fieldKota = new JTextField(10);
JButton tombolHapus = new JButton(“hapus”);
JButton tombolBatal = new JButton(“batal”);
JPanel panel = new JPanel();
delete(){
GridLayout gl = new GridLayout(7,2);
panel.setLayout(gl);
panel.add(fieldCari);
panel.add(tombolCari);
panel.add(labelNim);
panel.add(fieldNim);
panel.add(labelNama);
panel.add(fieldNama);
panel.add(labelProdi);
panel.add(fieldProdi);
panel.add(labelAlamat);
panel.add(fieldAlamat);
panel.add(labelKota);
panel.add(fieldKota);
panel.add(tombolHapus);
panel.add(tombolBatal);
tombolCari.addActionListener(this);
tombolHapus.addActionListener(this);
tombolBatal.addActionListener(this);
JFrame frame = new JFrame(“delete data”);
frame.setContentPane(panel);
frame.setSize(400,200);
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
public void actionPerformed(ActionEvent event) {
try {
if(event.getSource() == tombolHapus) {
String nim = fieldNim.getText();
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
“root”,”t3l0g0dh0g”);
System.out.println(“koneksi sukses…”);
Statement statement = koneksi.createStatement();
String sql = “DELETE FROM mahasiswa WHERE nim=’”+nim+”‘”;
statement.executeUpdate(sql);
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
statement.close();
koneksi.close();
JOptionPane.showMessageDialog(panel, “data telah terhapus…”);
}
else if(event.getSource() == tombolBatal) {
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
}
else if(event.getSource() == tombolCari) {
String cari = fieldCari.getText();
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
“root”,”t3l0g0dh0g”);
System.out.println(“koneksi sukses…”);
Statement statement = koneksi.createStatement();
String sql = “SELECT * FROM mahasiswa WHERE nim LIKE ‘”+cari+”‘”;
ResultSet rs = statement.executeQuery(sql);
if(rs.next()) {
fieldNim.setText(rs.getString(1));
fieldNama.setText(rs.getString(2));
fieldProdi.setText(rs.getString(3));
fieldAlamat.setText(rs.getString(4));
fieldKota.setText(rs.getString(5));
} else {
JOptionPane.showMessageDialog(panel, “nim ” + cari + ” tidak ditemukan”);
}
statement.close();
koneksi.close();
}
}
catch(Exception e) {
System.out.println(“terjadi kesalahan”);
}
}
public static void main(String[] args) {
delete deleteRecord = new delete();
}
}
Output program:
tampilan program delete.java
Contoh program: gabungan insert, delete, dan hapus (gabung.java)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
public class gabung implements ActionListener {
JMenuBar menu = new JMenuBar();
JMenu menuFile = new JMenu(“File”);
JMenuItem subMenuInsert = new JMenuItem(“Insert Data”,’S’);
JMenuItem subMenuDelete = new JMenuItem(“Delete Data”,’D’);
JMenuItem subMenuUpdate = new JMenuItem(“Update Data”,’U’);
JMenuItem subMenuQuit = new JMenuItem(“Quit”,’Q’);
Container container = new Container();
JPanel panel = new JPanel();
gabung() {
JFrame frame = new JFrame(“Java DataBase Manajemen Sistem”);
frame.setSize(640,480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(menu);
menuFile.setMnemonic(‘F’);
subMenuInsert.addActionListener(this);
subMenuDelete.addActionListener(this);
subMenuUpdate.addActionListener(this);
subMenuQuit.addActionListener(this);
menu.add(menuFile);
menuFile.add(subMenuInsert);
menuFile.add(subMenuDelete);
menuFile.add(subMenuUpdate);
menuFile.addSeparator();
menuFile.add(subMenuQuit);
menu.setSize(400,100);
menu.setLocation(60,60);
menu.setVisible(true);
FlowLayout fl = new FlowLayout();
panel.setLayout(fl);
frame.setContentPane(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
gabung gabungan = new gabung();
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == subMenuInsert) {
frameInsert();
} else if(event.getSource() == subMenuDelete) {
frameDelete();
} else if(event.getSource() == subMenuUpdate) {
frameUpdate();
} else if (event.getSource() == subMenuQuit) {
System.exit(0);
}
}
private void frameInsert() {
insert frameInsert = new insert();
}
private void frameDelete() {
delete frameDelete = new delete();
}
private void frameUpdate() {
update frameUpdate = new update();
}
}
Output program:
Tampilan program gabungan.java