Berikut tampilan program untuk Data Mahasiswa :
Dan berikut listingnya :
package koneksi;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
*
* @author m4rry
*/
public class mahasiswa extends javax.swing.JFrame {
/** Creates new form mahasiswa */
public mahasiswa() {
initComponents();
}
/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txtNim = new javax.swing.JTextField();
btCari = new javax.swing.JButton();
txtNama = new javax.swing.JTextField();
txtAlamat = new javax.swing.JTextField();
btTambah = new javax.swing.JButton();
btSimpan = new javax.swing.JButton();
btEdit = new javax.swing.JButton();
btHapus = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("NIM :");
jLabel2.setText("NAMA :");
jLabel3.setText("ALAMAT :");
btCari.setText("Cari");
btCari.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btCariActionPerformed(evt);
}
});
btTambah.setText("Tambah");
btTambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btTambahActionPerformed(evt);
}
});
btSimpan.setText("Simpan");
btSimpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSimpanActionPerformed(evt);
}
});
btEdit.setText("Edit");
btEdit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btEditActionPerformed(evt);
}
});
btHapus.setText("Hapus");
btHapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btHapusActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btTambah)
.addGap(18, 18, 18)
.addComponent(btSimpan))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(txtAlamat))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(txtNama))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(txtNim, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(btCari))))
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addComponent(btEdit)
.addGap(18, 18, 18)
.addComponent(btHapus)))
.addContainerGap(116, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtNim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btCari))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addGroup(layout.createSequentialGroup()
.addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtAlamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btTambah)
.addComponent(btSimpan))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btEdit)
.addComponent(btHapus))
.addContainerGap(105, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btTambahActionPerformed(java.awt.event.ActionEvent evt) {
txtNim.setText("");
txtNama.setText("");
txtAlamat.setText("");
txtNim.setEditable(true);
txtNama.setEditable(true);
txtAlamat.setEditable(true);
txtNim.requestFocus();
}
private void btSimpanActionPerformed(java.awt.event.ActionEvent evt) {
String nim = txtNim.getText();
String nama = txtNama.getText();
String alamat = txtAlamat.getText();
try
{
String dbname="jdbc:odbc:CobaDB";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
java.sql.Statement statement = con.createStatement();
String sql = "insert into mahasiswa_nim values('"+nim+"','"+nama+"','"+alamat+"');";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null,"Data berhasil dimasukan..","Insert Data", JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void btCariActionPerformed(java.awt.event.ActionEvent evt) {
String nim =txtNim.getText();
try
{
String dbname="jdbc:odbc:CobaDB";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
Statement statement = con.createStatement();
String sql = "SELECT * FROM mahasiswa_nim WHERE NIM like'"+nim+"'";
ResultSet rs = statement.executeQuery(sql);
if (rs.next())
{
txtNama.setText(rs.getString(2));
txtAlamat.setText(rs.getString(3));
JOptionPane.showMessageDialog(null,"Data ditemukan","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,"Data tidak ditemukan","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
statement.close();
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Error:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void btEditActionPerformed(java.awt.event.ActionEvent evt) {
String nim=txtNim.getText();
String nama=txtNama.getText();
String alamat=txtAlamat.getText();
try{
String dbname="jdbc:odbc:CobaDB";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
Statement statement = con.createStatement();
String sql = "UPDATE mahasiswa_nim SET NAMA='"+nama+"',ALAMAT='"+alamat+"'WHERE NIM LIKE'"+nim+"'";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null,"Data berhasil di Edit..","Edit Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch (Exception e){
JOptionPane.showMessageDialog(null,"Error:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void btHapusActionPerformed(java.awt.event.ActionEvent evt) {
String nim=txtNim.getText();
try{
String dbname="jdbc:odbc:CobaDB";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");
Statement statement = con.createStatement();
String sql = "DELETE FROM mahawiswa_nim WHERE NIM LIKE '"+nim+"'";
statement.executeUpdate(sql);
statement.close();
txtNim.setText("");
txtNama.setText("");
txtAlamat.setText("");
JOptionPane.showMessageDialog(null,"Data berhasil dihapus..","Hapus Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(mahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(mahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(mahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(mahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new mahasiswa().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btCari;
private javax.swing.JButton btEdit;
private javax.swing.JButton btHapus;
private javax.swing.JButton btSimpan;
private javax.swing.JButton btTambah;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField txtAlamat;
private javax.swing.JTextField txtNama;
private javax.swing.JTextField txtNim;
// End of variables declaration
}
Selamat Mencoba ye.........................
Tidak ada komentar:
Posting Komentar