Sou estudante de ciência da computação e estou iniciando no mundo dos projetos de software. Estou com uma dúvida com relação a implementação do acesso ao BD. Lendo algumas coisas na net descobri um padrão chamado Data Acess Object (DAO), mas n entendi muito bem o seu funcionamento.
Segue o código q fiz para o tal DAO e uma classe q implementei com o intúito de me fornecer PreperadedStatement´s a qual intitulei de QueryManager. Porém estou achando minha própria implementação confusa, com certeza pelo fato de não ter entendido o funcionamento do DAO, aproveito para pedir sugestões de como implementar esse acesso único ao BD, para evitar de q em todos os métodos eu carrege o Driver JDBC e chame o DriverManager passando url login e senha do BD. E outra pergunta =PPP Esse queryManager seria um Factory???
[code]package util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DAO {
private Properties properties;
private static DAO instance = null;
private DAO(){
properties = new Properties();
try {
String url = getClass().getResource("/conf/db/DAO.properties").toString().substring(6);
properties.load(new FileInputStream(url));
} catch (FileNotFoundException e) {
throw new ConfigurationErrorException();
} catch (IOException e) {
throw new ConfigurationErrorException();
}
}
public static DAO instance(){
if (instance == null){
instance = new DAO();
}
return instance;
}
public Connection getConnection(){
try {
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
} catch (ClassNotFoundException e) {
throw new ConfigurationErrorException();
} catch (SQLException e) {
throw new ConfigurationErrorException();
}
}
}
[/code]
package util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;
public class QueryManager {
private Properties queriesProperty;
private Connection connection;
public QueryManager(String fileURL){
queriesProperty = new Properties();
try {
String fileURLString = getClass().getResource(fileURL).toString().substring(6);
queriesProperty.load(new FileInputStream(fileURLString));
connection = DAO.instance().getConnection();
} catch (FileNotFoundException e) {
throw new ConfigurationErrorException();
} catch (IOException e) {
throw new ConfigurationErrorException();
}
}
public PreparedStatement getPreparedStatement(String properties){
try {
String query = queriesProperty.getProperty(properties);
PreparedStatement statement = connection.prepareStatement(query);
return statement;
} catch (SQLException e) {
throw new ConfigurationErrorException();
}
}
public void closeConnection(){
try {
connection.close();
} catch (SQLException e) {
throw new ConfigurationErrorException();
}
}
public static java.sql.Date convertDateToSQLDate(Date date){
long dateMillis = date.getTime();
return new java.sql.Date(dateMillis);
}
}