JTable populate with resultset.

JTable populate with resultset.

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 {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            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++) {
                columnNames.addElement(metaData.getColumnName(i));
            }
            while (resultSet.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    row.addElement(resultSet.getObject(i));
                }
                data.addElement(row);
            }
            resultSet.close();
            statement.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        JTable table = new JTable(data, columnNames);
        TableColumn column;
        for (int i = 0; i < table.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            column.setMaxWidth(250);
        }
        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
ResultSet   What is a ResultSet
Advertisements
ResultSet
ResultSet  What is a ResultSet
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
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
JTable
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
JTable
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
JTable  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
JTable
", ""); Statement st=con.createStatement(); ResultSet rs..."}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
jtable
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
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
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
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
jtable
/** * Creates new form jtable1 */ ResultSet rs3; ResultSet rs1, rs; Connection con; Statement st; ResultSet rs2; public jtable1_1() throws
multiple resultset in one resultset
multiple resultset in one resultset  how to retrive multiple resultsets in one resultset in java.? plz help
regarding JTable - JDBC
regarding JTable  how to populate a JTable with mysql data after clicking JButton please explain with the example
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
ModuleNotFoundError: No module named 'populate'
ModuleNotFoundError: No module named 'populate'  Hi, My Python... 'populate' How to remove the ModuleNotFoundError: No module named 'populate... to install padas library. You can install populate python with following
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
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
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
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
resultset metadta
resultset metadta  import java.sql.Connection; import... Employee Details Using resultset metadata: "); Connection con = null...(); ResultSet rs = st.executeQuery("SELECT * FROM employee
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
ModuleNotFoundError: No module named 'satellite_populate'
ModuleNotFoundError: No module named 'satellite_populate'  Hi, My... named 'satellite_populate' How to remove the ModuleNotFoundError: No module named 'satellite_populate' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-populate'
ModuleNotFoundError: No module named 'django-populate'  Hi, My... named 'django-populate' How to remove the ModuleNotFoundError: No module named 'django-populate' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'foreman-populate'
ModuleNotFoundError: No module named 'foreman-populate'  Hi, My... named 'foreman-populate' How to remove the ModuleNotFoundError: No module named 'foreman-populate' error? Thanks   Hi, In your
REPORT WITH JTABLE
JTableDatabase extends JFrame{ ResultSet rs; JTableDatabase(){ final...(); ResultSet rs= st.executeQuery("Select * from employee...(Exception e){} JTable table = new JTable(data, columnNames); JScrollPane scrollPane
JTable - JDBC
JTable   Hello..... I have Jtable with four rows and columns and i have also entered some records in MsSql database. i want to increase Jtable's... to use ResultSet Metadata in it. please help me to retrieve my code. and remember
jtable problem
jtable problem  how to make a cell text hypertext
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
JTABLE OF JAVA
JTABLE OF JAVA  i have a jtable in java,i have used checkbox in jtable. now i want to add(submit) only those records that i have checked by checkbox how? i want small example with coding
Jtable-Java
Jtable-Java  Hi all,I have a Jtable And i need to clear the data in the table .I only Need to remove the data in the table.not the rows.Please help me
Swings JTable
Swings JTable  add values to JTable with four coloums,two of them are comboboxes
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
JTABLE Issue
JTABLE Issue  Hi Eveyone, I am developing a small application on Swing-AWT. I have used JTABLE to show data. There is "input field" and "search... on basis of input data provided in input field. For JTABLE is on some other
jtable query
jtable query  I need a syntax...where i could fetch the whole data from the database once i click the cell in jtable...and that must be displayed in the nearby text field which i have set in the same frame
java jtable
java jtable  Hello Sir, I am developing a desktop application in which i have to display database records in jtable .now I want to read only... that in jtable. plz help me with the code
ABOUT Jtable
ABOUT Jtable  My Project is Exsice Management in java swing Desktop Application. I M Use Netbeans & Mysql . How can retrive Data in Jtable from Mysql Database in Net Beans
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
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
ModuleNotFoundError: No module named 'odoo10-addon-users-ldap-populate'
ModuleNotFoundError: No module named 'odoo10-addon-users-ldap-populate' ...: ModuleNotFoundError: No module named 'odoo10-addon-users-ldap-populate' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-users-ldap-populate'
ModuleNotFoundError: No module named 'odoo10-addon-users-ldap-populate'
ModuleNotFoundError: No module named 'odoo10-addon-users-ldap-populate' ...: ModuleNotFoundError: No module named 'odoo10-addon-users-ldap-populate' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-users-ldap-populate'
ModuleNotFoundError: No module named 'odoo12-addon-users-ldap-populate'
ModuleNotFoundError: No module named 'odoo12-addon-users-ldap-populate' ...: ModuleNotFoundError: No module named 'odoo12-addon-users-ldap-populate' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-users-ldap-populate'
ModuleNotFoundError: No module named 'odoo8-addon-users-ldap-populate'
ModuleNotFoundError: No module named 'odoo8-addon-users-ldap-populate' ...: ModuleNotFoundError: No module named 'odoo8-addon-users-ldap-populate' How to remove the ModuleNotFoundError: No module named 'odoo8-addon-users-ldap-populate' error
ModuleNotFoundError: No module named 'odoo9-addon-users-ldap-populate'
ModuleNotFoundError: No module named 'odoo9-addon-users-ldap-populate' ...: ModuleNotFoundError: No module named 'odoo9-addon-users-ldap-populate' How to remove the ModuleNotFoundError: No module named 'odoo9-addon-users-ldap-populate' error
JTable duplicate values in row
JTable duplicate values in row  JTable duplicate values in row

Ads