I have already installed Mysql on my computer and I have already created my database. I have also created a Java program that connects to the database using the code attached below. If I publish the program, will the connection to the database work correctly on other users' computers? Will it be a problem if the code is in "localhost"?
Thanks to everyone who will help me!
public static Connection getConnection() throws Exception{
try {
String url = "jdbc:mysql://localhost:3306/mybooks?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String username = "root";
String password = "mypassword";
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
-
We'll need more information to help you. Please edit your question. Do you intend to ask the users of your Java program to install their own copies of the MySQL server, or to share a single server and the data in it? If the latter is true, do you have the server and MySQL installation set up already? Will your users find there's a firewall between their machines and the server?O. Jones– O. Jones2020年11月15日 14:21:03 +00:00Commented Nov 15, 2020 at 14:21
-
Does this answer your question? How to make sure that MySQL app will work on client's PC even if MySQL isn't installedSlava Rozhnev– Slava Rozhnev2020年11月15日 14:51:14 +00:00Commented Nov 15, 2020 at 14:51
-
Thanks to you both fo the answers. As you can see @O.Jones , I attached my code in the edited question. Hope this can help!Rich05– Rich052020年11月19日 13:52:33 +00:00Commented Nov 19, 2020 at 13:52
-
@SlavaRozhnev my idea wasn't to install on users' pc Mysql (because i don't think it's the best way to solve the problem), but to allow them to connect to my database.Rich05– Rich052020年11月19日 13:55:30 +00:00Commented Nov 19, 2020 at 13:55
1 Answer 1
You will need to put a MySQL server on a machine that's accessible to all your users via a LAN or the internet. That machine will need to have a usable internet address, either a hostname like database.rich05.example.com or some IP address like 192.0.2.123.
Then you'll need to create an accounts and passwords on the MySQL server for your users. You may choose to create just one account for all your users, or a separate one for each user. (I suggest the latter approach.)
Then you'll need to change your connection string from
jdbc:mysql://localhost:3306/mybooks?whatever=val&whatever=val
to
jdbc:mysql://192.0.2.123:3306/mybooks?whatever=val&whatever=val
or
jdbc:mysql://database.rich05.example.com:3306/mybooks?whatever=val&whatever=val
or whatever the internet address is. Then you'll configure your program to use the correct usernames and passwords.
In Java programs, connection strings and usernames are often stored in properties files.
Explaining all this in detail is too much for a Stack Overflow answer. Give it a try and ask more specific questions if you need to.