0

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;
    }
4
  • 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? Commented 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 installed Commented 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! Commented 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. Commented Nov 19, 2020 at 13:55

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much @O.Jones , that was the answer I was looking for! I am going to try as soon as possible. Bye
Hi man, I tried to do as you advised me by following all the steps. Unfortunately it did not work as I hoped, because users from other machines are unable to connect to my database. You advised me to make my machine accessible via LAN or the Internet, but I'm not sure about how to do that. Could you explain what you meant? Thank you so much
Do you have access to a server computer? Do you have access to somebody who can help you set up a server computer? You need to install MySql on that computer and load your data into it.
Please help me level-set any advice I can offer. Who are your intended users? How will you deliver your app to them? Do you work in a company with an IT function? Will your app run on the public 'net or on a LAN? Do you know how to set up a server machine (server computer)?
Hi, thanks @O.Jones for the speed in your reply. I have already installed MYSQL on my computer, but unfortunately I don't know anyone who can help me. I'm learning to code on my own at 15, so I think users won't be a huge amount at the beginning. I would pass the program to them in the form of an installation setup. I don't have any idea about ​​how to set up a server machine, that's what I was asking to you. However, do you think it is still appropriate to try this solution or is it better to switch to another type of database, like SQLite?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.