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
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(e.g. 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.
This is a basic approach to this problem:
-Have a client side application and server side
Request access to the database, have the server side application either accept or reject the request.
Once authenticated to the database, have the server side application send the information over to the client application containing the database info
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.
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.
Related
For a university project, we were taught to use JDBC to connect to a MariaDB database. The database was created on localhost:3306.
This is what we used:
Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/dbname", username, password);
I am now doing an online group project using GitHub. How would my group partners gain access to this database if it is not on their local machine? I read somewhere about SQLdump but I couldn't seem to get it working. If I successfully did an SQLdump, could I just include the file in github and it would work for them?
Otherwise, would I need to put it on a public server?
First you need to decide whether you want to share the data or the database:
share data
This will put copies of data to your group members. Every one of you has his/her own copy in a locally installed MariaDB database, and every one of you can access it using localhost in the connection string.
https://mariadb.com/kb/en/backup-and-restore-overview/
The sql dump file can be easily shared with others. If you find the backup/restore stuff is getting tedious and merging the data is a requirement, check for https://www.liquibase.org/, or even consider going for the shared database.
shared database
Ensure your MariaDB server not only binds to localhost (127.0.0.1) but also to your machine's public IP.
open your machine's firewall to allow access to this IP. Maybe you have to look at more firewalls to be opened (e.g. your router).
create user accounts that are allowed to connect from their respective host or from any host (https://mariadb.com/kb/en/create-user/#account-names)
You can do all that from your local development machine. If you find the network or uptime requirements are not comfortable, switch to a dedicated server. This may be a cloud solution or on-premise, whatever is comfortable for you.
I have a java application that I programmed, and I also created a Wampserver database to store the app's data. When I install the application in another computer, it basically can't do anything because wampserver isn't installed on that computer, and even if it was, the database wouldn't be imported on that same computer.
Can I install the database with the program itself, without making a database server in my computer? (For security reasons)
Sorry if this is a dumb question, im new to programming.
Assuming you are doing this in a Home/office environment. Where your IP Addresses do not change so often. Or you have the power to assign them statically.
You could try using a multi-threaded client server model. Where your wampserver computer(server) will accept a connection from another computer(client), and from there any queries to the database will be handled by that thread. This way, you could get away without installing wampserver on every client.
It would require some networking codes to be added to your java program. There are many examples on multi-threaded client server model -> Multithreading with client server program
You can open your MySQL server to the outside world or to certain IP (comment or edit bind value in your MySQL configuration file).
This can be a security issue. What is your objective? Usually, you'd do an API allowing access to your data from your application.
You are talking about "hosting" your database. You need to either:
Create, develop, and run a hosting service (not trivial), or
Pay to one the myriad of hosting services available, and install your database there (cheap these days). Just a couple of well known examples: Digital Ocean or Amazon Web Services.
In any case, it's a huge security risk to open your database to any client online.
Normally you write a program/application that accesses the database in the same [local] network, and that application serves web pages (or other web service) to the world.
Currently I have a Java application which performs all I wanted and the limitation
in it is "It only resides on the Local machine for which it needs to work". I have the Access Databases located on a Remote Server and I install the Java app on the remote server to connect the databases using a connection string jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=D:\\DATA\\DPPI\\DATA\\DPPI\\DPPIPR01.mdb;
It connects to Access Databases and do all the business logic with them.
Enhancements (to be done):
Making the application online - not a big issue.
Connecting the databases remotely - its the only concern.
If I make the Application Online the Access Databases need to be connecting through network (somewhat like IP or some web address).
Problem:
But after going through this Connect to Access database remotely It seems my requirement can not be completed.
Is there any way if I'm able to upload the database to the online web hosting server and access the database from there only? If yes, then how should I do the connectivity since Access Database connects by giving the fully qualified name containing drive letter and all.
But how should I proceed further so that I may be able to connect the mdb Databases remotely?
ADO Solution:
But after going through the link: http://webcheatsheet.com/ASP/access_connection_strings.php, it seems we can perform remote Access Database connectivity.
The solution for the ADO is something like:
connectionString="Provider=MS Remote; Remote Server=http://your_remote_server_ip;" &_
"Remote Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\your_database_name.mdb"
I'm a tad confused to how Access Database we can connect.
There is one more question in my head.
I'm making use of Type-1 Driver. Should the Type-4 Driver support the remote connectivity?
If your application is a server side application (like a web application) and your server's OS is Windows then theoretically it is possible to use your Access database. In this scenario, the Access database is local to your application. In your connection string you can simply use the path to the Access database inside your server's file system.
But keep in mind, that Access is a desktop database not made to use it as a server database. This can lead to poor performance and maybe system crashes. And it is possible that there are some licencing issues for this use case.
So even it may be possible to use Access with a server application it is strongly recommended to use a real server database like MySQL, MS SQL Server, etc. Even an embedded database like Apache Derby or H2 would be a better fit.
If your application runs at client side like a fat client, then your only chance to use Access is to put it on a network filesystem, so it is visible from every client. But this works only inside a LAN. If you cannot put it on a network filesystem then you have to use a real server database.
Do not use Provider=MS Remote (RDS) for new development. Here is a quote from the MSDN article
Microsoft OLE DB Remoting Provider (ADO Service Provider)
Important
Beginning with Windows 8 and Windows Server 2012, RDS server components are no longer included in the Windows operating system (see Windows 8 and Windows Server 2012 Compatibility Cookbook for more detail). RDS client components will be removed in a future version of Windows. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Applications that use RDS should migrate to WCF Data Service.
As others have suggested, you should seriously consider using a different back-end database for this particular application.
What if I'm able to call another java application residing on the Server housing the Microsoft Access Databases? Can you please let me know if it can be the solution such that I will have the division of my application into two:
1. The Online java Application performing 2 things: Accepting the requests coming in and waiting for the response to come from another application running on the Server housing the Microsoft Access Databases.
2. The Java application installed on the Server housing the Microsoft Access Databases only responsible for connecting and returning the DTO (Data Transfer Objects) -or some kind of objects in Collections.
So, I would expect a serverside program to handle the communication with the DB and the client application would communicate with the server app.
Please let me know if I should proceed in this manner.
it can be done by marking the traffic and queueing the traffic from the server---to----remote site ---(QOS..quality of service ) .....the two sites will be connected through VPN methods to reduce the unnecessary hops that will improve the latency plus add the security , thus server and remote site will logically act as if they are on the same network (LAN)
regards ,
Ali Rehan
I have already developed a JAVA SWING Application. I want to install this application in several computers which all have access to internet .
So where should I place my MYSQL Database ? All these computers should have the ability to access the same database via the internet.
It's not very common to allow access to a database directly over the internet for security (authentication authorization, encryption) and performance reasons. In most cases, you would create a web-based server application which can perform database operations, enforcing business, domain, and security rules. The client application makes calls over the internet (using REST calls, AJAX calls, or something proprietary), and gets the results back from the server.
In your case, since your client software is Java, you may want to research Java Servlets and run something lightweight like Tomcat as a serer.
Please see this page and this page for an explanation of client-server relationships.
Put your database in host that is accessable by Internet.
For each of the external ip addresses of each of the Applicaton host create a user on mysql.
GRANT ALL PRIVILEGES TO database.* to `user1`#`APPLICATION_HOST_IP` identified by 'PASSWORD';
Now user1 will have access from APPLICATION_HOST_IP IP.
Well the possibility is limitless.. You can place it on every computer that is accessible by the others. As long as you can acces the database via network it does not really matter where it is. But if the Computers with your client Swing are all connected in LAN, than store the DB on the computer with best Performance.
As a tip use the IP, and port dynamicaly from text or entry, so u do not recompile every time u change the IP.
Chiers!!
I am developing a java applet which connects to mysql database, the applet works fine on local host but when I uploaded it and tried to run, i get an error as "communications link failure the last packet sent successfully to the server was 0 milliseconds ago.
the driver has not recieved any packets from the server"
i tried it on x10hosting and hostable, please help me if anyone knows the solution...
Are you sure your MySQL server port is really open to the public? E.g., on a publicly-routable IP address and not blocked by your routers/network? Most likely your MySQL instance is not accessible from the outside world...
...which is how it should be. It's very, very, very rarely appropriate to have an applet talk directly to your database server. The usual thing is for the applet to talk to a middle tier on your web server, which in turn talks to the database server. There's a security aspect to that (your middle-tier can defend against various attacks on your database), and there's a practicality aspect: You'll probably want to keep the chatter across the public network to a minimum, and your server can roll up the results of multiple operations into one bundle it then sends to the client.
If your MySQL server really is available to the outside world, is its host the same as the host the applet is loaded from? That's one of the security requirements for (unsigned) applets, that they can only open network connections to the same host they were loaded from. But if that were the issue, I would have expected you to get a security exception (unless Connector/J is hiding it from you).
The applet is client-side. If you want to directly connect to a mysql server this would mean each user that opens the applet should either have mysql installed or have access and credentials to the remote database. Even if your firewall allows the access it is a very bad idea to do so. If you do everyone will have access to your database.
So if you want to communicate with a remote database, use some protocol (http would be easiest, via a Servlet) to send requests to the server and receive responses generated based on the database results. For example invoking http://yoursite.com/addRecord?name=foo&email=bar#baz.com could tell the server-side application to open a (local) connection to mysql and insert a record.