Created
March 25, 2012 16:38
-
-
Save resarahadian/2198143 to your computer and use it in GitHub Desktop.
Aplikasi Java dengan Database SQLite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @ author Resa C.R | |
*/ | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.sql.*; | |
import javax.swing.table.DefaultTableModel; | |
import javax.swing.UIManager.*; | |
public class Identitas extends JFrame | |
{ | |
Container konten = getContentPane(); | |
private JLabel lblNama = new JLabel("Nama : "); | |
private JTextField txtNama = new JTextField(); | |
private JLabel lblAlamat = new JLabel("Alamat : "); | |
private JTextField txtAlamat = new JTextField(); | |
private JLabel lblEmail = new JLabel("Email : "); | |
private JTextField txtEmail = new JTextField(); | |
private JLabel lblGambar = new JLabel(new ImageIcon("src/Data/gambar/sqlite.png")); | |
private JButton btnSimpan = new JButton("Simpan"); | |
private JButton btnUpdate = new JButton("Update"); | |
private JButton btnHapus = new JButton("Hapus"); | |
private JButton btnKeluar = new JButton("Keluar"); | |
private JButton btnRefresh = new JButton("Refresh"); | |
//Membuat Tabel | |
private DefaultTableModel tabelModel; | |
private JTable tabel = new JTable(); | |
String field[] = {"Nama","Alamat","Email"}; | |
private JScrollPane pane = new JScrollPane(); | |
//Konstruktor | |
public Identitas() | |
{ | |
super("Java dan SQLite Database"); | |
setSize(900,500); | |
setVisible(true); | |
setLocationRelativeTo(null); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
konten.setLayout(null); | |
lblNama.setBounds(10, 10, 75, 20); // int x,int y,int width, int height | |
konten.add(lblNama); | |
txtNama.setBounds(120, 10, 200, 25); | |
txtNama.setToolTipText("Masukkan Nama"); | |
txtNama.setForeground(Color.BLUE); | |
konten.add(txtNama); | |
lblAlamat.setBounds(10, 60, 75, 20); | |
konten.add(lblAlamat); | |
txtAlamat.setBounds(120, 60, 200, 25); | |
txtAlamat.setToolTipText("Masukkan Alamat"); | |
txtAlamat.setForeground(Color.BLUE); | |
konten.add(txtAlamat); | |
lblEmail.setBounds(10, 110, 75, 20); | |
konten.add(lblEmail); | |
txtEmail.setBounds(120, 110, 120, 25); | |
txtEmail.setToolTipText("Masukkan alamat Email (Optional)"); | |
txtEmail.setForeground(Color.BLUE); | |
konten.add(txtEmail); | |
lblGambar.setBounds(500,300,150,150); | |
konten.add(lblGambar); | |
tabelModel = new DefaultTableModel(null,field); | |
tabel.setModel(tabelModel); | |
tabel.setEnabled(true); | |
pane.getViewport().add(tabel); | |
pane.setBounds(380, 30, 400, 250); | |
konten.add(pane); | |
btnSimpan.setBounds(50, 200, 100, 25); | |
konten.add(btnSimpan); | |
btnUpdate.setBounds(200, 200, 100, 25); | |
konten.add(btnUpdate); | |
btnHapus.setBounds(50, 240, 100, 25); | |
konten.add(btnHapus); | |
btnRefresh.setBounds(200, 240, 100, 25); | |
konten.add(btnRefresh); | |
btnKeluar.setBounds(800, 430, 75, 25); | |
konten.add(btnKeluar); | |
//Memberikan aksi ketika tombol simpan diklik | |
btnSimpan.addActionListener(new ActionListener() | |
{ | |
@Override | |
public void actionPerformed(ActionEvent act) | |
{ | |
// TODO Auto-generated method stub | |
Object obj = act.getSource(); | |
if(obj == btnSimpan) | |
{ | |
try | |
{ | |
Class.forName("org.sqlite.JDBC").newInstance(); | |
String url = "jdbc:sqlite:Data"; | |
Connection koneksi = DriverManager.getConnection(url); | |
String sql = "insert into Identitas values(?,?,?)"; | |
PreparedStatement prepare = koneksi.prepareStatement(sql); | |
prepare.setString(1,txtNama.getText()); | |
prepare.setString(2, txtAlamat.getText()); | |
prepare.setString(3, txtEmail.getText()); | |
prepare.executeUpdate(); | |
JOptionPane.showMessageDialog(null,"Data berhasil ditambahkan ke dalam database","Pesan",JOptionPane.INFORMATION_MESSAGE); | |
prepare.close(); | |
} | |
catch(Exception ex) | |
{ | |
JOptionPane.showMessageDialog(null,"Data gagal ditambhakan ke dalam database","Pesan",JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
} | |
}); | |
//Memberikan aksi ketika tombol Update diklik | |
btnUpdate.addActionListener(new ActionListener() | |
{ | |
@Override | |
public void actionPerformed(ActionEvent act) | |
{ | |
// TODO Auto-generated method stub | |
Object obj = act.getSource(); | |
if(obj == btnUpdate) | |
{ | |
try | |
{ | |
Class.forName("org.sqlite.JDBC").newInstance(); | |
String url = "jdbc:sqlite:Data"; | |
Connection koneksi = DriverManager.getConnection(url); | |
String sql = "update Identitas set Alamat = ?,Email = ? where Nama = ?"; | |
PreparedStatement prepare = koneksi.prepareStatement(sql); | |
prepare.setString(1,txtAlamat.getText()); | |
prepare.setString(2,txtEmail.getText()); | |
prepare.setString(3,txtNama.getText()); | |
prepare.executeUpdate(); | |
JOptionPane.showMessageDialog(null,"Data berhasil diupdate","Pesan",JOptionPane.INFORMATION_MESSAGE); | |
prepare.close(); | |
} | |
catch(Exception ex) | |
{ | |
JOptionPane.showMessageDialog(null,"Data gagal diupdate","Pesan",JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
} | |
}); | |
//Memberikan aksi ketika tombol Refresh diklik | |
btnRefresh.addActionListener(new ActionListener() | |
{ | |
@Override | |
public void actionPerformed(ActionEvent act) | |
{ | |
// TODO Auto-generated method stub | |
Object obj = act.getSource(); | |
if(obj == btnRefresh) | |
{ | |
refresh(); | |
} | |
} | |
}); | |
//Memberikan aksi ketika tombol Hapus diklik | |
btnHapus.addActionListener(new ActionListener() | |
{ | |
@Override | |
public void actionPerformed(ActionEvent act) | |
{ | |
// TODO Auto-generated method stub | |
Object obj = act.getSource(); | |
if(obj == btnHapus) | |
{ | |
int a = tabel.getSelectedRow(); | |
if(a == -1) | |
{ | |
//tidak ada data terseleksi | |
return; | |
} | |
String Nama = (String) tabelModel.getValueAt(a,0); | |
try | |
{ | |
Class.forName("org.sqlite.JDBC").newInstance(); | |
String url = "jdbc:sqlite:Data"; | |
Connection koneksi = DriverManager.getConnection(url); | |
String sql = "delete from Identitas where Nama = ? "; | |
PreparedStatement prepare = koneksi.prepareStatement(sql); | |
prepare.setString(1,Nama); | |
prepare.executeUpdate(); | |
JOptionPane.showMessageDialog(null,"Data berhasil dihapus","Pesan",JOptionPane.INFORMATION_MESSAGE); | |
prepare.close(); | |
} | |
catch(Exception ex) | |
{ | |
JOptionPane.showMessageDialog(null,"Data gagal dihapus","Pesan",JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
} | |
}); | |
//Memberikan aksi ketika TextFiled Nama dienter | |
txtNama.addActionListener(new ActionListener() | |
{ | |
@Override | |
public void actionPerformed(ActionEvent act) | |
{ | |
// TODO Auto-generated method stub | |
Object obj = act.getSource(); | |
if(obj == txtNama) | |
{ | |
try | |
{ | |
Class.forName("org.sqlite.JDBC").newInstance(); | |
String url = "jdbc:sqlite:Data"; | |
Connection koneksi = DriverManager.getConnection(url); | |
Statement state = koneksi.createStatement(); | |
String sql = "select * from Identitas where Nama = '"+txtNama.getText()+"'"; | |
ResultSet rs = state.executeQuery(sql); | |
if(rs.next()) | |
{ | |
txtAlamat.setText(rs.getString("Alamat")); | |
txtEmail.setText(rs.getString("Email")); | |
} | |
rs.close(); | |
koneksi.close(); | |
} | |
catch(Exception ex) | |
{ | |
JOptionPane.showMessageDialog(null,"Data tidak ditemukan","Pesan",JOptionPane.ERROR_MESSAGE); | |
} | |
finally | |
{ | |
tampilTabel(); | |
btnSimpan.setEnabled(false); | |
btnHapus.setEnabled(true); | |
btnUpdate.setEnabled(true); | |
btnRefresh.setEnabled(true); | |
} | |
} | |
} | |
} ); | |
//Menampilkan data tabel ke TextField | |
tabel.addMouseListener(new MouseAdapter() | |
{ | |
public void mouseClicked(MouseEvent me) | |
{ | |
int a = tabel.getSelectedRow(); | |
if(a == -1) | |
{ | |
return; | |
} | |
String Nama = (String) tabelModel.getValueAt(a,0); | |
txtNama.setText(Nama); | |
String Alamat = (String) tabelModel.getValueAt(a,1); | |
txtAlamat.setText(Alamat); | |
String Email = (String) tabelModel.getValueAt(a,2); | |
txtEmail.setText(Email); | |
} | |
}); | |
//Memberikan aksi ketika tombol Keluar diklik | |
btnKeluar.addActionListener(new ActionListener() | |
{ | |
@Override | |
public void actionPerformed(ActionEvent act) | |
{ | |
// TODO Auto-generated method stub | |
Object obj = act.getSource(); | |
if(obj == btnKeluar) | |
{ | |
JOptionPane.showMessageDialog(null,"Anda telah keluar dari program","Pesan",JOptionPane.WARNING_MESSAGE); | |
System.exit(0); | |
} | |
} | |
}); | |
}//Akhir Konstruktor | |
public void tampilTabel() | |
{ | |
//Menampilkan data ke dalam tabel | |
try | |
{ | |
Class.forName("org.sqlite.JDBC").newInstance(); | |
String url = "jdbc:sqlite:Data"; | |
Connection koneksi = DriverManager.getConnection(url); | |
Statement state = koneksi.createStatement(); | |
String sql = "select * from Identitas where Nama = '"+txtNama.getText()+"'"; | |
ResultSet rs = state.executeQuery(sql); | |
while(rs.next()) | |
{ | |
String Nama = rs.getString("Nama"); | |
String Alamat = rs.getString("Alamat"); | |
String Email = rs.getString("Email"); | |
String Data[] = {Nama,Alamat,Email}; | |
tabelModel.addRow(Data); | |
} | |
rs.close(); | |
koneksi.close(); | |
} | |
catch(Exception ex) | |
{ | |
} | |
} | |
//Method membersihkan isi tabel | |
public void hapusIsiTabel() | |
{ | |
int baris = tabelModel.getRowCount(); | |
int a; | |
for(a=0;a<baris;a++) | |
{ | |
tabelModel.removeRow(0); | |
} | |
} | |
public void refresh() | |
{ | |
hapusIsiTabel(); | |
txtNama.setText(""); | |
txtAlamat.setText(""); | |
txtEmail.setText(""); | |
btnSimpan.setEnabled(true); | |
btnRefresh.setEnabled(true); | |
btnHapus.setEnabled(false); | |
btnKeluar.setEnabled(true); | |
btnUpdate.setEnabled(false); | |
txtNama.requestFocus(); | |
} | |
public static void main(String[] ar) | |
{ | |
//Membuat Look and Feel dengan Java Nimbus | |
try{ | |
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); | |
} | |
catch (UnsupportedLookAndFeelException e) | |
{ | |
} | |
catch (ClassNotFoundException e) | |
{ | |
} | |
catch (InstantiationException e) | |
{ | |
} | |
catch (IllegalAccessException e) | |
{ | |
} | |
new Identitas(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment