ratna rathor
JTable populate with resultset.
1 Answer(s)      4 years and 9 months ago
Posted in : Swing AWT

How to diplay data of resultset using JTable?


View Answers

May 28, 2012 at 6:34 PM

JTable is component of java swing toolkit. JTable class is helpful in displaying data in tabular format. You can also edit data. JTable provide view of data, stored in database, file or in some object. It does not contain data. You can define your table as ?

JTable table=new JTable(data,columneName);

JTable constructors-

  1. JTable(Object[][] data, Object[] columnNames)
  2. JTable(Vector data, Vector columneNames)

Here is a JTable populate with resultset example ?

import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableColumn;

public class JTableResultSet {
    public static void main(String[] args) {
        Vector columnNames = new Vector();
        Vector data = new Vector();
        JPanel panel = new JPanel();   //
        try {
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "root");
            String sql = "Select name,address from user";
            Statement statement = con.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columns = metaData.getColumnCount();
            for (int i = 1; i <= columns; i++) {
            while (resultSet.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
        } catch (Exception e) {
        JTable table = new JTable(data, columnNames);
        TableColumn column;
        for (int i = 0; i < table.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
        JScrollPane scrollPane = new JScrollPane(table);        panel.add(scrollPane);               
        JFrame frame = new JFrame();
        frame.add(panel);         //adding panel to the frame
        frame.setSize(600, 400); //setting frame size
        frame.setVisible(true);  //setting visibility true

Description: - Vector class allows you to implement dynamic array of objects. That is the size of vector can grow or shrink as data is added or deleted.

JPanel is light weighted container used for general purpose. By default, panels have only their background. If can set colors and borders. test is database and user is table in test. First create connection to database.

ResultSetMetaData is used here to take information about the types and properties of columns in resultset object.

JTable table = new JTable(data, columnNames); //Creating object of JTable JscrollPane is used for providing the facility of scrolling.It is light weight component of swing.

JScrollPane scrollPane = new JScrollPane(table) //Here we are adding facility of scrolling into our table.

Related Tutorials/Questions & Answers:
JTable populate with resultset.
JTable populate with resultset.  How to diplay data of resultset...) JTable(Vector data, Vector columneNames) Here is a JTable populate with resultset... of columns in resultset object. JTable table = new JTable(data, columnNames
ResultSet   What is a ResultSet
ResultSet  What is a ResultSet
ResultSet  I want to retrieve data of unknown datatype from database.how to do it using resultset or Resultsetmetadata.Can you please tell me what... = con.createStatement(); ResultSet rs = st.executeQuery
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
JTable   how to select a definite cell which containing a similar text containg to the one which the user entering from a jtable at runtime in java
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
pre-populate form
pre-populate form  how to pre populate form in php   what do you mean by pre populate
populate Combo Box dynamically
populate Combo Box dynamically  Hi, How to populate the Combo Box dynamically using Flex with Jsp's
JTable  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
multiple resultset in one resultset
multiple resultset in one resultset  how to retrive multiple resultsets in one resultset in java.? plz help
", ""); Statement st=con.createStatement(); ResultSet rs..."}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane
Populate value into HTML page
Populate value into HTML page  Hi, How to populate values from one html page table to another html page..? Thanks
jtable  how to get the values from database into jtable and also add a checkbox into it and then when selected the checkbox it should again insert into database the selected chewckbox.plzz help
jtable  hi Sir i am working netbeans IDE,I have a jtable when i insert values in jtable then i am unable to print all inserted values,For eg if i insert 1,2,3,4,5,6,7,8 values then , i am getting output
jtable  hey i have build a form and i m also able to add data from database to jtable along with checkbox.the only problem is that if i select multiple checkboxes the data doesnt get inserted into new database and if only one
JTable  Hi I have problems in setting values to a cell in Jtable which is in a jFrame which implements TableModelListener which has a abstract method tableChanged(TableModelEvent e) . I'll be loading values from data base when
JSP Populate - JSP-Servlet
parameters and at the same time want to use that parameter to populate the same XYZ jsp from database. How do I populate this XYZ jsp after hitting that URL (it needs to call java/servelt to populate that jsp.. my question is how?)  Hi
/** * Creates new form jtable1 */ ResultSet rs3; ResultSet rs1, rs; Connection con; Statement st; ResultSet rs2; public jtable1_1() throws
populate dropdown box
populate dropdown box  hi, Is there any ways in which i can populate my dropdown menu with values from 1 to n (n = the value stored in database)using java script or jsp, either is fine? Ex: If value corresponding to selected
regarding JTable - JDBC
regarding JTable  how to populate a JTable with mysql data after clicking JButton please explain with the example
resultset metadta
resultset metadta  import java.sql.Connection; import... Employee Details Using resultset metadata: "); Connection con = null...(); ResultSet rs = st.executeQuery("SELECT * FROM employee
regarding jtable...
regarding jtable...  sir, im working with jtables. i wanted to populate a jtable from the database and when i click any row it should add a container... a container on the jtable. kindly help me sir. thanks in advance regards, rajahari
Resultset with one to many relationship
populate my bean classes from resultset without using hibernate, so...Resultset with one to many relationship   Suppose there are 5 tables... not able to populate bean classes properly.Please suggest easy and convinient
Populate Menus In Tree View - Struts
Populate Menus In Tree View  Hi all, i am writing one simple application using struts framework. In this application i thought to bring the different menus of my application in a tree view.Like what we have in window
The cursor in scrollable resultset
The cursor in scrollable resultset   How to move the cursor in scrollable resultset
Update a resultset programmatically
Update a resultset programmatically  How to update a resultset programmatically
Populate a combo box using data from a database
Populate a combo box using data from a database  Hi Guys, In need... to populate the first one, and then how to send the data off to a servlet and preform... to the client using ajax, and then populate the combo box, I must'nt reload
How to populate text fields using php code?
How to populate text fields using php code?  How could I use php to populate text fields by selecting a name of a business from dropdown list? Those text fields that will be populated by information in regards its company name
Resultset - JSP-Servlet
Resultset  I am writing while(rs.next){} but if resultset is null then it cannot enter in while block. I want,if resultset is null then it will enter into the while block.  Hi friend, A ResultSet object