• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Populating a TableView's TableRows with data from a ResultSet  RSS feed

 
Ranch Hand
Posts: 47
1
IntelliJ IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've made some decent progress on this app so far, but I'm stuck again. When you click the connect button, given you have selected a database type from the ComboBox and have supplied a url in the TextField, it will access the database, and through a series of SQL queries, it will generate a TableView on a Tab within the TabPane for each table found in the database, then populate the TableViews with TableColumns with the corresponding names from the database, then (and here is where I am stuck) I want to query the database for all the data within each column and populate each TableColumn individually. As for now, this third while loop is generating rows for each result in the ResultSet returned from the query.


Specifically I am here...


Here is what the app looks like now after having connected to the db.


and here is a shot of the contents of the LineItems table in another application.


I notice that this final ResultSet "columnData" actually contains the data for the entire table, because it generates 32 rows, when it should only generate 8. Super lost here, any help would be greatly appreciated.
 
Master Rancher
Posts: 4076
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you probably ought to log what is coming out from all these queries.
That will help you debug it and (hopefully) see what is actually going on.
Also log the queries.
 
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric Arnold wrote:I want to query the database for all the data within each column and populate each TableColumn individually.


I'm having trouble visualizing this.  SQL is row based, not column based.  Do you mean display all the columns in a row?  Also, a TableView is driven by an object per row, normally (although I don't think you're going to be able to do that).

I haven't looked at your code yet, so let me do that and maybe it will become clearer.
 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, this isn't going to be easy.

First, there's no way that I know of just to populate one column of a multi-column TableView, so you will need to finish your table header loop and start a new loop for the rows.  Next, you have to figure out how to get table column values into the cells without a row value object.  So normally, if your table row represents a person, you would create a Person class to hold the data.  You can't do that, because you don't know what the data are until you start the header loop.  I don't know of a way to construct a Java class on-the-fly, so you (and possibly me) need to figure out how to get values into columns without an class.

I will keep researching, but start looking into how you're going to create this TableView and data model.
 
Eric Arnold
Ranch Hand
Posts: 47
1
IntelliJ IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Made a few changes. I think your correct Snortum, in that I am not going to be able to use a column based approach. And Tolls, I have implemented some Console logging for the queries and ResultSets.


The next step as far as I can see would be to query the database for all the values and add those values to the TableView, something like...

 
Dave Tolls
Master Rancher
Posts: 4076
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would argue the next step is to breakdown this method.
There's at least two methods, more like three, in there.

The first (which is the overall method) would be something like createTableTabs.
That would hold the table query and the tableNames loop.
The second would be something like createTableTab, which takes the code inside that while loop.
Finally the third method would be addColumn, which would hold all the code inside the columnNames while loop.

At least as a rough guess.

Then you can add your new populateTable method, which would be the code inside that new for loop.

Just to say, if I was writing this I would probably have broken it down even more, but the above would make reading this all a little easier.

ETA:  As for that bit of code, this line:

should be just:

as the for-each loop provides you with that.
 
Rancher
Posts: 1170
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way would be to create an wrapperclass. Put you're result in an observable list and use this list to populate your table.
 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric: have you gotten this to work yet?  I'm curious if you do, because my research seems to tells me this is very hard.  The problem I see with DD's solution is that it uses a PropertyValueFactory, and that relies on getting the properties from the class of the Table row.  But you don't have a class, because you can't predict what types the SQL ResultSet are going to contain.

I think your table row items will look like this:
This assumes all your results are type String, so then you convert them into whatever type you need using rs.getMetaData().getColumnType(index), where rs is a ResultSet and index is the index of your column.  From there I think you need a giant switch on the column types, converting the String result with an SQL type into a Java type, then putting your converted data manually into your items variable.  One loop for setting up column types, then one loop for writing the columns inside a loop for the rows.

At the end of all this, you would plop the items into the TableView:
 
Eric Arnold
Ranch Hand
Posts: 47
1
IntelliJ IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, so we're almost done getting this thing functional. I've made some changes in the Controller and to design of the fxml. So first I'll just show the code and explain the last remaining bug that needs to be corrected.

Controller.java


and the new design


So, when I run this thing with line 99 commented out, some magic happens. It starts building the first table correctly, columns correctly, and even gets the rows data logged to console correctly. But,
When I run it with line 99, which sets the data in the TableView, it thows an ArrayIndexOutOfBoundsException on line 86. So there's something going on with that int j.

I have uploaded this project to GitHub, anyone and everyone's welcome to contribute, or just check it out.
 
Eric Arnold
Ranch Hand
Posts: 47
1
IntelliJ IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction, it throws an IndexOutOfBoundsException, here are the details.

What's even stranger is that it is thrown twice....
 
Eric Arnold
Ranch Hand
Posts: 47
1
IntelliJ IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And done, thanks to Marcus Kuhn's reply to this thread here. So, without further adieu...


proofs in the pudding...



This application still has a long way to go, but the tables populated, this thread's [SOLVED] !
 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you're treating every column as if it's a String.  That works for the data you have, but I would encourage you to add a date and a price (double) column and see if you're happy with the display.
 
Eric Arnold
Ranch Hand
Posts: 47
1
IntelliJ IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that may be possible. I'm thinking with an extra sql query and a few conditional statements, we can determine the columns data type, and then during the TableColumn instantiation loop, have some conditional statements that will create a TableColumn of that type using generics. I'm back to work this week, and also studying for the OCP so I might have to shelf this for a few days, but I think you have a good idea, I'll look into it.
 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Determining the SQL type is easy: rs.getMetaData.getColumnType(i); where rs is the ResultSet and i is the index to the column.
 
Eric Arnold
Ranch Hand
Posts: 47
1
IntelliJ IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum, done and done



of course in the future we will need more than just Integer, Double, and String types, but this example demonstrates the concept, and it compiles, so...
 
Eric Arnold
Ranch Hand
Posts: 47
1
IntelliJ IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aye Radder, I posted a duplicate thread on java-fourms just to see which fourm would provide more feedback. CodeRanch won ;)
 
Knute Snortum
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

...duplicate thread on java-fourms...


This is fine on CodeRanch only if you inform both sites what you have done.
 
Eric Arnold
Ranch Hand
Posts: 47
1
IntelliJ IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, sorry I was unaware that was a no-no. Thanks for the heads-up.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!