How to query select and call the connection from within

Asked

Viewed 605 times

0

I need help for my dao structure, as I call the connection, how I select within the function and how I list in my main the information coming from select.

As it is so far:

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
    }
}

1 answer

2


One of the best ways is to make your class Conexaodb method return a Connection using 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 Task class connect the database in 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 Task object and call the method listaTarefas() 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 some things that need to be highlighted:

From what I understand you want to return the information of various tasks, but passes only one object to the method listaTarefas() of your Dao being that should pass a list or simply pass nothing and just return to the list as I did.

Remember that by convention, method names start with lower case java letter, just like your controller’s correct name would be TarefasController.

  • Got it, I don’t move much with java can show me how to send this data to the view, an example ?

  • What kind of view do you have? JSP, swing, fxml?

  • View is JSP type

  • You pass your controller through the method setAttribute of the interface ServletRequest, and then access via JSP like this: ${tarefa.getNrPlaca}. But that still depends on many factors, will use spring? If not, how well do you know about ServletRequest? Already have the views created? This part is more complex, maybe it is the subject of another question.

  • Okay, I’ll see if I can ask another question about that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.