Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Answers Viewqa Swing-AWT JTable populate with resultset.

Have Programming Question? Ask it here!

ratna rathor
JTable populate with resultset.
1 Answer(s)      2 years and 3 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 ( {
                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 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
JTable  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
populate Combo Box dynamically
populate Combo Box dynamically  Hi, How to populate the Combo Box dynamically using Flex with Jsp's
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
", ""); Statement st=con.createStatement(); ResultSet rs..."}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane
multiple resultset in one resultset
multiple resultset in one resultset  how to retrive multiple resultsets in one resultset in java.? plz help
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
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  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
/** * Creates new form jtable1 */ ResultSet rs3; ResultSet rs1, rs; Connection con; Statement st; ResultSet rs2; public jtable1_1() throws
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
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({} 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
JTableDatabase extends JFrame{ ResultSet rs; JTableDatabase(){ final...(); ResultSet rs= st.executeQuery("Select * from employee...(Exception e){} JTable table = new JTable(data, columnNames); JScrollPane scrollPane
SQL exception, Exhausted ResultSet
SQL exception, Exhausted ResultSet  javax.servlet.ServletException: java.sql.SQLException: Exhausted Resultset iam getting this error messege whenever i run my code. what would be the possible reasons
jtable problem
jtable problem  how to make a cell text hypertext
resultset problem - JSP-Servlet
resultset problem  hi I have one problem with resultset? ResultSet rs=st.execute("select subjname,staffname from staffdetails"); while... is java.sql.SQLException: Operation not allowed after ResultSet closed how
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
How to populate collection like map with annotation in mybatis?
How to populate collection like map with annotation in mybatis?   <collection property="participantCas" column="agmtsID" javaType="java.util.ArrayList" ofType="com.Client"> <id property="partyId" column
Resultset Issue in SQL
Resultset Issue in SQL  when i call in my code it is returning false, though im having valid query to execute and fetch a row from can i resolve this issue. My code is: SELECT JOBDESCRIPTION,CATEGORY
large resultset values - SQL
large resultset values  i have one query which will bring more than one lakh records from database(total records are abt 3 crores). While getting the details i'm getting error like
JSP - Resultset - JSP-Servlet
JSP - Resultset  hello sir, how can i count record in jsp. i use resultset in my jsp so in my login form how can i check the username and password is correct or not...?? please reply it's urgent.. thanks
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  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
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
Swings JTable
Swings JTable  add values to JTable with four coloums,two of them are comboboxes
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
ResultSet In Java
ResultSet In Java In this section we will learn about the ResultSet in Java... that are mostly used in to get the value. ResultSet contains the data of a table after executing the SQL queries. Object of ResultSet also maintains the current position
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
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
I want to Populate ComBobox value from database that combox is Itemrenderer of DataGrid.
I want to Populate ComBobox value from database that combox is Itemrenderer of DataGrid.  I want to Populate ComBobox value from database that combox is Itemrenderer of DataGrid
Populate dropdown menu from database using jsp and servlet
Populate dropdown menu from database using jsp and servlet  please i need code to populate dropdown menu from mysql database using jsp and servlet. thanks