use of br.senac.tads3.pi03b.gruposete.models.Voo in project Loja_Agencia_De_Viagens by ArtCouSan.
the class VooDAO method getVooById.
public Voo getVooById(int id) throws SQLException, ClassNotFoundException {
Voo voo = new Voo();
String query = "SELECT * FROM Voo WHERE id_voo = ? ";
try {
connection = DbUtil.getConnection();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
voo.setId(resultSet.getInt("id_voo"));
voo.setData_ida(resultSet.getString("data_ida"));
voo.setData_volta(resultSet.getString("data_volta"));
voo.setDestino(resultSet.getString("destino"));
voo.setOrigem(resultSet.getString("origem"));
voo.setQuantidade_passagens(resultSet.getInt("quantidade_passagens"));
voo.setPreco(resultSet.getFloat("preco"));
}
} finally {
if (preparedStatement != null && !preparedStatement.isClosed()) {
preparedStatement.close();
}
if (connection != null && !connection.isClosed()) {
connection.close();
}
}
return voo;
}
use of br.senac.tads3.pi03b.gruposete.models.Voo in project Loja_Agencia_De_Viagens by ArtCouSan.
the class VendaDAO method procurarVoo.
public ArrayList<Voo> procurarVoo(String busca) throws SQLException, ClassNotFoundException {
// Conecta.
connection = DbUtil.getConnection();
ArrayList<Voo> listaResultado = new ArrayList<>();
// Comando SQL.
String slq = "SELECT * FROM voo WHERE ativo = true" + " AND (data_volta LIKE ?" + " OR data_ida LIKE ?" + " OR destino LIKE ?" + " OR origem LIKE ?" + " OR preco LIKE ?" + " ) AND quantidade_passagens > 0 LIMIT 5";
PreparedStatement stmt = connection.prepareStatement(slq);
// Insercoes.
stmt.setString(1, "%" + busca + "%");
stmt.setString(2, "%" + busca + "%");
stmt.setString(3, "%" + busca + "%");
stmt.setString(4, "%" + busca + "%");
stmt.setString(5, "%" + busca + "%");
// Executa e recebe resultado.
ResultSet result = stmt.executeQuery();
// Declara variaveis.
int id_voo;
String data_ida;
String data_volta;
String destino;
String origem;
float preco;
int quantidade_passagens;
// Loop com resultados.
while (result.next()) {
// Prenche.
id_voo = (result.getInt("id_voo"));
data_volta = (result.getString("data_volta"));
data_ida = (result.getString("data_ida"));
destino = (result.getString("destino"));
preco = (result.getFloat("preco"));
origem = (result.getString("origem"));
quantidade_passagens = (result.getInt("quantidade_passagens"));
Voo voo = new Voo(data_ida, data_volta, destino, origem, quantidade_passagens, preco, true);
voo.setId(id_voo);
listaResultado.add(voo);
}
// Fecha conexao.
connection.close();
// Retorna lista.
return listaResultado;
}
use of br.senac.tads3.pi03b.gruposete.models.Voo in project Loja_Agencia_De_Viagens by ArtCouSan.
the class VooDAO method ListarVoo.
public List<Voo> ListarVoo() throws SQLException, ClassNotFoundException {
List<Voo> ListaVoo = new ArrayList<>();
String query = "SELECT * FROM Voo WHERE ativo = true";
try {
connection = DbUtil.getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
Voo voo = new Voo();
voo.setId(resultSet.getInt("id_voo"));
voo.setData_ida(resultSet.getString("data_ida"));
voo.setData_volta(resultSet.getString("data_volta"));
voo.setDestino(resultSet.getString("destino"));
voo.setOrigem(resultSet.getString("origem"));
voo.setQuantidade_passagens(resultSet.getInt("quantidade_passagens"));
voo.setPreco(resultSet.getFloat("preco"));
ListaVoo.add(voo);
}
} finally {
if (preparedStatement != null && !preparedStatement.isClosed()) {
preparedStatement.close();
}
if (connection != null && !connection.isClosed()) {
connection.close();
}
}
return ListaVoo;
}
use of br.senac.tads3.pi03b.gruposete.models.Voo in project Loja_Agencia_De_Viagens by ArtCouSan.
the class VooDAO method procurarVoo.
public List<Voo> procurarVoo(String busca) throws SQLException, ClassNotFoundException {
List<Voo> listaResultado = new ArrayList<>();
String sql = "SELECT * FROM voo WHERE" + " (data_volta = ?" + " OR data_ida = ?" + " OR destino = ?" + " OR origem = ?" + " OR quantidade_passagens = ?" + " OR preco = ?)" + " AND ativo = true";
connection = DbUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
// Insercoes.
preparedStatement.setString(1, busca);
preparedStatement.setString(2, busca);
preparedStatement.setString(3, busca);
preparedStatement.setString(4, busca);
int n1 = 0;
try {
n1 = Integer.parseInt(busca);
} catch (NumberFormatException e) {
System.out.println("Erro");
}
float n2 = 0;
try {
n2 = Float.parseFloat(busca);
} catch (NumberFormatException e) {
System.out.println("Erro");
}
preparedStatement.setInt(5, n1);
preparedStatement.setFloat(6, n2);
try (ResultSet result = preparedStatement.executeQuery()) {
while (result.next()) {
Voo voos = new Voo();
voos.setId(result.getInt("id_voo"));
voos.setData_ida(result.getString("data_ida"));
voos.setData_volta(result.getString("data_volta"));
voos.setDestino(result.getString("destino"));
voos.setOrigem(result.getString("origem"));
voos.setQuantidade_passagens(result.getInt("quantidade_passagens"));
voos.setPreco(result.getFloat("preco"));
listaResultado.add(voos);
}
} finally {
if (preparedStatement != null && !preparedStatement.isClosed()) {
preparedStatement.close();
}
if (connection != null && !connection.isClosed()) {
connection.close();
}
}
return listaResultado;
}
use of br.senac.tads3.pi03b.gruposete.models.Voo in project Loja_Agencia_De_Viagens by ArtCouSan.
the class BuscaVooServlet method doPost.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
VooDAO dao = new VooDAO();
String pesquisa = request.getParameter("pesquisa");
try {
if ("".equals(pesquisa.trim())) {
List<Voo> encontrados = dao.ListarVoo();
request.setAttribute("encontrados", encontrados);
request.setAttribute("pesquisa", pesquisa);
} else {
List<Voo> encontrados = dao.procurarVoo(pesquisa);
request.setAttribute("encontrados", encontrados);
request.setAttribute("pesquisa", pesquisa);
}
RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/ListaVoo.jsp");
dispatcher.forward(request, response);
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(BuscaVooServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
Aggregations