Anda di halaman 1dari 10

Escuela Especializada en Ingeniera

ITCA FEPADE
Escuela de ingeniera en computacin
Tcnico en ing.de sistemas informticos
EJERCICIO DE JAVA
DESARROLLO DE APLICACIONES MULTIPLATAFORMA

PRESENTADO POR:

Oscar Ernesto ngel Cortez


Kevin Ernesto Ramrez Gmez
Johana Beatriz Rivera Ayala
Roberto Antonio Ortiz Reyes

Docente: Ing. Jhony Mikel Escobar Galdmez

Grupo de clase: SIS02 B

USUARIO SERVLET
package controlador;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modeloDAO.UsuarioDAO;
import uml.Usuario;
public class UsuarioServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
RequestDispatcher rd = null;
UsuarioDAO userdao = new UsuarioDAO();
Usuario user = new Usuario();
try {
if (request.getParameter("btnAsk") != null) {
user.setCorreo(request.getParameter("txtCorreo"));
user.setPsw(request.getParameter("txtPsw"));
String rol = userdao.validar(user);
if (rol == null) {
request.setAttribute("login", "Credenciales erroneas");
rd=request.getRequestDispatcher("index.jsp");
}else if (rol.equals("administrador")) {
rd=request.getRequestDispatcher("insertar.jsp");
}else if (rol.equals("vendedor")) {
rd=request.getRequestDispatcher("facturar.jsp");
}
} else if (request.getParameter("btnAdd") != null) {
user.setCorreo(request.getParameter("txtCorreo"));
user.setPsw(request.getParameter("txtPsw"));
user.setRol(request.getParameter("txtRol"));
request.setAttribute("resp", userdao.insertar(user));
rd=request.getRequestDispatcher("insertar.jsp");
}
} catch (Exception e) {
request.setAttribute("error", e.toString());
rd=request.getRequestDispatcher("error.jsp");
}

rd.forward(request, response);
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the
code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

USUARIO DAO
package modeloDAO;
import java.sql.*;
import java.util.logging.Logger;
import javax.ws.rs.NotFoundException;
import uml.Usuario;
public class UsuarioDAO {
Logger log = Logger.getLogger(getClass().getName());
ConnBD bd = new ConnBD();
public UsuarioDAO() {
}
public String validar(Object obj) {
Usuario us = (Usuario) obj;
String rol = null;
try {
Class.forName(bd.getDriver());
Connection cn = DriverManager.getConnection(
bd.getUrl(),
bd.getUser(),
bd.getPsw());
String sql = "select * from usuarios where correo=? and password=?";
PreparedStatement pst = cn.prepareStatement(sql);
pst.setString(1, us.getCorreo());
pst.setString(2, us.getPsw());
ResultSet rs = pst.executeQuery();
while (rs.next()) {
rol = rs.getString("rol");
}
rs.close();
pst.close();
cn.close();
} catch (SQLException e) {
log.severe(e.toString());
} catch (Exception e) {
log.severe(e.toString());
}
return rol;
}
public String insertar(Object obj) {
Usuario us = (Usuario) obj;
String msj = null;
try {
Class.forName(bd.getDriver());
Connection cn = DriverManager.getConnection(
bd.getUrl(),
bd.getUser(),
bd.getPsw());
String sql = "insert usuarios values(?,?,?)";
PreparedStatement pst = cn.prepareStatement(sql);
pst.setString(1, us.getCorreo());

pst.setString(2, us.getPsw());
pst.setString(3, us.getRol());
int fila = pst.executeUpdate();
msj = fila + " Registro fue insertado con exito";
pst.close();
cn.close();
} catch (SQLException e) {
log.severe(e.toString());
} catch (Exception e) {
log.severe(e.toString());
}
return msj;
}
}

CONBD

package modeloDAO;
public class ConnBD {
private String driver;
private String url;
private String user;
private String psw;
public ConnBD() {
this.driver = "com.mysql.jdbc.Driver";
this.url = "jdbc:mysql://localhost:3306/bdfactura";
this.user = "alumno";
this.psw = "Itca123!";
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}

}
CLIENTE UML
public class Cliente {
private String dui;
private String nombre;
public Cliente() {
}
public Cliente(String dui, String nombre) {
this.dui = dui;
this.nombre = nombre;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getDui() {
return dui;
}
public void setDui(String dui) {
this.dui = dui;
}
}

USUARIO UML
package uml;
public class Usuario {
private String Correo;
private String psw;
private String rol;
public Usuario() {
}
public Usuario(String Correo, String psw, String rol) {
this.Correo = Correo;
this.psw = psw;
this.rol = rol;
}
public String getRol() {
return rol;
}
public void setRol(String rol) {
this.rol = rol;
}
public String getCorreo() {
return Correo;
}
public void setCorreo(String Correo) {
this.Correo = Correo;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
}

BASE DE DATOS
create database bdFactura;
use bdFactura;
/*Esquema de la base de datos*/
create table usuarios(
correo varchar(50),
password varchar(20),
rol varchar(25),
constraint pk_Usuario primary key(correo)
);
create table Cliente(
DUI char(10) not null,
Nombre_Cliente varchar(60) not null,
constraint pk_Cliente primary key(DUI)
);
create table Producto(
Id_Producto char(9) not null,
Nombre_producto varchar(50) not null,
Precio float not null,
stock int not null,
existencia int not null,
constraint pk_Producto primary key(Id_Producto)
);
create table Factura(
Id_Factura char(6) not null,
fecha date not null,
DUI char(10) not null,
constraint pk_Factura primary key(Id_Factura),
constraint fk_Factura foreign key(DUI) references Cliente(DUI)
);
create table Detalle_Factura(
Id_Factura char(6) not null,
Id_Producto char(9) not null,
cant int not null,
constraint pk_Detalle_Factura primary key(Id_Factura, Id_Producto),
constraint fk1_Detalle_Factura foreign key(Id_Factura) references Factura(Id_Factura),
constraint fk2_Detalle_Factura foreign key(Id_Producto) references Producto(Id_Producto)
);
/*Datos d eprueba*/
insert into Cliente values
('12345678-9', 'Jhony Mikel Escobar'),
('21345678-9', 'Kevin ramirez'),

('31245678-9', 'oscar Ernesto angel');


insert into Producto values
('P0001', 'Placa Base', 98.00, 5, 12),
('P0002', 'Memoria RAM', 35.00, 5, 15),
('P0003', 'Disco Duro', 125.00, 5, 6),
('P0004', 'Tarjetas de Red', 22.00, 5, 7);
insert into Factura values
('012345', '2013-09-07', '12345678-9'),
('012346', '2013-09-08', '21345678-9'),
('012347', '2013-09-13', '31245678-9');
insert into Detalle_Factura values
('012345', 'P0002', 1),
('012345', 'P0003', 2),
('012345', 'P0004', 2),
('012346', 'P0001', 1),
('012347', 'P0002', 3),
('012347', 'P0004', 3);
select c.`DUI`, c.`Nombre_Cliente`, f.`Id_Factura`, f.fecha,
p.`Nombre_producto`, p.`Precio`, d.cant, (p.`Precio`* d.cant) as Total
from (((cliente c join factura f on c.`DUI` = f.`DUI`)join
detalle_factura d on f.`Id_Factura` = d.`Id_Factura`)join
producto p on d.`Id_Producto` = p.`Id_Producto`)
where f.`Id_Factura`='012345';

Anda mungkin juga menyukai