In Java you can use the Java Database connector (JDBC) API to connect your program with most databases. In this example I will demonstrate how to establish a connection Pool and use a JDBC connection to do some basic CRUD functions with a MySQL database.
To start with you need to download the JDBC driver for your chosen Database. In this case I am using the MySQL driver which can be downloaded here.
Simply add this driver to your projects class path.
Next we will create a DataSourceFactory class that will be used to establish a connection with our MySQL database.
Next we will instantiate the database connection object.
And use this object to get a connection from the connection pool. In the following example I will check a users credentials using a SELECT MySQL query.
This form of connection pooling is considered quite a low level method of establishing a connection to a database. In industry it is more common to use Frameworks like Spring that hide most of this functionality, but it is still useful to know.
Connection pooling is necessary to ensure a persistent connection is available for the entire duration that the program is running, which in the case of a web service could potentially be indefinitely. If you attempted to establish a direct connection to a database, this connection would timeout after a short period of time and require the service to be restarted.