miércoles, 4 de marzo de 2009

Codigo Java N1: pryMenuTabla ProductoDao

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package prymenu;
import java.util.*;
import javax.swing.*;
/**
*
* @author Administrador
*/
public class ProductoDao extends Conectar{
Vector caja=new Vector();

public void modificar(ProductoBean o){
try{
sql="update tg_producto set cod_categoria='"+o.getCod_categoria()+"',"+
"cod_producto='"+o.getTxt_codigo()+"', txt_producto='"+o.getTxt_nombre()+
"' where cod_producto='"+o.getTxt_codigo()+"'";
stmt=cn.createStatement();
fila=stmt.executeUpdate(sql);
stmt.close();
}catch(Exception e){
System.out.println("Error= "+e.getMessage());
}
}

public void registrar(ProductoBean o){
try{
sql="insert into tg_producto (cod_categoria,cod_producto,txt_producto) values ('"+o.getCod_categoria()+"',"+
"'"+o.getTxt_codigo()+"','"+o.getTxt_nombre()+"')";
stmt=cn.createStatement();
fila=stmt.executeUpdate(sql);
stmt.close();
}catch(Exception e){
System.out.println("Error= "+e.getMessage());
}
}
//Eliminar
public void eliminar(ProductoBean o){
try{
sql="delete from tg_producto where cod_producto= '" + o.getTxt_codigo()
+ "'"; // and txt_clave = '" + o.getTxt_clave() + "'";
stmt=cn.createStatement();
fila=stmt.executeUpdate(sql);
stmt.close();
}catch(Exception e){
System.out.println("Error= "+e.getMessage());
}
}

public Vector listProducto(){
caja.removeAllElements();
try{
sql="select a.txt_categoria as cate, b.cod_producto as prod, b.txt_producto as nom, b.cod_categoria as cate2"
+ " from tg_producto b left join tg_categoria a on b.cod_categoria = a.cod_categoria"
+ " order by cod_producto";
System.out.println("Cons= "+sql);
stmt=cn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
ProductoBean u=new ProductoBean();
u.setTxt_categoria(rs.getString("cate"));
u.setTxt_codigo(rs.getString("prod"));
u.setTxt_nombre(rs.getString("nom"));
u.setCod_categoria(rs.getString("cate2"));
caja.addElement(u);
}
rs.close();
stmt.close();
}catch(Exception e){
System.out.println("Error= "+e.getMessage());
}
return caja;
}

public boolean encProducto(ProductoBean o){
boolean existe=false;
try{
sql="select * from tg_producto where cod_producto like '%"+o.getTxt_codigo()+"%'" +
" and txt_producto like '%"+o.getTxt_nombre()+"%'";
stmt=cn.createStatement();
rs=stmt.executeQuery(sql);
if (rs.next()){
o.setTxt_nombre(rs.getString("txt_producto"));
existe=true;
}
rs.close();
stmt.close();
}catch(Exception e){
System.out.println("Error= "+e.getMessage());
}
return existe;
}

}

No hay comentarios: