Vous êtes sur la page 1sur 10

ConexionBd.

java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package utilidades;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author Kevin Arnold
*/
public class ConexionDB {
public static Connection GetConnection()
{
String servidor;
Connection conexion=null;
try
{
Class.forName("org.postgresql.Driver");
servidor = "jdbc:postgresql://localhost:5432/catalogo";
String usuarioDB="postgres";
String passwordDB="admin";
conexion= DriverManager.getConnection(servidor,usuarioDB,passwordDB)
;
}
catch(ClassNotFoundException ex)
{
System.out.println("error en la conexion con la base de datos");
conexion=null;
}
catch(SQLException ex)
{
System.out.println("error2 en la conexion con la base de datos");
conexion=null;
}
catch(Exception ex)
{
System.out.println("error3 en la conexion con la base de datos");
conexion=null;
}
finally
{
return conexion;
}
}
}
Componentemenu.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package utilidades;
/**
*
* @author programador
*/
public class ComponenteMenu {
private String alimento,calorias,proteinas,grasa;
public ComponenteMenu() {
}
public String getGrasa() {
return grasa;
}
public void setGrasa(String grasa) {
this.grasa = grasa;
}
public String getAlimento() {
return alimento;
}
public void setAlimento(String alimento) {
this.alimento = alimento;
}
public String getCalorias() {
return calorias;
}
public void setCalorias(String calorias) {
this.calorias = calorias;
}
public String getProteinas() {
return proteinas;
}
public void setProteinas(String proteinas) {
this.proteinas = proteinas;
}
}
Controldieta.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package utilidades;

import
import
import
import
import
import
import
import
import

java.sql.Connection;
java.sql.SQLException;
java.util.ArrayList;
java.util.List;
java.util.logging.Level;
java.util.logging.Logger;
javax.faces.bean.ManagedBean;
javax.faces.bean.SessionScoped;
javax.faces.bean.ViewScoped;

