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
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 made an java application which utilises SQL database through MYSql . But I want to transfer that exe file to another computer. I want that the data is stored and can be accessed locally on that computer without installing Mysql on that computer through the Java application . Is there any solution to this problem ?
If you don't want to install any Sql server and still want to save your sql data on your new computer, the answer is no.
Your program needs to connect to a database server to CRUD your database. Java application could not do it by itself.
If you write your program to connect to MySQL, your users must connect to some MySQL server if you want it to run. So each user must either have their own copy of the MySQL server or connect to an existing shared one. So if that's the way your application works, you will have to install a MySQL server on each user's machine. (This is difficult to do in an integrated installer).
(A big selling point of DBMS systems is the sharing of data between multiple users on different machines.)
Other JDBC- and SQL- compliant database systems exist that run entirely within a Java application. Those database systems are single-user, but you can package their .jar files with the rest of your installer. The H2 in-memory database is an open source example. You may have to make some changes to your SQL to use an in-memory database, but you'll be able to use the same framework.
Some developers create programs with both standalone and shared modes. They do that by testing their programs both with MySQL (or some other database server) and their chosen in-memory database. If you do that, your users will be able to use personal, locally shared, or cloud-based data.
Of course, the users of shared or cloud-based data will have to know the JDBC connection strings for their servers. And you'll have to make sure they can put the connection strings they provide into the right properties file, when they "go shared".
I have two domains.
In domain A, I have a PHP web site and in domain B, I have Java Website.
Now I want to use both database from both site.
How can I do that? I am using MySql 5.7.17
You could set up a third server dedicated to mySQL and share that database with the 2 web servers, or you can just run a mySQL database on each of those computers and use the IP address or domain name of them to connect to each other's server
Explanation:
You currently have a java website running and a php website running on one machine if i understand correctly.
What you need to do is download mySQL, and go through the tutorial to get the server up and running https://dev.mysql.com/doc/mysql-getting-started/en/
Then you need to download the mysql Java driver, and the mysql PHP driver
Then you need to use the drivers to connect to the same mySQL server from the code, which would be on localhost if you are doing it all on one machine.
MySQL is a server. It is its own thing, not a database like SQLite that you create in the java code or php code. You create and set it up separately as it's own server, and then any other server can connect to it
You can open and close a mysql connection any time.
mysql_connect(server, username, password);
Do some stuff with one database, then you can use:
mysql_close();
mysql_connect(server2, username2, password2);
as many times as you like.
Or you can use resource links:
$db1 = mysql_connect(server1, user1, passwd1);
$db2 = mysql_connect(server2, user2, passwd2);
But in this case, each mysql command needs to be referenced with the resource link:
mysql_query(query, $db1);
I made an inventory system in netbeans using the language java and i connect it to derby as my database it is already working as a whole system but I want to have a client-server functionality what I mean is I want my program to have a server which holds the database(different computer) and different users with different computers which they can save/view data at the same time to the database means they are connected in one network.
Working with multiple connections to a single database.
What code can I use or method or do I have to import something?
I did some research and the only thing that I found is socket which can be used to create a chat between server and a client.
But I only tried the IP 127.0.0.1 for the client since I am making running the server and the client in the same computer.
And also can I open the connection of the server in the client form and send data like SQLQuery so I can save it in the database of the server?
Where can I see examples for these? Please help thanks
Yes, Derby supports a client-server configuration of your application.
Deploying your application in the client-server configuration is straightforward:
Deploy the Derby Network Server, for example by running the startNetworkServer script that is included with the Derby distribution.
Ensure that derbyclient.jar is in your application's CLASSPATH, and that you register "org.apache.derby.jdbc.ClientDriver" with the JDBC DriverManager.
Change your JDBC Connection URL from jdbc:derby:<db details> to jdbc:derby://<host:port>/<db details>.
If this is your first time using Derby, I strongly recommend working your way through the Derby tutorial at https://db.apache.org/derby/docs/10.12/getstart/index.html
For more information about running the Derby Network Server to service database requests for your applications, read the Derby Admin Guide: https://db.apache.org/derby/docs/10.12/adminguide/index.html
Use the IP 0.0.0.0 or for all connections in the server. The the connection url should include the name of the server or the ip address of the server in the network. When you use ip 127.0.0.1 or localhost derby can only accept connections to the database in the same machine, in this case localhost. All of this can be done by your network application server
I am attempting to create a MySQL DB and Java client app for my home network. I haven't really had any experience with MySQL other than PHPMyAdmin for a website backend (also have used SQLite). I have downloaded the full MySQL installation and a test DB from the MySQL website. On the server machine I successfully connected to the DB as root. Not a difficult task.
Now I want to connect to the DB from my client PC, just to check I can. Eventually I will use the JDBC driver to connect from my Java client app, but before that I just want to check I can connect.
How should I do this? SHould I just install the MySQL command line program onto the client PC?
EXTRA INFO: forgot to add, I'm using Windows 7 on all my machines.
Yes, you could install the MySQL to get MySQL command line program and connect with it, but also you can use another MySQL client tool like PHPMyAdmin, dbForge Studio for MySQL or another one.
To connect from remote host you should create special accout for it, e.g. - 'user_name'#'your_host_name'. Find more information here -
Specifying Account Names
Account Names and Passwords