DataSource
The SQL package defines the DataSource interface. An object of that type is where you keep your database connection info such as database server address, database user name, password, and so on.
To quote the documentation:
A factory for connections to the physical data source that this DataSource object represents. An alternative to the DriverManager facility, a DataSource object is the preferred means of getting a connection.
Choose an implementation of that interface. A JDBC driver usually comes with one or more implementations specific to that particular database. You need an implementation specific to that database because various databases have various features and settings.
Return an object  of the more general type, the interface, rather than the more specific type, the concrete implementation. Doing so gives you the flexibility of swapping out the concrete class for another without breaking any calling code.
    private DataSource configureDataSource ( )
    {
        System.out.println( "INFO - `configureDataSource` method. " + Instant.now() );
        com.mysql.cj.jdbc.MysqlDataSource dataSource = Objects.requireNonNull( new com.mysql.cj.jdbc.MysqlDataSource() );  // Implementation of `DataSource`.
        dataSource.setServerName( "db-mysql-lon3-433-do-user-89673-1.x.db.ondigitalocean.com" );
        dataSource.setPortNumber( 24_090 );
        dataSource.setDatabaseName( "defaultdb" );
        dataSource.setUser( "scott" );
        dataSource.setPassword( "tiger" );
        return dataSource;
    }
Retain that DataSource object in your app. When needing a database connection, call DataSource#getConnection.
        String sql = "SELECT name_ from  user_ ; " ;
        try (
                Connection conn = myDataSource.getConnection() ;
                Statement statement = conn.createStatement() ;
                ResultSet resultSet = statement.executeQuery( sql ) ;
        )
        {
            while ( resultSet.next() )
            {
                String userName = resultSet.getString( "name_" );
                System.out.println( username ) ;
            }
        }
        catch ( SQLException e )
        {
            e.printStackTrace();
        }
Be sure to close your open resources such as Connection, Statement, PreparedStatement, and ResultSet. Generally best to use the try-with-resources syntax to automatically close your resources.
Notice a DataSource is never “opened”, so we never need close it. A DataSource merely holds the bits of info needed to ask the database server for a connection. The DataSource object itself is not a resource.
JNDI
Eventually you may want to use JNDI to obtain a DataSource object. This enables you to externalize the database configuration through a Jakarta EE server, LDAP server, etc. rather than hard-code the username, password, etc.
Under this approach, when your database changes its address or passwords, you can update the external configuration without needing to recompile your code and redeploy your app.
To quote the documentation:
An object that implements the DataSource interface will typically be registered with a naming service based on the Java Naming and Directory (JNDI) API.
For more info
See also: