@author Moska
*/
public class HabitanteDAOImp implements HabitanteDAO{
//conexão
final String DB_URL=“jdbc:postgresql://localhost:5432/bdpromayor”;
public final static String USER=“postgres”;
public final static String PASSWD=“postdba”;
Connection connection=null;
//variavl para obter a instancia do banco
private static HabitanteDAOImp instance=null;
//construtor da classe
private HabitanteDAOImp(){
try{
//1.Loading database driver
Class.forName(“org.postgresql.Driver”).newInstance();
//2.Creating a JDBC Connection
connection=DriverManager.getConnection(DB_URL,USER,PASSWD);
System.out.println("Conectado com sucesso!!!");
}catch (ClassNotFoundException cnfe){
cnfe.getMessage();
}catch (InstantiationException ie){
ie.getMessage();
}catch (IllegalAccessException iae){
iae.getMessage();
}catch (SQLException sqle){
sqle.getMessage();
}
}
//metodo para obter a instancia que realizara a conexao
public static HabitanteDAOImp getInstance(){
if (instance==null){
instance = new HabitanteDAOImp();
}
return instance;
}
public void cadastrar(Habitante habitante) {
PreparedStatement stmt=null;
try{
String sql=“Insert into habitante(nome_habitante,sobrenome_habitante,” +
“endereco_habitante,numero_habitante,bairro_habitante,”+
“cidade_habitante,cep_habitante,estado_habitante,”+
“telefone_habitante,telefone2_habitante,email_habitante,”+
“login_habitante,senha_habitante)”+
“values(?,?,?,?,?,?,?,?,?,?,?,?,?)”;
stmt=connection.prepareStatement(sql);
stmt.setString(1,habitante.getNome_habitante());
stmt.setString(2,habitante.getSobrenome_habitante());
stmt.setString(3,habitante.getEndereco_habitante());
stmt.setInt(4,habitante.getNumero_habitante());
stmt.setString(5,habitante.getBairro_habitante());
stmt.setString(6,habitante.getCidade_habitante());
stmt.setInt(7,habitante.getCep_habitante());
stmt.setString(8,habitante.getEstado_habitante());
stmt.setInt(9,habitante.getTelefone_habitante());
stmt.setInt(10,habitante.getTelefone2_habitante());
stmt.setString(11,habitante.getEmail_habitante());
stmt.setString(12,habitante.getLogin_habitante());
stmt.setString(13,habitante.getSenha_habitante());
stmt.execute();
System.out.println("Habitante Cadastrado com Sucesso.");
}catch(SQLException sqle){
System.out.println("Problemas ao Cadastrar Habitante. Erro: "+sqle.getMessage());
sqle.printStackTrace();
}finally{
try{
stmt.close();
}catch(SQLException e){
System.out.println("Problemas ao fechar o statement! Erro: "+e.getMessage());
}
}
}
public List listar() {
throw new UnsupportedOperationException(“Not supported yet.”);
}
public void excluir(Integer id_habitante) {
throw new UnsupportedOperationException(“Not supported yet.”);
}
public void alterar(Habitante habitante) {
throw new UnsupportedOperationException(“Not supported yet.”);
}
public List listarAluno(Integer id_habitante) {
throw new UnsupportedOperationException(“Not supported yet.”);
}