When you connect Java to the database (in the MySQL case), you typically do something like this:
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/basededados", "usuario", "senha");
Note that in the first parameter you have a URL. If in it, instead of using localhost
, use an IP or host name, that's where it will connect. So you give the network location where the database is, so the database does not need to be on the same machine where your Swing program runs. This command will only run when the program is running, never during compilation. Even because it may be that the user or the seha, or even the URL itself is read from the user or from some configuration file.
You compile the program without the database, just put in the classpath or modulepath the JAR needed to connect to it at runtime. You do not need the database to compile the program.
It might even make sense for you to install MySQL on the same machine where the Swing program would run. But if you need two or more machines accessing the same database instead of separate databases, it would only be installed on a single machine and accessed via IP or hostname.
class DriverManager
and a interface Connection
are in package java.sql
. This is the package that contains the necessary classes and interfaces so that your program can use some database. However, the connection to the same database is managed by the connector, which in the case of MySQL can be downloaded here . In general, for each different database type, you will need a different specific connector.