简体   繁体   中英

java desktop application - sharing one database host on local server

I am working on java desktop application (swing) which needs to be install/run in three different computers on same Local Network ( LAN ). But this application need to share one database and each application should be able to insert, update, delete records in the same database.

How do i achieved that in java, any suggestion/help would be appreciated ?

Thanks in advance

It works exactly as if the database is local.

THe only main difference is that the url to the database is not in the form 127.0.0.1 (or localhost) but has the ip of the machine where the database is present.

If changing that you have connection problems probably a firewall is blocking your requests. Check if a firewall exists and open the port of the database.

If more applications try to access to data at the same time you need to put autocommit to false and manually commit the data only when the update is finished. Other threads reading or trying to update the same data will wait until the first thread has committed (or rollbacked) the transaction.

This is a basic approach to this problem:

-Have a client side application and server side

  1. Request access to the database, have the server side application either accept or reject the request.

  2. Once authenticated to the database, have the server side application send the information over to the client application containing the database info

  3. When the client application wants to perform one of the actions you described : update,delete,etc...have it send requests to the server application to complete these.

I came across this while reading some questions. I also created a desktop application which is being accessed by other computers on the same network which includes laptops and desktop PCs in the past.

Step 1: Choose which of the PCs will be the server. In other words, which among the PCs will be your server. Your chosen PC-server will have a server application(eg XAMPP) installed. This PC is where you will import your existing database (.sql file). Other PCs don't have to have XAMPP(or other server applications) installed on them. Other PCs only need to have a copy of the desktop application that you created. No need to set other PCs with server application and sql.

This PC-server that you have chosen will have a connection string with localhost since I assume that all of them are not accessing the internet to use the database.

Example: jdbc:mysql://localhost:3306/yourdatabasename

Step 2: Grant privileges to the database that was setup on your chosen PC-server The simplest example is the one below.

grant all privileges on db_name.* to 'username'@'localhost' identified by 'password';

Don't forget to replace with your connections' username and password

But you may also select certain privileges based on your preference.

Step 3:

Set other PCs' connection string that has copy of the desktop application you created.

If PC-server has an IP Address of let's say 192.168.2.3 then, the connection string of other PCs will be

jdbc:mysql://192.168.2.3:3306/yourdatabasename

Other PCs will connect using the PC-Server's IP address since our database and server application was setup on PC-Server.

I hope this helps and others who might see this question.

Peace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM