Semua orang berubah. Kalau tidak, mereka mati sebelum bisa berubah.
-- Orochimaru.
Malam min nih ane mau Share Tugas Lagi kali ini Perograman Java Berbasis Database Langsung aja dah gak banyak Cing Congcong. Langsung Cekidottttttttt
Pertama Liat Outputnya dulu nih
Bahan Bahan Untuk Buat Databasenya
1. Software Navicat atau SQLyog
2. XAMPP
Instal Dah Tuh Sofwantrnya Setelah Itu Mulai Mengcreate Database Dan Buat Tabel Bernama Persegi_Panjang dengan 2 Kolom Panjang dan Lebar Isi Degan 20 Record, Angka
Nihh Penampakan Databasenya
Setelah Data Base Jadi Ente Masuk Ke Netbeans Dah Saatnya Ngoding Min
Nih Codingannya
import java.sql.*;
import javax.swing.*;
/**
*
* @author al dkidrebel
*/
public class Tugas_9 extends javax.swing.JFrame {
/**
* Creates new form Tugas_9
*/
final String drv = "com.mysql.jdbc.Driver";
final String jenisDB = "jdbc:mysql:";
final String namaDB = "//localhost/PersegiPanjang";
final String conDB = jenisDB + namaDB;
Connection con = null;
Statement stm = null;
String qry;
ResultSet rs;
void tampil() {
try {
txt1.setText(rs.getObject(1).toString());
txt2.setText(rs.getObject(2).toString());
String Masuk1 = txt1.getText();
String Masuk2 = txt2.getText();
Double x1 = Double.parseDouble(Masuk1);
Double x2 = Double.parseDouble(Masuk2);
txt3.setText("" + (x1 + x2) * 2);
txt4.setText("" + (x1 * x2));
} catch (SQLException sqlEx) {
JOptionPane.showMessageDialog(null, sqlEx.getMessage());
}
}
public Tugas_9() {
initComponents();
try {
Class.forName(drv);
con = DriverManager.getConnection(conDB, "root", "");
stm = con.createStatement();
qry = "select * from Persegi_Panjang";
rs = stm.executeQuery(qry);
rs.first();
tampil();
} catch (ClassNotFoundException cnf) {
JOptionPane.showMessageDialog(null, cnf.getMessage());
} catch (SQLException sqlEx) {
JOptionPane.showMessageDialog(null, sqlEx.getMessage());
}
}
/**
* 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();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txt1 = new javax.swing.JTextField();
txt2 = new javax.swing.JTextField();
txt3 = new javax.swing.JTextField();
txt4 = new javax.swing.JTextField();
btnfirst = new javax.swing.JButton();
btnvieaw = new javax.swing.JButton();
btnnext = new javax.swing.JButton();
btnlast = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("al d'kidrebel");
jLabel1.setText("Panjang");
jLabel2.setText("Lebar");
jLabel3.setText("Keliling");
jLabel4.setText("Luas");
btnfirst.setBackground(new java.awt.Color(51, 255, 255));
btnfirst.setText("First");
btnfirst.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnfirstActionPerformed(evt);
}
});
btnvieaw.setBackground(new java.awt.Color(255, 0, 0));
btnvieaw.setText("previous");
btnvieaw.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnvieawActionPerformed(evt);
}
});
btnnext.setBackground(new java.awt.Color(51, 255, 0));
btnnext.setText("Next");
btnnext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnnextActionPerformed(evt);
}
});
btnlast.setBackground(new java.awt.Color(255, 0, 204));
btnlast.setText("Last");
btnlast.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnlastActionPerformed(evt);
}
});
jLabel5.setText("cm");
jLabel6.setText("cm");
jLabel7.setText("cm");
jLabel8.setText("cm2");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(txt3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
.addComponent(txt2, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txt1, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txt4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jLabel6)
.addComponent(jLabel7)
.addComponent(jLabel8))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btnvieaw, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnnext, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnlast, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnfirst, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(42, 42, 42))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txt1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnfirst)
.addComponent(jLabel5))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnvieaw))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txt3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnnext)
.addComponent(jLabel7))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txt4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnlast)
.addComponent(jLabel8)))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jLabel6)))
.addContainerGap(34, Short.MAX_VALUE))
);
pack();
}//
private void btnfirstActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
rs.first();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
tampil();
}
private void btnvieawActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
rs.previous();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
tampil();
}
private void btnnextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
rs.next();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
tampil();
}
private void btnlastActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
rs.last();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
tampil();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* 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(Tugas_9.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Tugas_9.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Tugas_9.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Tugas_9.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Tugas_9().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnfirst;
private javax.swing.JButton btnlast;
private javax.swing.JButton btnnext;
private javax.swing.JButton btnvieaw;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JTextField txt1;
private javax.swing.JTextField txt2;
private javax.swing.JTextField txt3;
private javax.swing.JTextField txt4;
// End of variables declaration
}
Wihhh Banyak Dan Rumit Min di Coba aja Dulu Min Perhatikan Tulisan berwarna Pink Min
1. PersegiPanjang Nah Ni Nama Databasenya
2. "select * from Persegi_Panjang"; Kalau Yang ini Nama Table yang ane Buat di Databasenya
3. Tlisan Yang Warna Merah Untuk Pungsi Ke 4 Buttonnya, Min
from
http://greenz-blogger.blogspot.com/