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



Home Answers Viewqa Swing-AWT JTable populate with resultset.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


How to diplay data of resultset using JTable?


Advertisement
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
pre-populate form
pre-populate form  how to pre populate form in php   what do you mean by pre populate
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
JTable
", ""); Statement st=con.createStatement(); ResultSet rs..."}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane
populate Combo Box dynamically
populate Combo Box dynamically  Hi, How to populate the Combo Box dynamically using Flex with Jsp's
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
multiple resultset in one resultset
multiple resultset in one resultset  how to retrive multiple resultsets in one resultset in java.? plz help
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
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
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
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
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
 

 

 

DMCA.com