Pada tahan pelajaran ke - 8, kita akan belajar menampilkan data dari database Sqllite, Edit dan delete.
Buat tampilan seperti dibawah ini :
Code : ( tambahkan event masing - masing componen dan class )
Connection p_connect;
Statement st;
ResultSet rs;
DefaultTableModel model = new DefaultTableModel();
Statement st;
ResultSet rs;
DefaultTableModel model = new DefaultTableModel();
p_update();
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
p_bersihkanText();
p_tampil();
}
private void txtkodeKeyTyped(java.awt.event.KeyEvent evt) {
char c = evt.getKeyChar();
if (txtkode.getText().length() >= 10){
evt.consume();
}
if (c == KeyEvent.VK_ENTER){
txtnama.nextFocus();
}
}
private void txtnamaKeyTyped(java.awt.event.KeyEvent evt) {
char c = evt.getKeyChar();
if (txtnama.getText().length() >= 150){
evt.consume();
}
if (c == KeyEvent.VK_ENTER){
txtqty.nextFocus();
}
}
private void txtqtyKeyTyped(java.awt.event.KeyEvent evt) {
char c = evt.getKeyChar();
if (txtkode.getText().length() >= 10 || !Character.isDigit(evt.getKeyChar())){
evt.consume();
}
if (c == KeyEvent.VK_ENTER){
txtqty.nextFocus();
}
}
private void btncancelActionPerformed(java.awt.event.ActionEvent evt) {
p_bersihkanText();
}
private void btngetActionPerformed(java.awt.event.ActionEvent evt) {
String kode;
String nama;
String qty;
kode = tblbarang.getValueAt(tblbarang.getSelectedRow(), 0).toString();
nama = tblbarang.getValueAt(tblbarang.getSelectedRow(), 1).toString();
qty = tblbarang.getValueAt(tblbarang.getSelectedRow(), 2).toString();
txtkode.setText(kode);
txtnama.setText(nama);
txtqty.setText(qty);
}
private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {
p_delete();
}
private void p_bersihkanText(){
txtkode.setText("");
txtnama.setText("");
txtqty.setText("");
}
@SuppressWarnings({"UnusedAssignment", "RedundantStringToString"})
private void p_update(){
String pesan = "";
String sqlinsert = "";
String kode = "";
String nama = "";
int qty = 0;
kode = txtkode.getText().toString();
nama = txtnama.getText().toString();
qty = Integer.parseInt(txtqty.getText().toString());
try{
Class.forName("org.sqlite.JDBC");
String url = "jdbc:sqlite:d://tokoku.db";
p_connect = DriverManager.getConnection(url);
st = p_connect.createStatement();
sqlinsert = "update barang set nama = '" + nama + "', qty = " + qty + " where kode = '" + kode + "'";
st.execute(sqlinsert);
st.close();
p_connect.close();
JOptionPane.showMessageDialog(rootPane, "data tersimpan");
p_bersihkanText();
}catch(Exception e) {
pesan = " gagal koneksi ke database ";
}
p_tampil();
}
private void p_delete(){
int p_jawab = 0;
p_jawab = JOptionPane.showConfirmDialog(rootPane, "Anda yakin ingin menghapus data ini", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if ( p_jawab == 0){
p_hapus();
}
}
@SuppressWarnings("UnusedAssignment")
private void p_hapus(){
String sqldelete = "";
String kode = "";
kode = tblbarang.getValueAt(tblbarang.getSelectedRow(), 0).toString();
try{
Class.forName("org.sqlite.JDBC");
String url = "jdbc:sqlite:d://tokoku.db";
p_connect = DriverManager.getConnection(url);
st = p_connect.createStatement();
sqldelete = "delete from barang where kode = '" + kode + "'";
st.execute(sqldelete);
st.close();
p_connect.close();
//
JOptionPane.showMessageDialog(rootPane, "data terhapus");
model.removeRow(tblbarang.getSelectedRow());
}catch(Exception e) {
}
}
private void p_tampil(){
String sql = "select * from barang";
try{
Class.forName("org.sqlite.JDBC");
String url = "jdbc:sqlite:d://tokoku.db";
p_connect = DriverManager.getConnection(url);
st = p_connect.createStatement();
rs = st.executeQuery(sql);
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector
while (rs.next()) {
Vector
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
model = new DefaultTableModel(data, columnNames);
tblbarang.setModel(model);
// tblbarang.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tblbarang.getColumnModel().getColumn(0).setPreferredWidth(100);
tblbarang.getColumnModel().getColumn(1).setPreferredWidth(250);
rs.close();
st.close();
p_connect.close();
}catch(Exception e) {
}
}
Hasil