package db_app;
/**
*
* @author zunaidi
*/
public class person {
private int id;
private String nama;
private String alamat;
private String telp;
public person() {
}
public int getid(){
return id;
}
public void setid(int id)
{
this.id=id;
}
public String getnama()
{
return nama;
}
public void setnama(String nama)
{
this.nama=nama;
}
public String getalamat()
{
return alamat;
}
public void setalamat(String alamat)
{
this.alamat=alamat;
}
public String gettelp()
{
return telp;
}
public void settelp(String telp)
{
this.telp=telp;
}
}
Person Manager
package db_app;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author zunaidi
*/
public class PersonManager {
Connection con = null;
Statement st = null;
String url = "jdbc:mysql://localhost:3306/dbapp_11092095";
String user = "root";
String pass = "password";
public PersonManager(){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url,user,pass);
st = con.createStatement();
}
catch(Exception ex){
ex.printStackTrace();
}}
public List
getperson(){
ResultSet rs = null;
List
persons = new ArrayList();
try{
rs = st.executeQuery("SELECT id,nama,alamat,telp FROM tperson");
while(rs.next()){
person p = new person();
p.setid(rs.getInt(1));
p.setnama(rs.getString(2));
p.setalamat(rs.getString(3));
p.settelp(rs.getString(4));
persons.add(p);
}}catch(Exception ex){
ex.printStackTrace();
}
return persons;
}
public int insert(person p){
int result = 0;
try{
result = st.executeUpdate("INSERT INTO tperson(id,nama,alamat,telp)"+
"VALUES("+p.getid()+","+p.getnama()+","+p.getalamat()+","+p.gettelp()+")");
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
public int delete(person p){
int result = 0;
try{
result = st.executeUpdate("DELETE FROM tperson WHERE ID="+p.getid()+"");
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
My Window
package db_app;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
/**
*
* @author zunaidi
*/
public class MyWindow extends javax.swing.JFrame {
PersonManager pmgr = new PersonManager();
List
int currentRow=0;
private void loadData(){
persons=pmgr.getperson();}
private void bindData(){
if(persons.size()>0){
person p = persons.get(currentRow);
txtId.setText(p.getid()+"");
txtNama.setText(p.getnama());
txtAlamat.setText(p.getalamat());
txtTelp.setText(p.gettelp());
}
else{
currentRow=0;
txtId.setText("");
txtNama.setText("");
txtAlamat.setText("");
txtTelp.setText("");
}
}
/** Creates new form MyWindow */
public MyWindow() {
initComponents();
loadData();
bindData();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtId = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
txtNama = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
txtAlamat = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
txtTelp = new javax.swing.JTextField();
btnPrev = new javax.swing.JButton();
btnNext = new javax.swing.JButton();
btnNew = new javax.swing.JButton();
btnSave = new javax.swing.JButton();
btnHapus = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Data Person");
jLabel2.setText("ID :");
txtId.setEditable(false);
jLabel3.setText("Nama :");
txtNama.setEditable(false);
jLabel4.setText("Alamat :");
txtAlamat.setEditable(false);
jLabel5.setText("Telp :");
txtTelp.setEditable(false);
btnPrev.setText("<<");
btnPrev.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPrevActionPerformed(evt);
}
});
btnNext.setText(">>");
btnNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNextActionPerformed(evt);
}
});
btnNew.setText("Baru");
btnNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNewActionPerformed(evt);
}
});
btnSave.setText("Simpan");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
btnHapus.setText("Hapus");
btnHapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnHapusActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(102, 102, 102)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(txtNama, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
.addComponent(txtAlamat, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
.addComponent(txtId, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
.addComponent(txtTelp, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(btnNew)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnSave)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnHapus)))))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addContainerGap(332, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(btnPrev)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnNext)
.addContainerGap(255, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtAlamat, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(txtTelp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnPrev)
.addComponent(btnNext)
.addComponent(btnNew)
.addComponent(btnSave)
.addComponent(btnHapus))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}//
private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txtId.setText("");
txtNama.setText("");
txtAlamat.setText("");
txtTelp.setText("");
txtId.setEditable(true);
txtNama.setEditable(true);
txtAlamat.setEditable(true);
txtTelp.setEditable(true);
txtId.requestFocus();
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
person p = new person();
p.setid(Integer.parseInt(txtId.getText()));
p.setnama(txtNama.getText());
p.setalamat(txtAlamat.getText());
p.settelp(txtTelp.getText());
if(pmgr.insert(p)>0){
loadData();
currentRow=persons.size()-1;
bindData();
JOptionPane.showMessageDialog(this, "Data berhasil disimpan","Informasi",JOptionPane.INFORMATION_MESSAGE);
txtId.setEditable(false);
txtNama.setEditable(false);
txtAlamat.setEditable(false);
txtTelp.setEditable(false);
}else{
JOptionPane.showMessageDialog(this, "Data gagal disimpan","Informasi",JOptionPane.INFORMATION_MESSAGE);
}
}
private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
person p=persons.get(currentRow);
if(pmgr.delete(p)>0){
loadData();
currentRow=currentRow-1;
bindData();
JOptionPane.showMessageDialog(this, "Data berhasil dihapus","Informasi",JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(this, "Data gagal dihapus","Informasi",JOptionPane.INFORMATION_MESSAGE);
}
}
private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(currentRow>0){
--currentRow;
}
bindData();
}
private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(currentRow>0){
++currentRow;
}
bindData();
}