Data persistence is essential in most applications. The objective of this project is to elaborate a basic structure of a project with JDBC and also implement the DAO(Data Acess Object) design pattern manually.
one of the main goals is to learn to access the data, making entries, data recovery, updates and deletions using the JDBC library.
This project implements a data access layer of a small system that relates all sellers to their respective departments.
public static Connection getConnetion() {
if (conn == null) {
try {
Properties props = loadProperties();
String url = props.getProperty("dburl");
conn = DriverManager.getConnection(url, props);
} catch (SQLException e) {
throw new DbException(e.getMessage());
}
}
return conn;
}
The loadProperties method is explained below:
private static Properties loadProperties() {
try (FileInputStream fs = new FileInputStream("db.properties")) {
Properties props = new Properties();
props.load(fs);
return props;
} catch (IOException e) {
throw new DbException(e.getMessage());
}
}
A file was created in the project root called db.properties, containing the database settings. The loadProperties method is responsible for reading this file and returning an object of type Properties that will be passed as a parameter in the getConnection function for connection to the database.