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.
Related
I've made a Mysql db that i can easily manipulate with Java,
However I cant access it from other machines since its running off localhost only right? How would i go about making it so i would be able to manipulate the data from another machine. Or am i going about this all wrong? I've tried playing around with the remote access on MySQL workbench but I'm just not understanding it.
Here is how I'm accessing it currently.
Connection myConn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sampledatabase"
);
Of course you could access it remotely!
Firstly, you must understand what's a MySQL user and the authentication mechanism. MySQL treat 'UserName'#'Host' as a real user who can access to the MySQL server. When you access it locally the actual user you used is 'Me'#'localhost' (Assume the username here is 'Me'). And inside MySQL server there must be a user named 'Me'#'localhost' exist(or 'Me'#'%'). You must provide the right password for 'Me'#'localhost' as well.
When you need to access it remotely, you must replace localhost with the remote ip address so that your app could create the TCP connection correctly. Assume that the ip address of MySQL server is 172.0.1.5 and the ip address of your app is 172.0.1.10. The actual user you used now is 'Me'#'172.0.1.10'. You must make sure that the user 'Me'#'172.0.1.10' does exist in MySQL server. Otherwise you will encounter a auth failure.
To check all users:
select * from mysql.user; // if you have the privilige to perform it
Add users: Create User Syntax.
Yes, you can access the database, which is installed on other machine. You need to understand the url, which you provided
jdbc:mysql://localhost:3306/sampledatabase
Here localhost refers to hostname of the database server, mean same machine or local machine. If you are trying to access some other database server, which resides on other machine, then you need to replace localhost with remote-machine-hostname or it's ip-address.
I'm creating a java application that connects to a Firebird database and retrieve some data. The program is done, and it runs well in my computer. The problem is that, in the computer I need to run the application, the database is in another computer in the local network and I have no idea of which path should I put into the connection string of jaybird to get it running. Let me explain a bit:
I need to run my application in a computer X, but the actual database is not in a drive in the computer, but in a computer in the local network. Something like this (this is an example, and its reproduced at my home, not at the actual site I need to get it to work):
So the path that it gives me for the database is something like:
I know that if my database is in my drive ( for example, in C:\Users\john\Desktop ), my connection to the database using Jaybird should be something like this:
static final String FB_DB_URL = "jdbc:firebirdsql://localhost:3050/C:\\Users\\john\\Desktop\\TEST1.FDB";
FB_connection = DriverManager.getConnection( FB_DB_URL , FB_USER , FB_PASS);
Actually, that is how I managed to make it work in my computer. But in this case I have no clue of how should the path be. I tried something like:
static final String FB_DB_URL = "jdbc:firebirdsql://192.168.0.101:3050/\\\\DESKTOP-1UFA09I\\Documents\\TEST1.FDB";
With ‘192.168.0.101’ being the ipv4 address of the computer where the database is. But this gave me an exception saying that the application was unable to find the database. I think this is the closest I was to find the solution, but I had no luck.
Some extra information:
Both computers had the firewall off, and could send ping to each other, so they are, in fact, connected.
I'm using Firebird 2.5 and jaybird 2.2.1.
I also tried the following with no luck:
static final String FB_DB_URL = "jdbc:firebirdsql://192.168.0.101:3050/C:\\users\\john2\\Documents\\TEST1.FDB";
The folder is shared (or at least, that’s what I think) because I can get access to it via windows, and the database file gives read/write permissions to everyone.
I'm sure that Firebird is running in both PCs.
Any help in the matter would be greatly appreciated. I'm sure the problem is in the path, because, as I said, the application works well in with the database in my computer. Also, if you could give me a solution that doesn’t require the IP of the computer where the DB is stored that would be awesome, but I think I can get that information anyways.
EDIT
For documenting purposes the solution to the problem was the following:
the right way of making the path to the database is:
"jdbc:firebirdsql://192.168.0.101:3050/C:\\folder1\\folder2\\TEST1.FDB";
the exception occurred because, apparently, jaybird has some permission issues with the users folder in the server. As my DB was located in the documents folder, it was causing some problems when jaybird tried to get access to it. the solution is to move the DB to another folder in C://
To connect to a remote database you need to know the following things:
The hostname or IP address of the Firebird server
The port number of the Firebird server (default 3050)
User name and password
Alias of the database or the full path of the database
You seem to have the first three items covered, so the problem is with the fourth. You think the database is located in C:\users\john2\Documents\TEST1.FDB. If that is the case, then the JDBC url is:
jdbc:firebirdsql://192.168.0.101:3050/C:\\users\\john2\\Documents\\TEST1.FDB
However, the user running the Firebird server process by default has no access to user folders for security reasons.
I suggest that you move the database to a location outside of the user folder, and make sure the account running the Firebird server service has read and write access (NETWORK SERVICE or LOCAL SYSTEM, I forgot which and I currently don't have access to Windows machine) .
Other notes:
Locations of a Firebird database should not be shared, because accessing a database remotely should be done through a Firebird server
You should really update to Jaybird 2.2.11; 2.2.1 is almost 4 years old and a lot has been fixed since
If you are really using Firebird 2.5(.0), then upgrade to 2.5.6, or consider upgrading to 3.0
It looks like you want to connect to the Firebird server in a client/server mode and pass the filename of the database to it. The "host:port" part connects to the server, what follows the next "/" is the name of the database file.
If you use 192.168.0.101:3050 you have to make sure the server runs on the other machine having the IP 192.168.0.101. Since the server is already on the other machine, you need to give it local name of the database name on that machine, like in
static final String FB_DB_URL = "jdbc:firebirdsql://192.168.0.101:3050/C:\\users\\john2\\Documents\\TEST1.FDB";
To not use the IP you'd need to give that machine a name that can be DNS resolved in the local network, which is not difficult, an entry in the hosts name would suffice. The port 3050 is actually the default port so you can omit it.
Since this doesn't work I guess the server is not running on the target machine, only the database file is there. In this case you should connect to you local server but let it know the network reachable database file name, like in
static final String FB_DB_URL = "jdbc:firebirdsql://localhost/\\\\DESKTOP-1UFA09I\\Documents\\TEST1.FDB";
It should allow the local Firebird server to open the \\DESKTOP-1UFA09I\Documents\TEST1.FDB file over the local network - of course if the network share DESKTOP-1UFA09I points to the right location and the account under which the local Firebird server has sufficient access rights on the other machine.
I have created a standalone application as a school project. The major problem we are encountering is that since it has Java as front-end and MySQL as back-end (compulsory), and we have created a database that solely belongs to one computer, we cannot run the same project on different machines because it won't have the required database, the tables, or the same username and password we used to connect to MySQL.
So my question is **How can I connect to MySQL server in different Machines? **
For database and Tables, I could run a sql file, but that will happen when I would be connected to the MySQL server. Also I am developing the project at my home computer, and I want to run the project on different computers who are connected to my computer by no means .
You a following options
Shared drive : Attach database stored on remote shared drive to a local SQL Server read here
Connect to remote SQL Server instance from local computer - better if they are in same LAN - Steps here
If you want to package DB with app where you have predefined data and you dont want to save transactional data - use inMemory DB. They will load when you application starts up.
Already your application can work using different machine.
You have only to change the localhost string in your connection string with the IP of the MySQL machine :
DriverManager.getConnection("jdbc:mysql://localhost/database?"+ "user=sqluser&password=sqluserpw");
And make sure that remote access are enabled in your MySQL configuration. You can find more about it here.
Instead of using localhost in your database connection string, you should use the ip or aname of the database host
e.g.
jdbc:mysql://IP:3306/db?user=user&password=password
jdbc:mysql://A-NAME/db?user=user&password=password
Just make sure that port 3306 is open and that you have access on the system the databasse is hosted on
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.
I have always used a database located in my pc, so the steps to connect to it are:
connect to my local db using jdbc
perform queries
show results
now, i want to know what are the steps if the database is located in an online server?
some guides,tutorials are very appreciated.
Since you will be connecting to a remote database I would advise you to read about secure connections using JDBC. See this question, for example. You don't want to interact with a remote database without something like SSL to protect the confidentiality of the data.
Once you think you have secured your connection, you can use a tool like Wireshark to make sure that the packets that are going to and coming from your database are in fact opaque.
Furthermore, as others have said, not much changes if you already have a working connection to a local database, it's a matter of changing your URL from jdbc:mysql://localhost:port/database to jdbc:mysql://ipaddress:port/database.
From my experience, I found that some hosting companies block database access from unknown IP addresses, so it's possible that you'll have to go to your CPanel and whitelist your IP address.
Once your database connection is setup, the code you use to query the database should look the same.
Some useful links:
JDBC Best practices
JDBC Tutorials from Oracle
You have to change database url only to be like
jdbc:mysql://IP:3306/databasename
also username and password.
some hosting company like godaddy create database in localhost(in the same host) which doesn't need to change anything in your code.