CARA MELOAD DATA DARI DATABASE KE TABLE
di sini adalah contoh untuk menampilkan data dari database ke sebuah table,jika anda mau copas silahkan,tapi sebelumnya anda buat ODBC driver nya dulu.....langkah-langkah nya sudah pernah saya posting,silakan di cari sendiri..dan ini saya sertakan juga link download database nya :
http://bitshare.com/files/x2pmvegt/gaji.zip.html
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* viewtable.java
*
* Created on 08 Mei 12, 20:39:06
*/
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
*
* @author matt
*/
public class viewtable extends javax.swing.JFrame {
String nama,alamat,pekerjaan,gaji,tunjangan,bonus;
private final DefaultTableModel model;
/** Creates new form viewtable */
public viewtable() {
initComponents();
model = new DefaultTableModel();
tab.setModel(model);
model.addColumn("nama");
model.addColumn("alamat");
model.addColumn("pekerjaan");
model.addColumn("gaji");
model.addColumn("tunjangan");
model.addColumn("bonus");
}
/** 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">//GEN-BEGIN:initComponents
private void initComponents() {
ok = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tab = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
ok.setText(org.openide.util.NbBundle.getMessage(viewtable.class, "viewtable.ok.text")); // NOI18N
ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okActionPerformed(evt);
}
});
tab.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(tab);
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\matt\\Documents\\NetBeansProjects\\GUIFormExamples\\src\\logo-oracle.png")); // NOI18N
jLabel1.setText(org.openide.util.NbBundle.getMessage(viewtable.class, "viewtable.jLabel1.text")); // NOI18N
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.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1))
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
.addGroup(layout.createSequentialGroup()
.addGap(253, 253, 253)
.addComponent(ok, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addComponent(ok, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
try {
String dbname="jdbc:odbc:gaji";//nama odbc
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");//user & pasword
Statement statement = con.createStatement();
String sql = "select nama,alamat,pekerjaan,gaji,tunjangan,bonus from gaji order by gaji ";
ResultSet rs = statement.executeQuery(sql);
while(rs.next())
{
String namax = rs.getString("nama");
String alamatx = rs.getString("alamat");
String pekerjaanx = rs.getString("pekerjaan");
String gajix = rs.getString("gaji");
String tunjanganx = rs.getString("tunjangan");
String bonusx = rs.getString("bonus");
String [] data = {namax,alamatx,pekerjaanx,gajix,tunjanganx,bonusx};
model.addRow(data);
}
rs.close();
con.close();
}catch(Exception x)
{
JOptionPane.showMessageDialog(null,"error:"+x,"gagal",JOptionPane.WARNING_MESSAGE);
// TODO add your handling code here:
}
// TODO add your handling code here:
}//GEN-LAST:event_okActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new viewtable().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton ok;
private javax.swing.JTable tab;
// End of variables declaration//GEN-END:variables
}
http://bitshare.com/files/x2pmvegt/gaji.zip.html
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* viewtable.java
*
* Created on 08 Mei 12, 20:39:06
*/
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
*
* @author matt
*/
public class viewtable extends javax.swing.JFrame {
String nama,alamat,pekerjaan,gaji,tunjangan,bonus;
private final DefaultTableModel model;
/** Creates new form viewtable */
public viewtable() {
initComponents();
model = new DefaultTableModel();
tab.setModel(model);
model.addColumn("nama");
model.addColumn("alamat");
model.addColumn("pekerjaan");
model.addColumn("gaji");
model.addColumn("tunjangan");
model.addColumn("bonus");
}
/** 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">//GEN-BEGIN:initComponents
private void initComponents() {
ok = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tab = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
ok.setText(org.openide.util.NbBundle.getMessage(viewtable.class, "viewtable.ok.text")); // NOI18N
ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okActionPerformed(evt);
}
});
tab.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(tab);
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\matt\\Documents\\NetBeansProjects\\GUIFormExamples\\src\\logo-oracle.png")); // NOI18N
jLabel1.setText(org.openide.util.NbBundle.getMessage(viewtable.class, "viewtable.jLabel1.text")); // NOI18N
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.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1))
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
.addGroup(layout.createSequentialGroup()
.addGap(253, 253, 253)
.addComponent(ok, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addComponent(ok, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
try {
String dbname="jdbc:odbc:gaji";//nama odbc
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");//user & pasword
Statement statement = con.createStatement();
String sql = "select nama,alamat,pekerjaan,gaji,tunjangan,bonus from gaji order by gaji ";
ResultSet rs = statement.executeQuery(sql);
while(rs.next())
{
String namax = rs.getString("nama");
String alamatx = rs.getString("alamat");
String pekerjaanx = rs.getString("pekerjaan");
String gajix = rs.getString("gaji");
String tunjanganx = rs.getString("tunjangan");
String bonusx = rs.getString("bonus");
String [] data = {namax,alamatx,pekerjaanx,gajix,tunjanganx,bonusx};
model.addRow(data);
}
rs.close();
con.close();
}catch(Exception x)
{
JOptionPane.showMessageDialog(null,"error:"+x,"gagal",JOptionPane.WARNING_MESSAGE);
// TODO add your handling code here:
}
// TODO add your handling code here:
}//GEN-LAST:event_okActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new viewtable().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton ok;
private javax.swing.JTable tab;
// End of variables declaration//GEN-END:variables
}
Komentar ini telah dihapus oleh administrator blog.
BalasHapusKomentar ini telah dihapus oleh pengarang.
BalasHapus