Select/Filter specific rows in jTable - java

My Task is to develop a Software which is measuring times of different participants.
Following problem:
each participant has 2 chances to give his/her best and the software has to select the best 32/16/.. times for the next round
The times are saved in a MySql DB, selected with Hibernate and saved in a Datatable using own Tablemodel (RunTableModel extends DefaultTableModel)
for example:
Participant | Time
------------|------
Tina | 10:15
Tina | 10:20
Carl | 11:00
Carl | 10:17
Charlie | 12:01
Charlie | 11:50
My question now is how to filter this results in a jTable.
-> One option would probably be to do this by selection from the Database (Hibernate HQL) but therefore I have to reload always from DB
Is there a possability to do this with filters?
Result should look like this:
Participant | Time
------------|------
Tina | 10:15
Carl | 10:17
Charlie | 11:50

Related

Postgres data with interval of 8 hours

I have a table with large number of rows. It has column like timestamp(in millis), value, and a siteId(foreign key). I want to fetch data from that of last three months with an interval in timestamp of 8 hours and I want to fetch data of all siteId in the three month timestamp. I have data in there for every 5 minutes of every siteId. If I fetch data of last three months, it is coming in millions. so I want to take data of every 8 hours. Sometimes, there can be a gap too so if a siteId was not there for the 8th hour, it should get its next data which can be 5 minutes past(or 10minutes past...) of that 8th hour.
Its hard to create a query for that and normal fetching and massaging the data in afterwards will take time.
I am using postgres, java and JPA. If I can do it via query or via some JPA utility to ease the CPU? I want to drop the time taken(right now 9 seconds for each query) to the least. Can you guys help me? Thanks in advance
My Table structure:
| timestamp | value | siteId |
|----------------|-------|--------|
| 1610370000000 | 22 | 123 |
| 1610370700000 | 21 | 123 |
| 1610370028000 | 22 | 123 |
| 1610369889000 | 23 | 123 |
| 1610370000000 | 22 | 124 |
| 1613534400000 | 21 | 124 |
| 1610369889000 | 22 | 124 |
| 1610370005000 | 23 | 125 |
So every site is having data for every 5 minutes. I want data of last three months with interval of at least 8 hours of every site. Hope this helps
Assuming you want same data structure like your example in question from last 3 months on 8 hours interval for each siteID.
Try this:
select
distinct on (siteId, "group" ) siteId, value, timestamp_,
ceil((extract(epoch from current_timestamp)*1000-timestamp_)/28800000) "group"
from test
where to_timestamp(timestamp_/1000) between current_timestamp - interval '3 month' and current_timestamp
order by 1,4,3
DEMO
Here I am dividing the difference of current_timestamp and timestamp_ field with 2880000(8*60*60*1000) to get the group and getting the first value of the group by using distinct on.
You can switch order by from order by 1,4,3 (return the value of min timestamp_ of range) to order by 1,4,3 desc (return the value of max timestamp_ of range) to check the correct result.
I am not sure about performance. But is should work better than java fetching.
If you need all data but you face issue with a transfer of huge data amount I would recommend to use pagination approach.
https://www.baeldung.com/jpa-pagination

How to ensure that there are no race conditions in a database update statement using spring

Imagine there are two entities Children and Gifts. Let's say there are 1000 children and 10 gifts. Children will always try to grab available gifts, and gifts will be tagged to children on a "first come, first serve" basis.
Table structure
children table
+----+------+
| id | name |
+----+------+
| 1 | Sam |
| 2 | John |
| 3 | Sara |
+----+------+
gift table
+----+---------+-------------+
| id | gift | children_id |
+----+---------+-------------+
| 1 | Toy Car | 2 |
| 2 | Doll | 3 |
+----+---------+-------------+
Here the children_id is the child who grabbed the gift first.
In my application, I want to update the children_id in such a way that only the first child who initiated the request will get it and rest get a GiftUnavailableException.
How will I ensure that, even if a 1000 requests come at a time to grab a specific gift, only the first one will get it. Or how will I ensure that there are no race conditions for this update.
Are there any spring specific feature that I can make use of or are there any other ways.
I'm using spring-boot as my backend.
I can't post a comment so here I go !
I assume you are using Spring Data JPA.
Then you should use #Transactional annotation : This mean that everytime you are requesting your database, you do a transaction :
Begin Transaction
Execute Transaction
Commit Transaction
Lot of usefull informations about this (Read it !!): Spring #Transactional - isolation, propagation
You will need to seed your Transactional Isolation to Serializable and maybe change the propagation method.
And if you are not using Spring data JPA.. Well There is a synchronized keywords but I think it's a bit awful to use it here.

Java & MySQL- Rankings

