Pemrograman Berorientasi Digital
Pemrograman Berorientasi Digital
Pemrograman Berorientasi Digital
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
- FormDosen
package Form;
import Config.Koneksi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public FormDosen() {
initComponents();
conn = Koneksi.getConnection();
getData();
nonAktifButton();
aktifButton();
}
private void getData() {
DefaultTableModel model =(DefaultTableModel) tbl_data.getModel();
model.setRowCount(0);
try {
String sql = "Select * From dosen";
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = st.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String nama = rs.getString("nama");
String telepon = rs.getString("telepon");
String alamat = rs.getString("alamat");
rs.close();
st.close();
} catch(Exception e) {
Logger.getLogger(FormDosen.class.getName()).log(Level.SEVERE, null, e);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Name");
t_telepon.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t_teleponActionPerformed(evt);
}
});
t_alamat.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t_alamatActionPerformed(evt);
}
});
t_nama.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t_namaActionPerformed(evt);
}
});
jLabel2.setText("Address");
jLabel3.setText("Number");
t_cari.setText("Search...");
t_cari.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
t_cariMouseClicked(evt);
}
});
t_cari.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
t_cariActionPerformed(evt);
}
});
t_cari.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
t_cariKeyTyped(evt);
}
});
btn_tambah.setText("ADD");
btn_tambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_tambahActionPerformed(evt);
}
});
btn_batal.setText("CANCEL");
btn_batal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_batalActionPerformed(evt);
}
});
btn_perbarui.setText("UPDATE");
btn_perbarui.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_perbaruiActionPerformed(evt);
}
});
btn_hapus.setText("DELETE");
btn_hapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_hapusActionPerformed(evt);
}
});
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LE
ADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LE
ADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(btn_tambah)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_batal)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_perbarui,
javax.swing.GroupLayout.PREFERRED_SIZE, 78,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn_hapus))
.addComponent(t_nama,
javax.swing.GroupLayout.PREFERRED_SIZE, 312,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(t_telepon,
javax.swing.GroupLayout.PREFERRED_SIZE, 312,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(t_alamat,
javax.swing.GroupLayout.PREFERRED_SIZE, 312,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(12, 12, 12)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LE
ADING, false)
.addComponent(t_cari)
.addComponent(jScrollPane1)))
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BA
SELINE)
.addComponent(t_nama, javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(t_cari, javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LE
ADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(t_telepon,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addGap(4, 4, 4)
.addComponent(t_alamat, javax.swing.GroupLayout.PREFERRED_SIZE,
42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BA
SELINE)
.addComponent(btn_tambah,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_batal,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_perbarui,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn_hapus,
javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 298,
javax.swing.GroupLayout.PREFERRED_SIZE)))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
try {
String sql = "INSERT INTO dosen (nama,telepon,alamat) VALUES (?,?,?)";
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, nama);
st.setString(2, telepon);
st.setString(3, alamat);
st.close();
}catch (Exception e) {
Logger.getLogger(FormDosen.class.getName()).log(Level.SEVERE, null, e);
}
}
try {
String sql = "UPDATE dosen SET nama=?,telepon=?,alamat=? WHERE id=?";
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, nama);
st.setString(2, telepon);
st.setString(3, alamat);
st.setString(4, id);
st.close();
}catch (Exception e) {
Logger.getLogger(FormDosen.class.getName()).log(Level.SEVERE, null, e);
}
}
t_nama.setText(nama);
t_telepon.setText(telepon);
t_alamat.setText(alamat);
}
btn_perbarui.setEnabled(true);
btn_tambah.setEnabled(false);
btn_hapus.setEnabled(true);
}
try {
String sql = "DELETE FROM dosen WHERE id=?";
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, id);
st.close();
}catch(Exception e) {
Logger.getLogger(FormDosen.class.getName()).log(Level.SEVERE, null, e);
}
}
resetForm();
getData();
aktifButton();
nonAktifButton();
try {
String sql = "Select * From dosen WHERE nama LIKE ? OR telepon LIKE ? OR
alamat LIKE ?";
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, "%" + cari + "%");
st.setString(2, "%" + cari + "%");
st.setString(3, "%" + cari + "%");
ResultSet rs = st.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String nama = rs.getString("nama");
String telepon = rs.getString("telepon");
String alamat = rs.getString("alamat");
rs.close();
st.close();
} catch(Exception e) {
Logger.getLogger(FormDosen.class.getName()).log(Level.SEVERE, null, e);
}
}
java.util.logging.Logger.getLogger(FormDosen.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FormDosen.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FormDosen.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FormDosen.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
}
//</editor-fold>
- Output
Create
Update
Delete