This time we will learn how to fill data JTabel from database use Netbeans IDE . Ok check this out.
Bahan
- Bikin Database (kita gunakan mysql).download ( rename menjadi books.sql kemudian create database dengan nama books kemudian Import sql ini)
- JFrame dangan jtabel.
- Bikin kelas untuk set model pada JTabel.download (rename menjadi KelasModel.java)
Ingredient
- Build database (we use mysql database).download (rename become books.sql then Create Databases books last import this sql)
- JFrame with JTabel.
- Make a class for to set Model for JTabel.download (rename become KelasModel.java)
Struktur Tabel
Table structure
CREATE TABLE IF NOT EXISTS `buku` (
`id` int(11) NOT NULL,
`ISBN` varchar(14) DEFAULT NULL,
`judul` varchar(60) NOT NULL,
`tahun` date NOT NULL,
`id_penerbit` int(11) NOT NULL,
`penulis` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Hasil Tampilan
Result
Ok bahan siap.....
Ok we’re ready to go
Bikin project dengan nama Belajar Isi JTabel
Build some project with name Belajar Isi JTabel
Tambahkan kelas “kelasModel” yang sudah di download (copi paste aja ke project)
Add the class “kelasModel” to our project that have been downloaded before (just copy it into our project)
Tambahkan JFrame
Add JFrame
Tambahkan JTabel cara nambahinnya cari JTabel di kolom Pallete Netbeans sebelah kiri klik dan drag JTabelnya ke arah Form kita (jika Pallete tidak muncul klik menu Window | Pallete or cntrl+shift+8 )
Add JTabel ,search JTabel in left Netbeans collum and drag JTabel into JFrame (if pallete windows not show click menu Window | Pallete or cntrl+shift+8 )
Buka source JFrame
Open JFrame source
Cari kode ini
Search This Code
class InsertingJtabel extends javax.swing.JFrame {
Setelah itu tambahkan :
Then Add:
Connection con = null;
Statement stmt = null;
public ResultSet getResultData() {
ResultSet rs = null;
try{
rs = stmt.executeQuery("SELECT * FROM buku");
}
catch(SQLException e) {
System.err.println("Error SQL: " + e.getMessage());
}
return rs;
}
public void closeResultSet(ResultSet rs){
if (rs != null) {
try{
rs.close();
}
catch(SQLException e) {
System.err.println("Error SQL: " + e.getMessage());
}
}
}
(di konstruktornya) Kemudian cari kode public InsertingJtabel() {
Tambahkan ini sebelum initComponents();
(on konstruktor) Search the code public InsertingJtabel() {
Add this before initComponents();
// Loading driver MySQL/J
try {
Class.forName("org.gjt.mm.mysql.Driver");
}
catch (ClassNotFoundException e) {
System.err.println("Error loading driver: " + e.getMessage());
}
try {
// Mendirikan koneksi database MySQL
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/books",
"root", "");
// Statement SQL statik
stmt = con.createStatement();
}
catch (SQLException e) {
System.err.println("Error SQL: " + e.getMessage());
}
Ok kode siap saatnya setting pada JTabel
Oc the code ready time to set the JTabel
Klik kanan JTabel pilih CodeCustomizer kemudian pilih defaultcode nya kita ganti dengan Custom Property
Right Click JTabel select CodeCustomizer then change Defaut Code with Custom Property
Tambahkan
Add
ResultSet rs = getResultData();
jTable1.setModel(new kelasModel(rs));
closeResultSet(rs);
Selanjutny jalankan
Next run
Kelihatannya aja panjang tapi ntar implementasinya cuman sekelumit …smoga berguna
Its looks so Much code but in implementation just in short time
Download source (build with netbeans 6.8)