miércoles, 4 de marzo de 2009

Codigo Java N1: pryMenuTabla UsuarioDao

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

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

public void modificar(UsuarioBean o){
try{
sql="update tg_usuario set txt_clave='"+o.getTxt_clave()+"',"+
"txt_nombre='"+o.getTxt_nombre()+"'"+
" where cod_usuario='"+o.getCod_usuario()+"'";
stmt=cn.createStatement();
fila=stmt.executeUpdate(sql);
stmt.close();
}catch(Exception e){
System.out.println("Error= "+e.getMessage());
}
}

public void registrar(UsuarioBean o){
try{
sql="insert into tg_usuario(cod_usuario,txt_clave,txt_nombre) values ('"+o.getCod_usuario()+"',"+
"'"+o.getTxt_clave()+"','"+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(UsuarioBean o){
try{
sql="delete from tg_usuario where cod_usuario = '" + o.getCod_usuario()
+ "'"; // 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 listUsuario(){
caja.removeAllElements();
try{
sql="select * from tg_usuario order by cod_usuario";
stmt=cn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
UsuarioBean u=new UsuarioBean();
u.setCod_usuario(rs.getString("cod_usuario"));
u.setTxt_clave(rs.getString("txt_clave"));
u.setTxt_nombre(rs.getString("txt_nombre"));
caja.addElement(u);
}
rs.close();
stmt.close();
}catch(Exception e){
System.out.println("Error= "+e.getMessage());
}
return caja;
}

public boolean encUsuario(UsuarioBean o){
boolean existe=false;
try{
sql="select * from tg_usuario where cod_usuario like '%"+o.getCod_usuario()+"%'" +
" and txt_clave like '%"+o.getTxt_clave()+"%'";
stmt=cn.createStatement();
rs=stmt.executeQuery(sql);
if (rs.next()){
o.setTxt_nombre(rs.getString("txt_nombre"));
existe=true;
}
rs.close();
stmt.close();
}catch(Exception e){
System.out.println("Error= "+e.getMessage());
}
return existe;
}

}

No hay comentarios: