1. Connecting to databases
1.1. MySQL JDBC controller
In order to create Java programs using MySQL driver, we need to load in our program and previously in our development environment. As we will use Gradle let's go how to use it:
- We need to find our server JDBC version, and we can find it in maven central repository https://mvnrepository.com.
- We will search for
mysql jdbc, and it will get as first result MySQL Connector/J. - Be notice to select the correct version, and then select the package manager (gradle, maven, etc.) and you will get:
| Bash | |
|---|---|
- When you save or build your project, you will be able to import your drivers properly

1.2. Connection URL
Once you have imported your driver, you need to tell your Java program how to connect to your database and finally connect to it. For this task we should create a Connection using a special URL called connection URL. As others URL, a connection URL may contain:
- Protocol \(\rightarrow\) we will use
jdbc. - DBMS \(\rightarrow\) we could use
mysql,postgres,sqlite,sqlserveror any DBMS with a JDBC connector. - Server address \(\rightarrow\) we could use either a full name or the server IP.
- [optional]Port \(\rightarrow\) the port where the server is listening. If none is used, the driver will try to connect to the default one. Remember to separate port from address with
:. - Other parameters, separating it with
?at the beginning and with&between parameters. For exemple: - user=
username - pass=
password - useUnicode=
true - characterEncoding=
UTF-8
A sample connection URL can be:
| Java | |
|---|---|
A better option will be:
Warning
Notice that both samples are hard-coded code, because you have values of the server (user and password) written inside Strings. Is a better option to store this values inside variables or in properties files.
1.3. Connection class
In Java the class needed to manage the driver is java.sql.DriverManager. It tries to load the drivers from the system when reading the JDBC drivers property, but we can indicate that it is loaded using the instruction:
| Java | |
|---|---|
The class that will centralize all operations with the database is java.sql.Connection, and we must obtain it from the DriverManager with any of the 3 static methods it has:
static Connection getConnection(String url)\(\rightarrow\) Returns a connection, if it is possible, to the database whose parameters are specified in the connection URL. Remember from last part how to create a connection URL.static Connection getConnection(String url, Properties info)\(\rightarrow\) Returns a connection, if it is possible, to the database, with some parameters specified in the URL and others in a properties object (Propertiesclass studied in unit 1). We will see samples later.static Connection getConnection(String url, String user, String pass)\(\rightarrow\) Returns a connection, if it is possible, to the database whose parameters are specified in the URL. User and password data are provided in two additional parameters, so you do not need to write in your connection URL.
A first sample will be:
With the Connection object that we have now connected, we will send our queries and ask for information, as we will see later.
1.4. Organizing our connection
Our application is going to connect to one (or more) databases. We can make many requests to said database, and if we are implementing a multithreaded application, the number of requests can increase a lot. That is why we have to control where and when connections are created and closed. A good idea is to create a class that encapsulates all these processes. The skeleton of such a class would be as follows:
1.4.1. Solved exercise
Create a single project with two classes. One for connecting to your database, as the last sample, and a main class that connect and show if an error have happened.
The exercise's solution is linked here connexio.properties and ConnexioDB.java