Tuesday, May 8, 2012

How to display values in jTable in netbeans

1 comments

Now we take a look of how to display values in jTable using netbeans with source code.
  • First create a simple jFrame form an build GUI like this.
  • For building GUI don't bother about it, You have to just drag and drop the components from palette.
  • Once you completed GUI then we need to create database for storing values.
  • Use any database here is a sample database with table.
  • If you create database in Ms Access you must set the database connectivity.To set connectivity.
  • Control panel --> Administrative tools --> Data Source(ODBC) --> Add --> select Microsoft Access Driver(*.mdb,*.accdb)-->Finish --> Type database name in Data Source Name Field --> Select --> select location or path of database --> ok.
  • Now actual implementation code.
  • First crate Action class for getting dynamic database connection overall the project.
  package jdefaulttabledemo;  
 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.SQLException;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 public class Action {  
   public static Connection getDBConnection() {  
     Connection con = null;  
     try {  
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
       con = DriverManager.getConnection("jdbc:odbc:jTabelDemo");  
     } catch (SQLException ex) {  
       Logger.getLogger(Action.class.getName()).log(Level.SEVERE, null, ex);  
     } catch (ClassNotFoundException ex) {  
       Logger.getLogger(Action.class.getName()).log(Level.SEVERE, null, ex);  
     }  
     return con;  
   }  
 }  
  • Define two methods named setTable() and getdata().
  • Purpose of setTable() method is we use DefaultTableModel to give a dynamic look for jTable. Are you confused? Let me explain. Normally when we take jtable we go through jTable property to set row and column. Here by help of DefaultTableModel we not worry about setting rows and columns. Look at method then I explained in detail.
  private void setTable() {  
     dm = new DefaultTableModel(0, 0);  
     String s[] = new String[]{"Blogger Name", "Website Name"};  
     dm.setColumnIdentifiers(s);  
     jTable1.setModel(dm);  
   }  
  • String array is used to store all column name and columnIdentifier is used to set that all column name to jTable at run time.
  • Now take a look at getdata() method. It define for retrieving database values and set to jTabel.
  private void getdata() {  
     Connection con;  
       try {  
       con = Action.getDBConnection();  
       PreparedStatement pst = con.prepareStatement("select * from jtabel");  
       ResultSet rs = pst.executeQuery();  
       while (rs.next()) {  
         String bloggername = rs.getString(1);  
         String websitename = rs.getString(2);  
         Vector<String> vector = new Vector<String>();  
         //     vector.add(name);  
         vector.add(bloggername);  
         vector.add(websitename);  
         dm.addRow(vector);  
       }  
     } catch (SQLException ex) {  
       Logger.getLogger(JdefaluttableDemo.class.getName()).log(Level.SEVERE, null, ex);  
     }  
   }  
  • Event on save button.
  • To insert values in database, First open database connection and prepare sql query for inserting values in database.
  • To display values in jTable and for retrieving values, call setTable() and getdata() methods end of the query.
  • But First must call that methods in constructor to set DefaultTableModel to jTabel.
  • Code for save button.
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {  
     Connection con = Action.getDBConnection();  
     try {  
       PreparedStatement pst = con.prepareStatement("insert into jtabel values(?,?)");  
       pst.setString(1,jTextField1.getText());  
       pst.setString(2,jTextField2.getText());  
       pst.executeUpdate();  
       JOptionPane.showMessageDialog(this,"save successfully.");  
     } catch (SQLException ex) {  
       Logger.getLogger(JdefaluttableDemo.class.getName()).log(Level.SEVERE, null, ex);  
     }  
     setTable();  
     getdata();  
   }  
  • Finally see how to call methods in constructor and import statements.
 import java.sql.Connection;  
 import java.sql.PreparedStatement;  
 import java.sql.ResultSet;  
 import java.sql.SQLException;  
 import java.util.Vector;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 import javax.swing.JOptionPane;  
 import javax.swing.table.DefaultTableModel;  
 public class JdefaluttableDemo extends javax.swing.JFrame {  
   DefaultTableModel dm;  
   public JdefaluttableDemo() {  
     initComponents();  
     setTable();  
     getdata();  
   }  
  • Output of this code.

One Response so far

  1. beautiful, thank you sir!

Leave a Reply