One database editable by multiple machines - java

I am new to coding at this level, and was wondering how I would get a database that multiple users on different networks could edit.
I am trying to make an application where customers of a lawn service can download my "app" or code, insert their address, and update their work log. This information can then be retrieved by the boss on a different machine.
I have a java application that uses swing as the front end. It prompts the user to enter their address, asks them whether they would like to update/retrieve work log/access different dress.
First, I had this data serialized so it could be retrieved later, but this only functions on one machine.
Next, I connected it with MySQL via JDBC so it is stored in a simple table with address as the primary key and work log as the only other column.
At the very least, I would like to figure out how to make this program work so I could export it, send it to someone else, and they could update the same database as me.
I can work on more action listeners etc later, right now I cannot figure out how different machines on different networks can access the same database via an executable jar.
Thanks so much.
*When sending executable jar to another machine, it only has access to *information they enter.
*I want it to have access to previously entered information, as well as *information that may be or may have been entered on other machines.

Here is an approach that would work for you and allow you to use your swing app with the least amount of object mapping. I assume that’s your goal since you went down the serialization path. Try this:
1. Data in the database is properly normalized to third normal form.
2. The JDBC query executes a stored procedure to get your object data fully denormalized in consumable format.
3. The stored procedure uses SQL command For JSON.
4. Map the result set to your java object using GSON. GSON will automatically map json to your object.
5. Should the end user change an object, push it back to the database and read it again. Don’t just update it locally.
I store my objects in a hashmap with a unique identifier. That way when I read in an object because it has been updated, inserting into the hashmap with the same unique id with replace the old version with the new. Put a change listener on the hashmap so that you can update the ui when an update is read in from the database. Using javafx you could further map to Observable Lists so the GUI updates automatically.
You could technically do this with the serialized object too. Just store a version ID and a unique ID with the serialized object.
Either way, you would finally create an async task that is periodically checking the version ID in the database against the objects in your hashmap. If the version changes for a particular unique identifier, then bring it in and do your mapping/class loading etc.

From your explanation, it seems like what you're trying to do is a straightforward client-server application.
The client : your Java app
The server : MySQL database server (on a
web-server online).
So all you need to do is let the client install/access the Java app, and use one database.
If it is not compulsory for you to use Java, simply consider a PHP solution, which your users may access using their web browsers.

Related

Chat Application, how to store chat history?

I'm developing a chat application in Java.
The architecture used is Server - Client(s) architecture.
The majority of the code is in Java, JavaFX for the GUI and PostgreSQL as the Database.
As this is a chat application (desktop), I'd like to know which is the best way to store chat history:
Locally in a text file, that the client has to read every-time
In the database as of type String (VarChar)
In the server as Lists
Some questions based on the three ways:
If a client connects from a different machine the text file will not be there
Is it possible to store every text entry in the database with a chatroomID?
How many objects can be stored in the server for as long as it runs?
Out of your three choices, I recommend you choose option #2 for storing chat history: A database, and here is why:
If you store the chat history locally in a text file, you run into issues such as how to sync with others. Also, you can modify the contents of the text file without going through your Java program (such as with an editor). If this file contains chats with sensitive information and someone has access to your computer, they can read it. This spells trouble.
Storing in a database is a great idea because it provides a central location for all your Java program. This is especially handy if multiple people are using your Java client, that way they can fetch chat history, as well as easily transfer chats to others! I wouldn't only use a type String (VarChar), but try to think of some other useful fields or columns that might be useful (i.e. timeSent, chatUserID, timeRead, etc). This also brings up the point that by using a database, you are able to set up some sort of user access rights (username and password) so that specific people can read specific chats.
If you store the chats on the server as a list within the Java server itself, and if your server restarts, you lose all your chat history. Bummer.
To sum up, keeping your Java client-server-database architecture is perfectly fine, and technically all 3 options could work, but databases is the way to go for storing your chat history! Even if setting up the database takes a little bit of work, it proves to be superior in efficiency and security out of the other 2 methods described since databases are built for archiving data.
I'm in a similar situation, I'm also developing a chat from 0, the only difference is that I'm doing it for iOS.
The way I'm developing my chat is:
I use an Ubuntu web server.
The server has a database in Mysql and the communication with the user is done through NodeJS.
In NodeJS I have a socket "Socket.io" which facilitates notifications between users.
On the iOS device, I store all the messages that it receives in Core Data, which is an extension of SQLite.
In order to obtain the pending messages depending on the device where the user is connected and I use an ID for each device, this ID is created and identified by the MAC physical address and thus what messages to obtain from the server and what not.
Initially I base myself on this database to know how to structure my application: https://github.com/yoosuf/Messenger
Socket.IO is incredibly easy to use and the best thing is that it has libraries for different programming languages, here is its page:
https://socket.io/
https://github.com/socketio/socket.io
I would say Nr2 - for safety reasons (if you care about it) and because it is a pretty easy way.
For the beginning a database with 4 columns should be enough (Date/Time, ChatroomID, UserID or just a name and the message itself). If a user sends a new message it creates a new row containing all the information that is needed for the columns. You can easily iterate through it as well when your client reloads (maybe every 10 seconds)

Synchronous use of database insertion

