How to do the query select and call the connection on dao

0

I need help to structure my dao, how do I call the connection, how do I select within the function, and how do I list in my main the information that comes from select.

As it is now:

Connection to the bank.

package Modal;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConexaoDB {

    Connection conexao = null;
    String driverDB = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String rotaDB = "jdbc:sqlserver//10.0.0.14:1433;databasename=dbteste;user=db;password=123";

    public boolean ConectarDB(){

        try{
            Class.forName(driverDB);
            conexao = DriverManager.getConnection(rotaDB);
            return true;
        } catch (Exception e) {
            System.out.println("Erro: Não foi possível se conectar ao banco de dados" + e.getMessage());

        }
    }
    return false;
}

Modal

package Modal;

public class Tarefas {

    public String NrPlaca;

    public String getNrPlaca() {
        return NrPlaca;
    }
    public void setNrPlaca(String nrPlaca) {
        NrPlaca = nrPlaca;
    }
}

Controller

package Cotroller;

import Dao.TarefasDao;
import Modal.Tarefas;

public class ControlTarefa {

    public void ListaTarefas(){
        Tarefas t = new Tarefas();
        TarefasDao tarefasdao = new TarefasDao();
        tarefasdao.ListaTarefas(t);
    }
}

dao

package Dao;

import Modal.Tarefas;

public class TarefasDao {

    public void ListaTarefas(Tarefas t){
        //chamar conexao com o banco
        //select
    }
}
    
asked by anonymous 15.05.2018 / 18:39

1 answer

2

One of the best ways is to have the method of your ConnectionDB class return a Connection through a static method. Ex.:

public static Connection conectaBD(){
    try {
        Class.forName(driverDB);
        conexao = DriverManager.getConnection(rotaDB);
    } catch (Exception e) {
        System.out.println("Não foi possível conectar ao bd" + e.getMessage());
    }
    return conexao;
}

Then in your TarefasDao class connect the database to the constructor and perform the SQL queries. Ex:

public class TarefasDao {

    Connection conexao;

    public TarefasDao() {
        conexao = ConexaoDB.conectaDB(); // utilização do método
    }

    public ArrayList<Tarefas> listaTarefas() {
        Statement sentenca;
        ResultSet registros;

        sentenca = conexao.createStatement();
        registros = sentenca.executeQuery("SELECT placa FROM tarefas");
        if (!registros.next()) {
            System.out.println("Nenhum registro encontrado");
        } else {
            int i = 0;
            List<Tarefas> tarefas = new ArrayList<Tarefas>();
            do {
                tarefas.add(new Tarefas());
                tarefas.get(i).setNrPlaca(registros.getString("placa"));
                i++;
            } while (registros.next());
            return tarefas;   
        }
        sentenca.close();
        return null;
    }
}

Then in your controller you can create a TarefasDao object and call the listaTarefas() method, and you will have a list of all the task boards:

public class ControlTarefa {

    public void ListarTarefas(){
        TarefasDao tarefasdao = new TarefasDao();
        List<Tarefas> tarefas = tarefasdao.listaTarefas();
    }
}

There are a few things that need to be highlighted:

So I understand you want to return the information of various tasks, but only passes an object to the listaTarefas() method of your Dao being that you should pass a list or just not pass anything and just return the list as I did .

Remember that by convention, method names start with a lowercase letter in java, just as the correct name of your controller would be TarefasController .

    
15.05.2018 / 20:04