I am making a game and I require a ranking system. I already save all the stats like kills, deaths, wins, innocent shots, etc with MySQL. I am clueless a the moment on how I would be able to rank everyone. I want to have it over MySQL but they will be updated very quickly. I was thinking I could load all ranks in a HashMap when the game starts but that would be very ineffective since there are thousands of players. I want to also use most of the stats to work this out. Could someone explain to me how I would be able to do this? Thanks!
One way would be to use mysql Events to trigger a stored procedure. The stored procedure would execute the ranking and store the rank in the db. You would then just set the event trigger time to whatever you wanted, say 10 minutes.
mysql events: https://dev.mysql.com/doc/refman/5.7/en/events.html
CREATE
[DEFINER = { user | CURRENT_USER }]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'comment']
DO event_body;
schedule:
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...]
[ENDS timestamp [+ INTERVAL interval] ...]
interval:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
Then you would setup a stored procedure to generate the rank and set it for your players: https://dev.mysql.com/doc/refman/5.7/en/stored-programs-views.html
An example procedure would be:
CREATE PROCEDURE simpleproc (OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END
You can make the procedures as complex as you need to.

Update stock table through the invoice table in MySQL

I have three tables which are customer, stock,invoice.
customer table PK is CNo(Customer No)
stock table PK is PNo(Product NO)
invoice table gets the both PKs(CNo from customer table and PNo from stock table) as Its Associate key .
What I want to do is , update stock table QuntyAvailable column through invoice table.
Ex. when a customer gets 2 Quantities from PNo 2 which is Dettol , it automatically update stock table QuntyAvailable column for above product's own row to 18( QuntyAvailable-Qty).
I tried many times with various queries but I got only errors. like PK cannot be Updated ..
bla bla bla......
Please help me on this Thank you.
customer table
CNo(PK) | Name | Address
1 | Jhon | 23, Hill St, NY.
2 | Sam | 24, Bejin , Chaina.
3 | Nic | 25, London ,England.
stock table
PNo(PK) | Description | Each Price | QntyAvailable
1 | Dettol | $2 | 10
2 | Astra | $5 | 20
invoice table
CNo(PK) | PNo(PK) | Qty | value
1 | 2 | 2 | $10
2 | 1 | 3 | $6
after the update done I want the stock table like this...
stock table
PNo(PK) | Description | Each Price | QntyAvailable
1 | Dettol | $2 | 7
2 | Astra | $5 | 18
Please help me .. I m using mysql server and netbeans IDE
My query -------------
s.executeUpdate("INSERT INTO invoice(CNo,PNo,Qty,Value) VALUES('1','2','10','150')"); s.executeUpdate("UPDATE stock set QuntyAvailable=QuntyAvailable-10 WHERE Pno ='2'");
If you want to update stocks table when you do an insert in invoice, you can either do the update after the insert with the data you have for the insert (like the one you tried, the only thing that looks wrong is the ='2' that looks like it should be only =2) or do it in a trigger, having the newly inserted values feed the update:
CREATE TRIGGER updateStock AFTER INSERT ON invoice
FOR EACH ROW BEGIN
UPDATE stock set QntyAvailable=QntyAvailable- new.Qty WHERE Pno =new.Pno;
END;
//
You can check this fiddle to see it working.
P.S.
You really should have an InvoiceID. The way you are constructing it, you are not allowing a customer to purchase the same product twice.
P.S. 2 - Your error creating the trigger is related to not setting the DELIMITER.
If you don't set it the statement will end at the first ; You need to set it before the trigger and end your trigger definition with it. After that you can set the delimiter back to ;.
DELIMITER //
CREATE TRIGGER updateStock AFTER INSERT ON invoice
FOR EACH ROW BEGIN
UPDATE stock set QntyAvailable=QntyAvailable- new.Qty WHERE Pno =new.Pno;
END;
//
DELIMITER ;

Jasper Reports - Limit number of rows in a group

I have a CSV datasource something like this:
User,Site,Requests
user01,www.facebook.com,54220
user01,plusone.google.com,2015
user01,www.twitter.com,33564
user01,www.linkedin.com,54220
user01,weibo.com,2015
user02,www.twitter.com,33564
user03,www.facebook.com,54220
user03,plusone.google.com,2015
user03,www.twitter.com,33564
In the report I want to display the first 3 rows (max) for each user, while the other rows will only contribute to the group total. How do I limit the report to only print 3 rows per group?
e.g
User Site Requests
user01 | www.facebook.com | 54220
plusone.google.com | 2015
www.twitter.com | 33564
| 146034
user02 | www.twitter.com | 33564
| 33564
user03 | www.facebook.com | 54220
user03 | plusone.google.com | 2015
user03 | www.twitter.com | 33564
| 89799
It is really just the line limiting I am struggling with, the rest is working just fine.
I found a way to do it, if anyone can come up with a more elegant answer I would be happy to see it, as this feels a bit hacky!
for each item in detail band:
<reportElement... isRemoveLineWhenBlank="true">
<printWhenExpression><![CDATA[$V{userGroup_COUNT} < 4]]></printWhenExpression>
</reportElement>
where userGroup is the field I am grouping by. I only seemed to need the isRemoveLineWhenBlank attribute for the first element.
you may consider to use subreport by querying the grouping fields in the main report and then passing the grouping fields as parameters into the subreport; the merits of this method is to avoid the report engine to actually looping through all un-required rows (although they are not shown) and spending unnecessary server-to-server or server-to-client bandwidth especially when the dataset returned is large

Categories