To be able to use JDBC you need to have the JDBC driver of your database on the class path of your application.
There are multiple ways to connect to a database, but the common ways are to either use the java.sql.DriverManager
, or to configure and use a database specific implementation of javax.sql.DataSource
.
A simple example to create a connection to a database with the url jdbc:somedb://localhost/foobar
and execute an update statement to give all employees a 5% raise:
try (Connection connection = DriverManager.getConnection(
"jdbc:somedb://localhost/foobar", "anna", "supersecretpassword");
Statement updateStatement = connection.createStatement()) {
updateStatement.executeUpdate("update employees set salary = salary * 1.05");
}
For further details see creating a database connection