/**
*
* @author programador
*/
@ManagedBean
@SessionScoped
public class ControlDieta {
/**
* Creates a new instance of ControlDieta
*/
private String regimiento,regimiento2;
private String batallon,batallon2;
private String fecha;
private String tpComida,tipComida2;
private String momentoComida,momentoComida2;
private String numSoldado,alimento;
private int proteinas,calorias,idAlimento,cantidad,conceMenu;
private float caloriaXsoldado,proteinasXsoldado,sumCalorias,sumProteinas;
private List<ComponenteMenu> lcm = new ArrayList<>();
public ControlDieta() {
calorias=0;proteinas=0;
}
public List<ComponenteMenu> getLcm() {
return lcm;
}
public void setLcm(List<ComponenteMenu> lcm) {
this.lcm = lcm;
}
public float getCaloriaXsoldado() {
return caloriaXsoldado;
}
public void setCaloriaXsoldado(float caloriaXsoldado) {
this.caloriaXsoldado = caloriaXsoldado;
}
public float getProteinasXsoldado() {
return proteinasXsoldado;
}
public void setProteinasXsoldado(float proteinasXsoldado) {
this.proteinasXsoldado = proteinasXsoldado;
}
public float getSumCalorias() {

return sumCalorias;
}
public void setSumCalorias(float sumCalorias) {
this.sumCalorias = sumCalorias;
}
public float getSumProteinas() {
return sumProteinas;
}
public void setSumProteinas(float sumProteinas) {
this.sumProteinas = sumProteinas;
}

public String getAlimento() {


return alimento;
}
public void setAlimento(String alimento) {
this.alimento = alimento;
}
public int getCantidad() {
return cantidad;
}
public void setCantidad(int cantidad) {
this.cantidad = cantidad;
}
public int getProteinas() {
return proteinas;
}
public void setProteinas(int proteinas) {
this.proteinas = proteinas;
}
public int getCalorias() {
return calorias;
}
public void setCalorias(int calorias) {
this.calorias = calorias;
}
public int getIdAlimento() {
return idAlimento;
}
public void setIdAlimento(int idAlimento) {
this.idAlimento = idAlimento;
}
public String getMomentoComida2() {

return momentoComida2;
}
public void setMomentoComida2(String momentoComida2) {
this.momentoComida2 = momentoComida2;
}
public String getTipComida2() {
return tipComida2;
}
public void setTipComida2(String tipComida2) {
this.tipComida2 = tipComida2;
}
public String getBatallon2() {
return batallon2;
}
public void setBatallon2(String batallon2) {
this.batallon2 = batallon2;
}

public String getRegimiento2() {


return regimiento2;
}
public void setRegimiento2(String regimiento2) {
this.regimiento2 = regimiento2;
}
public String getRegimiento() {
return regimiento;
}
public void setRegimiento(String regimiento) {
this.regimiento = regimiento;
}
public String getBatallon() {
return batallon;
}
public void setBatallon(String batallon) {
this.batallon = batallon;
}
public String getFecha() {
return fecha;
}
public void setFecha(String fecha) {
this.fecha = fecha;
}
public String getTpComida() {
return tpComida;
}

public void setTpComida(String tpComida) {


this.tpComida = tpComida;
}
public String getMomentoComida() {
return momentoComida;
}
public void setMomentoComida(String momentoComida) {
this.momentoComida = momentoComida;
}
public String getNumSoldado() {
return numSoldado;
}
public void setNumSoldado(String numSoldado) {
this.numSoldado = numSoldado;
}
public String comprobarDieta() throws SQLException{
regimiento2="";batallon2="";tipComida2="";momentoComida2="";
int rs1=0,concecutivo=0;
Connection c;
String StrQuery;
c = ConexionDB.GetConnection();
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
if (c != null) {
try {
StrQuery = " select * from regimiento where id='" + regimiento.tri
m() + "'";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIV
E, java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(StrQuery);
if (rs.next()) {
regimiento2=rs.getString(2);
//c.close();
}
} catch (SQLException ex) {
Logger.getLogger(ControlDieta.class.getName()).log(Level.SEVERE, n
ull, ex);
}
try {
StrQuery = "SELECT * FROM batallon where id_b='" + batallon.trim(
) + "'";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIV
E, java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(StrQuery);
if (rs.next()) {
batallon2=rs.getString(4);
//c.close();
}
} catch (SQLException ex) {
Logger.getLogger(ControlDieta.class.getName()).log(Level.SEVERE, n
ull, ex);
}
try {

StrQuery = "select * from alimento where id_a='" + tpComida.trim()


+ "'";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIV
E, java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(StrQuery);
if (rs.next()) {
tipComida2=rs.getString(5);
//c.close();
}
} catch (SQLException ex) {
Logger.getLogger(ControlDieta.class.getName()).log(Level.SEVERE, n
ull, ex);
}
try {
StrQuery = "select * from comida where idcomida='" + momentoComida
.trim() + "'";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIV
E, java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(StrQuery);
if (rs.next()) {
momentoComida2=rs.getString(3);
//c.close();
}
} catch (SQLException ex) {
Logger.getLogger(ControlDieta.class.getName()).log(Level.SEVERE, n
ull, ex);
}
c.close();rs.close();stmt.close();
}
if(regimiento2.isEmpty() || batallon2.isEmpty() || tipComida2.isEmpty()
|| momentoComida2.isEmpty()){
if(regimiento2.isEmpty()){
regimiento2="No se Encontro Regimiento";
}
if(batallon2.isEmpty()){
batallon2="No se Encontro Batallon";
}
if(tipComida2.isEmpty()){
tipComida2="No se Encontro Tipo Comida";
}
if(momentoComida2.isEmpty()){
momentoComida2="No se Encontro Momento de la Comida";
}
return "buscarDatos_1.xhtml";
}else{
c = ConexionDB.GetConnection();int conce=1;
if (c != null) {
StrQuery = "select max(idmenu) FROM menu";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITI
VE, java.sql.ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(StrQuery);
if(rs.next()){
concecutivo=Integer.parseInt(rs.getString(1));
conce=concecutivo+1;
conceMenu=conce;

}
StrQuery = "insert into menu(idMenu,idTipoComida,idMomentoComida,
fecha) values('"+conce+"','"+tpComida+"','"+momentoComida+"','"+fecha+"')";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSIT
IVE, java.sql.ResultSet.CONCUR_READ_ONLY);
rs1 = stmt.executeUpdate(StrQuery);
if(rs1>0){
System.out.println("inserto");
c.close();stmt.close();
}else{
System.out.println("no inserto");
}
}
return "buscarDatos.xhtml";
}
}
public String buscaPagcomponente(){
return "componente.xhtml";
}
public String sumarAlimentos(){
int conce = 0,concecutivo;
Connection c;
String StrQuery;
c = ConexionDB.GetConnection();
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
if (c != null) {
try {
StrQuery = "SELECT alimento,calorias, proteinas FROM alimento wh
ere id_a='" + idAlimento + "'";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSIT
IVE, java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(StrQuery);
if (rs.next()) {
alimento=rs.getString(1);
calorias=Integer.parseInt(rs.getString(2));
proteinas=Integer.parseInt(rs.getString(3));
if(idAlimento!=0){
//aqui s insertan los componentes del menu
StrQuery = "select max(idcommenu) FROM componenteMenu";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_I
NSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(StrQuery);
if(rs.next()){
if(rs.getString(1)!=null){
concecutivo=Integer.parseInt(rs.getString(1));
conce=concecutivo+1;
}else{
conce=1;
}
}
StrQuery = "insert into componenteMenu(idcommenu,idmenu,i
dalimento) values('"+conce+"','"+conceMenu+"','"+idAlimento+"')";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_I
NSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
int rs5=stmt.executeUpdate(StrQuery);

if(rs5>0){
System.out.println("inserto????? componenteMenu");
}else{
System.out.println("No *****inserto componenteMenu");
}
sumCalorias=sumCalorias+calorias*cantidad;
sumProteinas=sumProteinas+proteinas*cantidad;
}
//c.close();
}
c.close();stmt.close();rs.close();
} catch (SQLException ex) {
Logger.getLogger(ControlDieta.class.getName()).log(Level.SEVERE,
null, ex);
}
}
if(idAlimento==0){
caloriaXsoldado=sumCalorias/Integer.parseInt(numSoldado);
proteinasXsoldado=sumProteinas/Integer.parseInt(numSoldado);
return "total.xhtml";
}else{
return "introducirMasComponente.xhtml";
}
}
public String compMenu(){
int rs1,conce=1,idMenu = 0;List<String> lis = new ArrayList<>();
Connection c;
String StrQuery;
c = ConexionDB.GetConnection();
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
if (c != null) {
try {
//aqui se debe insertar en el menu
StrQuery = "SELECT idmenu\n" +
" FROM menu as m order by idmenu asc";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSIT
IVE, java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(StrQuery);
System.out.println("tamao");
while(rs.next()){
idMenu=Integer.parseInt(rs.getString(1));
System.out.println("dele--"+rs.getString(1));
}
System.out.println("!!!!!!!"+idMenu);
StrQuery = "select distinct idAlimento from componenteMenu where
idmenu="+idMenu+"";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSIT
IVE, java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(StrQuery);
while(rs.next()){
System.out.println("mico");
//
lis.add(rs.getString(1));
StrQuery = "SELECT alimento,calorias, proteinas, grasas FROM
alimento where id_a='" + rs.getString(1) + "'";
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSE
NSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(StrQuery);

if(rs.next()){
ComponenteMenu d = new ComponenteMenu();
d.setAlimento(rs.getString(1));
d.setCalorias(rs.getString(2));
d.setProteinas(rs.getString(3));
d.setGrasa(rs.getString(4));
lcm.add(d);
}
}
//
for(String x:lis){
////
ComponenteMenu d = new ComponenteMenu();
////
d.set
//
System.out.println("xxxxxx---"+x);
//
StrQuery = "SELECT alimento,calorias, proteinas, grasas FR
OM alimento where id_a='" + x + "'";
//
stmt = c.createStatement(java.sql.ResultSet.TYPE_SCROLL_IN
SENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
//
rs = stmt.executeQuery(StrQuery);
//
if(rs.next()){
//
ComponenteMenu d = new ComponenteMenu();
//
d.setAlimento(rs.getString(1));
//
d.setCalorias(rs.getString(2));
//
d.setProteinas(rs.getString(3));
//
d.setGrasa(rs.getString(4));
//
lcm.add(d);
//
}
//
//
}
c.close();rs.close();stmt.close();
} catch (SQLException ex) {
Logger.getLogger(ControlDieta.class.getName()).log(Level.SEVERE,
null, ex);
}
}
return "reporte.xhtml";
}
public String borrar(){
lcm.clear();
return "index.xhtml";
}
}

Vous aimerez peut-être aussi