I have created a java application that is inserting data to a mysql database. Under some conditions, i need to post some of these data via email, using a java application that I will write as well.
My problem is that i am not sure how i should implement this.
From what I understand, I could use a UDF inside MySql to execute a java application, for which there are many against opinions into using it. Let alone that both the database and the mail client application will reside in a VM that i dont have admin access, and dont want to install anything that neither me nor the admin knows.
My other alternative, that I can think of, is to set up the mail client (or some other application), to run every minute just to check for newly inserted data. Is this a better aproach? Isn't it going to use resources for doing almost nothing. At the moment the VM might not be heavily loaded, but i have no idea how many applications there might end up running on the same machine.
Is there any other alternative i should consider using?
You also need to consider the speed of internet, database server load, system resources. If you have enough memory and less load to insert data in databases or database load is not so much then you can approach this by cron setup. For linux call a script for every 5 minutes. The script perform the following-
1. Fetch unread Emails as files
3. Perfrom shell script to read needed data.
3. write data to mysql
4. Delete the email
If you have heavy loaded system then wise you need to do this once or twice in an hour or may vary.

Using a server for a turn game (Android) - how to store data?

I am writing a simple turn game in Android for two players. I already developed an offline version (when two persons exchange the phone) and now want to create an on-line one.
They way I wanted to do it was to use Tomcat or my own PHP server for my website. My question would be whether it is possible and how to store data?
I think that the only thing I need to send to the server is the current score and an array representing the board. Then I'll be able to retrieve these pieces of information on client's side. Am I thinking properly?
I gotta say, I am completely new to all this stuff. I talked to my friend who programs in Java and he told me that I should use Tomcat. But how? It is a local server so the only way it could work would be when two mobile phones are connected to the same network or what?
The simplest way would be to implement a set of RESTful services on an online server.
You can use Tomcat if you want, but the cheapest approach would be to have a simple web hosting with i.e. PHP.
Create REST API's like /get/board, /get/move, /update/move, /authenticated and so on.
The simplest way to start out is probably not even with any real service, but just a couple of PHP scripts and an MySQL database.
You'll need some server, per example just a simple FTP server where you can put some on-the-internet reachable scripts on, and host the (MySQL) database.
Then per example for keeping the score you'll just need 2 scripts : 1 that saves and 1 that retrieves the score of a player.
Saving the score will just demand you to call on a link as per example:
www.server.com/saveScore.php?playerId=111&score=60
In the PHP you could just take this out with $_GET['playerId'] and $_GET['score'] and save those values in to your database with a query.
Returning the score to your app could work out with calling a link as:
www.server.com/retrieveScore.php?playerId=111
Which will simply query your database for the info on that player, and then print it as XML or JSON.
Then your app can just parse the response (xml/json) again.
A good idea would be to move this from $_GET to $_POST (POST is not visible in the link, but you'll need a bit more complicated code to transmit the data through that) once it works.
Once you've got that going you can work on actually building a service, REST would be a good choice, in order to increase maintainability, extendability and security.

How to create java desktop application with offline and online database, syncing periodically?

I want to create java desktop application, which stores it's data offline in a database (not just some config files). The application should work fine when the user is offline. When the user becomes online, the offline database should be able to sync with the online master.
Any ideas which technologies can be used to achieve this?
This has been discussed on stackoverflow a lot and it usually boils down to: don't roll out your own solution - This is a very specialized field - Look up SymmetricDS. It does what you want.
One of my fav discussions is Strategy for Offline/Online data synchronization
Use one of the available pure java DB implementations as a local DB. Use any other DB as a remote one.
Implement logic that tries to connect to remote DB and fall backs to local one on failure. If it connects successfully to remote DB implement the data synchronization.
When the local application operates, it should not only change database, but also log the changes. That changes are sent to the server when the connection is available. Also, the application receives logged changes stored on the server (from other application instances).
The main problem is how to merge changes made by different instances. There can be 3 variants:
1) Each application instance can modify only its private part of the whole database. Your are lucky, no merging needed, and server can store only logs and not run the whole database.
2) modifications always can be merged automatically (for example, application can add a value to a common variable, but cannot set it directly). The server runs the whole database, accepts partial logs from clients, generates its own log and sends it to clients.
3) Clients are allowed to do arbitrary modifications. This leads to potential conflicts. In case of conflicts, one of conflicting changes should be rejected. That means, that if a client made local modifications, that modifications can be rejected later by the server. The user interface must reflect this issue. In the rest, this is similar to the variant 2.

Fix app engine types

I recently made a change to one of my app engine models. I changed a Key field to a String. I forgot to remove all the old records. I have already added new records that have strings in the key fields. If I do a query for all the records I get an error, can not cast Key to String. If I try and change the class back to the old way I get can not cast String to Key. All this info is on the local file. How can I delete this data and start fresh?
When you say "local file" you mean in local development server? If yes,
then the local development GAE server also comes with a Admin console which you can use to view and edit the data store. Its located at http://localhost:8080/_ah/admin (check port number)
alternately you can restart your GAE and the local data would be wiped off
If no,
You have an admin console un app engine (view it in your app's dashboard). You can execute queries to modify data store from there
worst case, you can write a small servlet that will execute "delete" queries for the data that you dont want and execute that servlet
I'm assuming that your interest is NOT is supporting both the use cases - hence not thinking about that here.

Categories