How to insert data into database in next row automatically - java

I am working on access control system with a database as a project. I want to the database to serve as a data log. That is, I am writing a program that writes the login time and logout time to the database and also logout date whenever access is granted to a user. What I am finding difficult is causing the cursor go to the next row whenever a user is inserted into the database(is logged in). Can anyone help me on how to use Java to get this done? I want each user to be inserted into database in separate row so that even if the same user logs in a day after, it goes to the next row not being updated or over written by new data log

Thanks guys, I have figured it out. All I have to do is auto increment the raw number each time data is written into the database. When data is sent to the serial port, it will be read as strings. the string of characters which represents users card number is also available in another separate database table. The read card number is compared with those in that database table. if match is found, Names of the user having the card number will be SELECTED and written to the log table. Each time there is data in the serial port the row number is incremented.

Related

display user according to there last chats in recyclerview

I have created a one to one personal chat application for my college project. As users chat with each other there chats are been stored in Firebase as in the following manner
+ Chats
+currentuser
+Reciverid
+postPushid
+informations
I want to display the users to whom the current user had a conversation according to there the last message means as the new message is been inserted under the current user chat node that users id should show on the top as according to there time of insertion.
Help me out to arrange according to user ids according to there last message time in Firebase.
Just add one more attribute as timestamp and generate it by using the command
long timestamp = System.currentTimeMillis();
save that to firebase when uploading the chat message with a negative sign like
timestamp = -timestamp;
Now in Query variable just add the this
query.orderByChild("timestamp");
this will select the value which is least but the value in positive becomes least in negative and that's how it gets resolved.
I already implemented the same logic. I did other model of chats. I have separate table with users. Separate table with chats where each user see only users he got chat with. And I have separate table for messages where id is the chat. So in this case it is very to manage and it is flexible. You can add/remove fields from each model separately. Want time stamp in messages no problem. Want timestamp in chats no problem. All tables maximum got two levels instead of five you got. So i would recommend think about restructure of db.

Can I run an infinite loop in my java program to check if a third party have done somechanges in the database

I am designing a program in java and using mysql as DB. The program allows multiple users to connect to the DB.
An important part of the program is instantly updating the users that another one has made a change in a specific table that will have the user do an action and hence the changes will be show on his end.
I thought of listening on a socket but I couldn't resolve the concept of multiple users and how the sender will notify a specific user only not the entire group
this is similar to text message
So what I am trying to do is run a while loop that runs every 10 seconds to enhance the performance a little bit and will check the table against the username I am using to log in
this will get me what I wan but I know it is not efficient
Any Ideas ???
Use the "version control" pattern in the database table. Add a numeric column that is incremented each time the table is updated, such as:
alter table users add version_number bigint:
Then, each time the row is updated, you should increment this value as well:
update users
set my_value = ...,
version_number = version_number + 1 -- include this change
where id = 1234
This way, each client will remember the last version_number they saw. I they find a different version number, then a change has happened so an action should be taken.

android, Everyone should evaluate each day only once

I'm going to build an app. Until now everything runs very well. Now I have a problem. The app gets its content from a mysql database.A column is called item.I have a ratingbar. The user can rate the item there.Every time the user evaluates an item the value is stored on the database in the respective item line.The values ​​are then added. In other words, when a user evaluates 20 times with 5 stars, the value adds up to 100 and so on.
I want to limit this. I will that the user can evaluate each day only once an item. I will it without a registration mask for the user. How can I solve this problem?
I know that i can identifier the WIFI MAC Adreess and other Unique Identifiers, but how can i solve this with them?
I can not use sqlite database, because the items should update with the time from the mysql database.
A registration mask should not be excluded. If this process is quite possible with them, then I supplement it with it.
I am looking forward to every comment
every computer has a machine ID, you will hash that and encrypt that to use as your identifier..most telecomms do not like using MAc addresses as IDs
One option would be to create UUID during every installation and sending this UUID to your server along with every request. In server, you can control very well if user can provide feedback once a day or others based on your requirement. Pls refer this link on how to create UUID.
https://developer.android.com/reference/java/util/UUID.html

Is checksum a good way to see if table has been modified in MySQL?

