auto reload content of a postgreSQL table - java

I am using this code:
my code
to output my table. My question is how is possible to updating it in the page without reloading?
Thank you

Either use AJAX requests or if you feel good, try HTML5 websockets : add a trigger to the table UPDATE / INSERT / DELETE, and keep your clients up-to-date.
AJAX means you'll be doing a request from client to server every few seconds, to check if anything have changed and if so, you'll download the new data (usually devs use JSON), and update your table using javascript with this data.
With websockets, the event starts server side : whenever the db table is updated, you send a request from the server to the clients to tell them to update the table with the new data.
You'll find plenty of examples on the web, searching either for AJAX or websockets.
If you don't know what they are, I'd recommend you to go for AJAX, as setting-up a websockets server with PHP might be tricky.

Related

Is it possible to refresh all clients with a new thymeleaf model?

I need the front end to display the most recent record in my db (mysql if it makes a difference). Is there a way for my java app to send that value in the thymeleaf model to all existing sessions?
The only way I can think of is to send an ajax request every second to an endpoint that will respond with whether it needs to update or not but this seems a bit hacky

How to write a function to handle custom query in JPA repository in spring boot java?

Basically I am hosting a database on server side using spring boot. And I wanted to write custom query from client side which is developed in angular and call a function from server side for giving me the needed results.
The function for server side will look like this:
List<rows> func(String customQuery){
//fetch rows from table using this custom query and return those rows
//which I can use in client side.
}
Below are examples of customQuery which I need to send from client side:
select * from table;
select * from table where id>10;
select * from table where id>20 and id<30;
So far I searched in Internet I could not find any solution. Please help.
Sending SQL directly from the client would be a complete security failure, because a malicious user could easily figure out how it works and send a delete from table to kill your entire app.
Much worse, they might even be able to run create user ... to give themselves complete access to your entire database, get sensitive information, install malware, etc.
Instead you'll want to create a REST service in your application with methods such as
GET /table
GET /table?minId=10
GET /table?minId=20&maxId=30
Return as application/json or a similar data format and only return the information your angular app really needs.
Angular would then be responsible for selectively updating the display with your data.
Edit:
This is a guide I have found for creating a basic web app based on Spring Boot and Angular. Might be a good starting point for you:
https://www.baeldung.com/spring-boot-angular-web
The best way is to send only the parameters (minID and maxID in your case) from the client-side and then build the query dynamically on server-side using Spring JPA Specifications.

How to return from a process to a frontend backend process?

I need load 10.000 rows in my database google cloud sql using AppEngine with Java. For this case, i use a proccess using backend, but i want advertise to user, how rows was wrong load? But, i don't know as send a message from my backend proccess to my front to show a message to screen.
Regards.
Maybe you don't need to send callback (from backend to front-end). Maybe you should make the front-end poll to see when these results are ready at the back-end side. Maybe through some JS/Ajax code which keeps polling on the background and once the results are ready, pulls them and displays them in the designated area of the page. I assume your front-end is a web page.

Send data to front end when back end updates [duplicate]

This question already has answers here:
Real time updates from database using JSF/Java EE
(3 answers)
Closed 2 years ago.
The backend of my web application receives updates from several clients. When such an update happens it should be communicated to all other clients.
How can I initiate an update from the server to all web browser clients when my backend is updated?
I'm using JBoss, JSF and the Spring framework.
See similar Stack overflow quetion : WebSockets vs. Server-Sent events/EventSource
I'm assuming, as DarthVader did, that your frontend is a (generally) stateless HTML page of some sort. Something in a browser. If you want all clients to be pushed changes automatically, you have three options:
Comet: (deprecated)
Comet is essentially making AJAX requests that have no request timeout limit. You make the request, and it sits there and streams data through it as is neccessary. This can be done with hidden iFrames or standard XMLHTTPRequests (which jQuery can wrap for you). You can read more about this method here.
Long Polling:
Essentially, you use the javascript setInterval method to continuously poll your server for changes. Simply set an interval that does a standard AJAX GET request to the server, and upon every success, update your page accordingly.
Browser APIs
HTML5 WebSockets
Using any type of Event-Based backend (Twisted, EventMachine, node.js, etc) makes WebSockets the ideal solution. Simply have all clients register with the backend, and upon a submit from any given client, push the changes to all other clients. You can read more (and see a nice example) of WebSockets on this page. Browser support => canIuse
Server-sent event (SSE)
With server-sent events, it's possible for a server to send new data to a web page at any time, by pushing messages to the web page. These incoming messages can be treated as Events + data inside the web page.
Browser suppport => canIuse
When you say front end, you are talking about stateless http client.
You cant push anything from your web servers to http or stateless clients.
The "trick" to do this if using asynchronous calls from front end to your back end, periodically.
Think about gmail, how do you think it displays that you have an email when you recieve a new email. You browser contantly, sending Asynch calls to gmail servers, if and when there is a new message, it displays it.
So Clients are stateless. use Ajax.
is this clear?
There are a couple of ways to go around this.. The way it should be in the future is following standards like Websockets
For now you are stuck with Comet which is essentially sending a request to the server and keeping it open (not signaling a response end) and just streaming data through it (Parking the request they call it). Or periodic polling, where you just do an AJAX request to the server every predefined interval to ask if the server has something new to say. Needless to say the first work around requires streaming support on both the server and browser but is more efficient in most scenarios.

mysql alert that it has been updated in PHP

I want the user to be informed that database is updated. I'm using PHP and mysql. There is a process behind which updates information in database. Once the process (java application) updates the database, how can I tell user the that they can view the processed information?
I don't have any idea at this moment how to do it or even is this possible?
You will have to check some script periodically (assuming your endclient is an webapp - probably using javascript) and raise an alert once the script returns the DB has been updated. Have a look on jQuery polling.
you could trigger ajax requests to poll information from the server, or you can try to use websockets to let the server
I've not fully understood your question.
If I've understood, you have a job that updates tables in the db.
While the job is running the user is not aware that the data are being updated and when the job has ended the user is not informed about.
I don't know if you can handle the tables, what kind of data are there, and if you can manage/control the java app...
If data and tables (and java app) are under my control, I will do the following:
create a table called job_in_progress, with some fields like running as boolean, started_at as timestamp.
modify the java app to update that table writing true in the running field when started, and writing false when ended.
when the user goes to your PHP page, you check the job_in_progress table, and if the running field is true, it means that the java app is still running.
Another way: send an e-mail to the user when the java has ended (if the update process is heavy).

Categories

Resources