Linking an editable tableView to a database in javaFX - java

I'm working on a project for JavaFX and databasing practice (mostly I'm a beginner), where I have a unique class which holds records about concerts (name, location, date etc). I originally held these in an observable arraylist which populated a table view. From here the records could be editted, deleted or new ones added.
I am now storing these in a database using ORMLite and sqlite. This is so I can perform filtering on the data (ie show all events in a particular location) and then show this on the table.
My issue is that when I read in the data from the database I convert it to a ObservableArrayList so the table view can use it, but by creating the new array list my edit/new/delete buttons only effect this list and not the database. The problem is that every time I perform a query such as adding or deleting records to the database, it needs to re-produce the observableArrayList for the tableView which is taking around 5 seconds with ~250k records.
Is there a more efficient way to work with databases and javaFx tableViews?

Is there a more efficient way to work with databases and javaFx tableViews?
How about setting some sort of dirty flag on the records when you update certain fields and then go through your list of records and only make the DAO calls on those that are dirty? Maybe an enum with CREATE, DELETE, UPDATE, NONE.
Another idea would be to do your dao.update(...), dao.delete(...), or dao.create(...) calls whenever you update an record although you may want to do the chances all once time.
Last idea is to keep 4 arrays. One with the entire list, one for the new records, one for the dirty records, and one for the records to be deleted. You would add the records to the lists as edit/new/delete buttons are pressed and then at the end you can save them all at once.

Related

Performance of database call from JAVA to Database

Our team is building a small application wherein a UI has about 10 drop-down list boxes. ( DDLB ).
These list boxes will be populated by selecting data from different tables.
Our JAVA person feels that making separate database call for each list will be very expensive and wants to make a single database call for all lists.
I feel it is impractical to populate all lists in one database call due to following reason
a. Imagine an end user chooses state = 'NY' from one DDLB.
b. The next drop down should be populated with values from ZIP_CODES table for STATE='NY'
Unless we know ahead of time what state a user will be choosing - our only choice is to populate a java structure with all values from ZIP_CODES table. And after the user has selected the state - parse this structure for NY zipcodes.
And imagine doing this for all the DDLB in the form. This will not only be practical but also resource intensive.
Any thoughts ?
If there are not many items in those lists and memory amount allows you could load all values for all drop boxes into memory at application startup and then filter data in memory. It will be better then execute SQL query for every action user makes with those drop boxes.
You could also use some cache engines (like EhCache) that could offload data to disk and store only some fraction in memory.
You can run some timings to see, but I suspect you're sweating something that might take 100th of a second to execute. UI design wise I never put zip codes in selection menus because the list is too long and people already know it well enough to just punch in. When they leave the zip code field I will query the city and state and pre-fill those fields if they're not already set.

Scroll to Row with Pageable option in a List grid Smart gwt

I have a list grid which contains thousands of records. I can update any row in this list grid. After updating a row, I need to remain focus to the particular row.
From the RecordClickEvent, I take the index of the record. When I fetch the data again I've used below methods.
listGrid.selectRecord(recordIndex);
listGrid.scrollToRow(recordIndex);
If I update a record within first 75, it works nicely. How can I remain the focus, if I update a record not in first 75 records?
You must be doing something in your code that breaks this default behavior (of keeping focus on the edited record), because I have a paged editable ListGrid with close to 20.000 records, and when I update any record, I retain focus on the edited record without doing anything specific. Without looking at your code, it's not possible to know what can be wrong in your case, that focus is lost after updating one record.
Take a look at this particular demo for an example of what I mean regarding the default behavior.
By the way, what you are trying to do could be accomplished by loading your data page by page, until you get past the recordIndex, but take into consideration that this could be a very disconcerting user experience.

How to create an undo function in spring MVC based web app.?

