Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/03/2012, 17:08
palmach
 
Fecha de Ingreso: noviembre-2007
Mensajes: 37
Antigüedad: 17 años, 4 meses
Puntos: 0
Validar si existe para dos inputText

Buenas tardes quiero validar dos inputTex si ya estan en mi base de datos de mysql uno es serie de factura y el numero de factura pero nose como hacerlo con los dos inputText esto es lo que tengo:


Código:
package programa.validadores;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.validator.FacesValidator;

import programa.utils.Utilidades;


@FacesValidator("programa.validadores.ValidarNumeroFactura")


public class ValidarNumeroFactura implements Validator  {

	protected static final String SQL_CODIGO_FACTURA = 
		"select * from factura where no_factura = ?";
	
	public void validate(FacesContext facesContext, UIComponent arg1, Object value) throws ValidatorException
	{
		String nofacturas = (String)value;
		boolean error = false;
		
		Connection conexion = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;

		try
		{
			conexion = Utilidades.obtenerConexion(Utilidades.conexionprograma());
			pstm = conexion.prepareStatement(SQL_CODIGO_FACTURA);
			pstm.setString(1, nofacturas);
			rs = pstm.executeQuery();

			if(rs.next())
			{ error = true; }
			
		}
		catch(Exception e)
		{ e.printStackTrace(); }
		finally
		{
			try
			{
				if(rs != null)
				{
					rs.close();
					rs = null;
				}
			}
			catch(Exception eRs)
			{ eRs.printStackTrace(); }

			try
			{
				if(pstm != null)
				{
					pstm.close();
					pstm = null;
				}
			}
			catch(Exception ePs)
			{ ePs.printStackTrace(); }

			try
			{
				if(conexion != null)
				{
					conexion.close();
					conexion = null;
				}
			}
			catch(Exception eCn)
			{ eCn.printStackTrace(); }
		}
		
		if(error)
		{ 
			FacesMessage fm = new FacesMessage("Ya existe la factura: " + nofacturas, "message");
			throw new ValidatorException(fm);
			
		}
		
	}

}

Última edición por palmach; 30/03/2012 a las 09:38