I'm currently developing an application in Java that connects to a MySQL database using JDBC, and displays records in jTable. The application is going to be run by more than one user at a time and I'm trying to implement a way to see if the table has been modified. EG if user one modifies a column such as stock level, and then user two tries to access the same record tries to change it based on level before user one interacts.
At the moment I'm storing the checksum of the table that's being displayed as a variable and when a user tries to modify a record it will do a check whether the stored checksum is the same as the one generated before the edit.
As I'm new to this I'm not sure if this a correct way to do it or not; as I have no experience in this matter.
Calculating the checksum of an entire table seems like a very heavy-handed solution and definitely something that wouldn't scale in the long term. There are multiple ways of handling this but the core theme is to do as little work as possible to ensure that you can scale as the number of users increase. Imagine implementing the checksum based solution on table with million rows continuously updated by hundreds of users!
One of the solutions (which requires minimal re-work) would be to "check" the stock name against which the value is updated. In the background, you'll fire across a query to the table to see if the data for "that particular stock" has been updated after the table was populated. If yes, you can warn the user or mark the updated cell as dirty to indicate that that value has changed. The problem here is that the query won't be fired off till the user tries to save the updated value. Or you could poll the database to avoid that but again hardly an efficient solution.
As a more robust solution, I would recommend using a database which implements native "push notifications" to all the connected clients. Redis is a NoSQL database which comes to mind for this.
Another tried and tested technique would be to forgo direct database connection and use a middleware layer like a messaging queue (e.g. RabbitMQ). Message queues enable design of systems which communicate using message. So for e.g. every update the stock value in the JTable would be sent across as a message to an "update database queue". Once the update is done, a message would be sent across to a "update notification queue" to which all clients would be connected. This will enable all of them to know that the value of a given stock has been updated and act accordingly. The advantage to this solution is that you get to keep your existing stack (Java, MySQL) and can implement notifications without polling the DB and killing it.
Checksum is a way to see if data has changed.
Anyway I would suggest you store a column "last_update_date", this column is supposed to be always updated at every update of the record.
So you juste have to store this date (precision date time) and do the check with that.
You can also add a column version number : a simple counter incremented by 1 at each update.
Note:
You can add a trigger on update for updating last_update_date, it should be 100% reliable, maybe you don't need a trigger if you control all updates.
When using in network communication:
A checksum is a count of the number of bits in a transmission unit
that is included with the unit so that the receiver can check to see
whether the same number of bits arrived. If the counts match, it's
assumed that the complete transmission was received.
So it can be translated to check 2 objects are different, your approach is correct.

Concurrency control in web application

I have to solve this situation: in my Spring + JPA web application I have a jsp similar to an excel work sheet.
So I have a certain number of cells and each cell is saved in a DB table with additional information: I have a row for each cell.
id | value | column | row | ...
I use this structure because number of columns in my jsp table is dynamic.
At the moment, when I save cells I truncate the current set of rows in DB table and re-insert all the new rows. This is the fastest way I found to update a large set of rows.
But now I have a concurrency problem: the jsp page can be used by different users at the same time and this can cause overwriting problems on other users savings.
I need to implement some kind of lock in my web app. I found there are mainly two types of lock: optimistic vs pessimistic.
Can you suggest me a common approach to solve this situation? Where do I need to implement the lock, at data access level or at service level?
NOTE to be more clear: table values are shared among users, but can be updated by anyone among authorized users.
The solution would probably depend on the behavior requirements.
How about the following scenario: users A and B started to change some values, then user A pressed Save button and saved data, after that user B did the same. User B got an error message saying something like "the data has been updated, please reload the page". He reloads the page and lose all changes he did :( Only after that he is able to save his changes, but he has to do it once again.
Other possible scenario: users A and B accessing the page, but only the user who was the first will be able to save his work, other users will see message saying something like "someone else is editing the page, try again later".
For the first scenario you can implement the following: each line of the table (in database) has a last-update-timestamp which is updated to current time each time this row is changed.
Now, let's imagine user A get row with timestamp 1 when opened the page, user B was a little bit slower and got the same row with timestamp 2. But, he did his changes faster and pressed Save button first. Now, the row is saved in DB with timestamp let's say 5. User A is trying to save his changes, but the timestamp of his data is 1, which is different from 5 currently in DB. That means someone changed that data already and he should see error message I mentioned above.
Second scenario is a little bit harder to implement. I think the best way to do this is to open transaction to DB which
reads the row(s) we want;
put some flag like "locked" to true for all of them;
if some row is locked already, fails (or return available rows, depending on what you need). But, probably should fail;
returns rows to jsp page;
Now, if other user requested the same rows, transaction will fail and he will not be able to start changing data.
User A should put these locked flags back to false when he saves the data.
Important thing: these locks should have timeout to prevent situation when user opened the page and closed it without saving (or browser crash, or something else). You may also want to implement some kind of lock reackquire for the same user - when user opened the page for the first time, then closed it without saving data and opened once again - he should be able to edit the data. This can be done by identifying user somehow - login, cookie, and so on.

Categories

Resources