Link to home
Start Free TrialLog in
Avatar of levyuk
levyuk

asked on

Dynamically creating jtables

Hi,

I have a problem, hence me being here, I would like to create an application that can connect to a database, retrive the data and then display this data in a table.

I have two problems, the first is, how do I display this data in a table, and the 2nd is how do I change the amount of columns in the table to reflect the ammount of columns in the database table?

ANy assistance would be great.

Jon
Avatar of fave_17
fave_17

You must get ResultSet from database connection after executing Statement.execQuery, from that ResultSet You get all the data You need - column count & names, the databse data... Then you make a JTable just like that:
=============
import java.awt.*;
import javax.swing.*;

public class B extends JFrame {
  B() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = getContentPane();
    cp.add(new JTable(5,4));
    setSize(300,300);
    setVisible(true);
  }
  public static void main(String[] args)
  {
    B b = new B();
  }
}
===========
and depending on the data You manage to get from ResultSet and ResultSetMetaData fill in the table.

Good luck.
ASKER CERTIFIED SOLUTION
Avatar of maheshexp
maheshexp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial