You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We shall see how to build a JDBC URL and then show how the exam expects me to get a connection to the database
🟥 10.3.1 Building a JDBC URL
The JDBC URL is like the URL of a website or the username/password to an email
The JDBC URL has various formats and all have the same components in common:
Protocol - this is always the same jdbc
Vendor name - the name of the database, e.g. postgres, mysql, derby
Database specific connection details - i.e. the location and name of database
E.g. jdbc:postgres://localhost:5432/zoo
Here are some examples of connection details:
jdbc:postgres://localhost/zoo - port number is optional using default
jdbc:oracle:thin:@123.123.123.123:1521:zoo
jdbc:mysql://localhost:3306/zoo?profileSQL=true
🟥 10.3.2 Getting a Database Connection
There are two ways of obtaining a connection to the db: DriverManager or DataSource
The DriverManager is the one which will be used in code but should not be used in practice as DataSource provides more features like pooling connections or store the db connection info outside the application
DriverManager uses factory pattern enabling you to statically obtain a connectionL DriverManager.getConnection("jdbc:derby:zoo")
Here is an example of getting a connection from the embedded database: