Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to display Database Table DATA in jTable  RSS feed

 
Bikash Megale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have a Swing GUI program where I have to display Database table data in jTable.

any advices would be appreciated..
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the first thing you need to do is to store your data in a two-dimensional array. (Otherwise known as an array of arrays). You also need to store your column headers in a one dimensional String[] array.

The following page is quite good at describing the setup.

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
 
Bikash Megale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:Well, the first thing you need to do is to store your data in a two-dimensional array. (Otherwise known as an array of arrays). You also need to store your column headers in a one dimensional String[] array.


How to store JDBC ResultSet data in two-dimensional array ?
thank you for your help
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the first thing you need to do is to store your data in a two-dimensional array.


Typically you don't know the number of rows in the table so you would create your DefaultTableModel using a Vector. You would populate the Vector with data from the ResultSet.

If you want to get fancy you you can check out Table From Database which uses a general purpose custom TableModel and does all the work for you.

But of course you should be reading the tutorial from above so you understand how the process.


 
Lei Guoguo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the jdbc api
DatabaseMetaData dbmd = conn.getMetaData();
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
megabi wrote:
Fred Hamilton wrote:Well, the first thing you need to do is to store your data in a two-dimensional array. (Otherwise known as an array of arrays). You also need to store your column headers in a one dimensional String[] array.


How to store JDBC ResultSet data in two-dimensional array ?
thank you for your help


Have a look at some of the methods in the ResultSet API.

If you start with a simple database with uniform datatypes and a known number of rows in your result set, it will be a little easier, if you are just learning and getting started with JDBC. You can then work your way up to more general ways of dealing with it. Usually I end up starting with a strategy that is less than optimal but easier to understand, and work my way towards a better solution that usually requires more advanced knowledge. and therefore more time to get something working.

The following link has a download of a large collection of sample code which I found very helpful.

http://java.sun.com/products/jdbc/reference/codesamples/index.html#1
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello megabi,

You probably missed our naming policy. Can you please change your display name accordingly?

Thank you.


Fred Hamilton wrote:Well, the first thing you need to do is to store your data in a two-dimensional array. (Otherwise known as an array of arrays). You also need to store your column headers in a one dimensional String[] array.

The two-dimensional array is not even necessary if you use a DefaultTableModel:
If you use a two-dimensional array with DefaultTableModel, that array is converted into a Vector of Vectors, and you end up with two two-dimensional structures. By doing it this way you only have the Vector of Vectors.

Of couse you can also define your own TableModel implementation (by subclassing AbstractTableModel), and have it use the two-dimensional array as storage. I would then still prefer a List<Object[]> instead of an Object[][] - because you don't know the number of records initially, and calling rs.last() and then rs.getRow() may be really slow. Plus you may not be able to go back after that if the result set only allows one-way traversal.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Hello megabi,

You probably missed our naming policy. Can you please change your display name accordingly?

Thank you.


Fred Hamilton wrote:Well, the first thing you need to do is to store your data in a two-dimensional array. (Otherwise known as an array of arrays). You also need to store your column headers in a one dimensional String[] array.

The two-dimensional array is not even necessary if you use a DefaultTableModel:
If you use a two-dimensional array with DefaultTableModel, that array is converted into a Vector of Vectors, and you end up with two two-dimensional structures. By doing it this way you only have the Vector of Vectors.

Of couse you can also define your own TableModel implementation (by subclassing AbstractTableModel), and have it use the two-dimensional array as storage. I would then still prefer a List<Object[]> instead of an Object[][] - because you don't know the number of records initially, and calling rs.last() and then rs.getRow() may be really slow. Plus you may not be able to go back after that if the result set only allows one-way traversal.


Learn something every day. Anyways clearly my statement "you need to store data in two dimensional array" was not accurate. I still say that working with a two dimensional array is intuitively easier for a beginner to understand, which makes it an appropriate place to get started, as long as one realizes that there are probably better, more powerful ways to learn about.
 
Bikash Megale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:
Rob Prime wrote:Hello megabi,

You probably missed our naming policy. Can you please change your display name accordingly?

Thank you.


Fred Hamilton wrote:Well, the first thing you need to do is to store your data in a two-dimensional array. (Otherwise known as an array of arrays). You also need to store your column headers in a one dimensional String[] array.

The two-dimensional array is not even necessary if you use a DefaultTableModel:
If you use a two-dimensional array with DefaultTableModel, that array is converted into a Vector of Vectors, and you end up with two two-dimensional structures. By doing it this way you only have the Vector of Vectors.

Of couse you can also define your own TableModel implementation (by subclassing AbstractTableModel), and have it use the two-dimensional array as storage. I would then still prefer a List<Object[]> instead of an Object[][] - because you don't know the number of records initially, and calling rs.last() and then rs.getRow() may be really slow. Plus you may not be able to go back after that if the result set only allows one-way traversal.


Learn something every day. Anyways clearly my statement "you need to store data in two dimensional array" was not accurate. I still say that working with a two dimensional array is intuitively easier for a beginner to understand, which makes it an appropriate place to get started, as long as one realizes that there are probably better, more powerful ways to learn about.







Hello everyone,

In my GUI program. I have two ComboBoxes, one for CategoryOption (StoreCode, ProductCode and ClassificationCode)
and another one for DateOption (Daily, Monthly).

I have to display data according to Selected Item in ComboBox.

I am going to give a scenerio, I want to get data StoreCode-wise from database StoreTable, data display output in jTABLE is in below format

Condition is
where StoreCode between 2 and 5
and SoldDate between '2009-06-26' and '2009-06-29'
*************************************************************
StoreCode StoreName SoldQuantity SoldAmount SoldDate
*************************************************************
2 store-1 100 1000 2009-06-29
3 store-2 200 2000 2009-06-29
4 store-3 300 3000 2009-06-29


I can get the selected item Options from both ComboBoxes. But I don't know how to retrive data using List<Object[]> and display the data in jTable. How can I get multiple rows in resultSet, return that resultSet data and display in jTable.

Any advice would be appreciated.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!