I have an employee and a corresponding employee history table.
Both the tables have same structure. History table is used to track the historical changes made to the employee over a period of time.
Now, I need to add an undo function to the changes made to the employee.
e.g. Employees title is changed on 1st August. Now, This will update the employees title in Employee table and insert an corresponding history record in employee_history table.
Now, I need to undo this change. Employee edit page will have a list of changes made to employee datewise with an undo button beside it.
Clicking on undo should revert changes in Employee table to previous value. Also I think the record in history table which says title is changed, should also be removed.
Also when I revert tghe changes to employee table i.e. revert title to previous title, this will fire an insert to history table, which I dont want.
I am not sure what is the best possible way to do this.
Any suggestions will be helpful.
In case you want to implement a "persistent" undo - one that would survive an application restart/session timeout, you should consider extending your DB schema width timestamp fields and either delete the last entry or replace it with an appropriate earlier entry.
A "light" version would be using a stack to store last interactions, including the original and the new value. You could persist the stack on session invalidation of course to combine both approaches. This seems to be what you are actually doing.
You could extend this solution by creating and storing or exporting SQL migration scripts for each change, recording the change and, if possible, the opposite action. So you could even transfer the scripts between application instances and environments and would have a complete "replayability" of your DB states.
tl;dr - it looks like you have already implemented a good solution
I would suggest using a flag telling the trigger/history logic to keep off while you have your undo running and not writing history data.
Normally this would be done by serializer-class feeding from your history table and restoring employee data and later cleaning up history-entries/unlocking history again.
You could maybe use the rollback feature of the transaction.

Related to Auto-Update of JTable

I have two radiobuttons(Say rbtn_Asia,rbtn_Europe)and one JTable. When I select rbtn_Asia, table must contains Asia's data. Similarly when I select rbtn_Europe, table must contains Europe's data. (Asia's data and Europe's data is in same database which will be updated periodically). I have implemented upto this.
My problem is like this: Consider the following case: I have selected rbtn_Asia and obviously table will contain Asia's data. Now let database has got two new tuples of Asia, how can I update the JTable dynamically without selecting the rbtn_Asia once again (because rbtn_Asia is already in selected state).
In your button handler, update your implementation of TableModel, which should then fire the appropriate event. A structure that supports clear() such as Map, shown here, is convenient. More examples may be found here.

How to Iterate across records in a MySql Database using Java

I have a customer with a very small set of data and records that I'd normally just serialize to a data file and be done but they want to run extra reports and have expandability down the road to do things their own way. The MySQL database came up and so I'm adapting their Java POS (point of sale) system to work with it.
I've done this before and here was my approach in a nutshell for one of the tables, say Customers:
I setup a loop to store the primary key into an arraylist then setup a form to go from one record to the next running SQL queries based on the PK. The query would pull down the fname, lname, address, etc. and fill in the fields on the screen.
I thought it might be a little clunky running a SQL query each time they click Next. So I'm looking for another approach to this problem. Any help is appreciated! I don't need exact code or anything, just some concepts will do fine
Thanks!
I would say the solution you suggest yourself is not very good not only because you run SQL query every time a button is pressed, but also because you are iterating over primary keys, which probably are not sorted in any meaningful order...
What you want is to retrieve a certain number of records which are sorted sensibly (by first/last name or something) and keep them as a kind of cache in your ArrayList or something similar... This can be done quite easily with SQL. When the user starts iterating over the results by pressing "Next", you can in the background start loading more records.
The key to keep usability is to load some records before the user actually request them to keep latency small, but keeping in mind that you also don't want to load the whole database at once....
Take a look at indexing your database. http://www.informit.com/articles/article.aspx?p=377652
Use JPA with the built in Hibernate provider. If you are not familiar with one or both, then download NetBeans - it includes a very easy to follow tutorial you can use to get up to speed. Managing lists of objects is trivial with the new JPA and you won't find yourself reinventing the wheel.
the key concept here is pagination.
Let's say you set your page size to 10. This means you select 10 records from the database, in a certain order, so your query should have an order by clause and a limit clause at the end. You use this resultset to display the form while the users navigates with Previous/Next buttons.
When the user navigates out of the page then you fetch an other page.
https://www.google.com/search?q=java+sql+pagination

Categories