Codigos java bean

Rampage94

Lanero Novato
25 May 2013
3
MODIFICAR DATOS

INDEX:

<body>
<form method="post" action="index.jsp">
<input type="text" name="id">
<input type="submit" name="busqueda" value="Buscar">
</form>

<% if (request.getParameter("busqueda")!=null){
int vid = Integer.parseInt(request.getParameter("id"));
%>
<jsp:useBean id="obj" scope="page" class="caja.Persona">
<jsp:setProperty name="obj" property="id" value="<%=vid%>"></jsp:setProperty>
<jsp:getProperty name="obj" property="mostrar"></jsp:getProperty>
<form method="post" action="index.jsp">
<input type="text" name="nombre" value="<%=obj.getNombre()%>">
<input type="hidden" name="id" value="<%=obj.getId()%>">
<input type="submit" name="actualizar" value="Modificar">
</form>
</jsp:useBean>

<%
}
%>

<% if(request.getParameter("actualizar")!=null){
int aid= Integer.parseInt(request.getParameter("id"));
String anom = request.getParameter("nombre");
%>

<jsp:useBean id="obj2" scope="page" class="caja.Persona">
<jsp:setProperty name="obj2" property="id" value="<%=aid%>"></jsp:setProperty>
<jsp:setProperty name="obj2" property="nombre" value="<%=anom%>"></jsp:setProperty>
<jsp:getProperty name="obj2" property="actualiza"></jsp:getProperty>
</jsp:useBean>

<%
}
%>
</body>
</html>

---------------------------------------------------------------------------------------------------
JAVA CLASS PERSONA:

public class Persona {
private int id;
private String Nombre;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getNombre() {
return Nombre;
}

public void setNombre(String Nombre) {
this.Nombre = Nombre;
}

public String getMostrar(){
String error ="";
try{
Negocio o = new Negocio();
ResultSet rsm= o.mostrarCampos(getId());
if(rsm.next()){
this.setId(rsm.getInt("id"));
this.setNombre(rsm.getString("nombre"));
}
}catch(SQLException ex){
error= ex.getMessage();
}catch(Exception ex){
error= ex.getMessage();
}
return error;
}

public String getActualiza(){
String resultado ="";
try{
Negocio o = new Negocio();
resultado= o.enviaModificacion(getId(), getNombre());


}catch(Exception ex){
resultado= ex.getMessage();
}
return resultado;
}

}
-----------------------------------------------------------------------------------------------
JAVA CLASS NEGOCIO:

public class Negocio {
Connection cn;
public void conectar(){
try{
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bdultimoej?zeroDateTimeBehavior=convertToNull", "root", "412423");
}catch(ClassNotFoundException ex){
System.out.println("Error");
}catch(SQLException ex){
System.out.println("Error");
}
}

public ResultSet mostrarCampos(int id){
conectar();
ResultSet rs = null;
try{
Statement query = cn.createStatement();
rs = query.executeQuery("SELECT * FROM persona WHERE id= '"+id+"'");
}catch(SQLException ex){
System.out.println("Error");
}
return rs;
}

public String enviaModificacion(int vid, String vnom){
conectar();
String resp = null;
try{
Statement query = cn.createStatement();
int op= query.executeUpdate("UPDATE persona SET nombre = '"+vnom+"' WHERE id ='"+vid+"'");
if(op>0){
resp = "Actualizacion satisfactoria";
}else{
resp = "No se pudo actualizar";
}
}catch(SQLException ex){
resp = "Error"+ ex.getMessage();
}
return resp;
}
}
---------------------------------------------------------------------------------------------------------
SUELDO Y DESCUENTO

INDEX:

<body>
<form method="post" action="index.jsp">
<table border="1" withd="30">
<tr>
<td>Sueldo base:<input type="text" name="suel"><br></td>
</tr>
<tr>
<td>Bono 1:<input type="text" name="bon1"><br></td>
</tr>
<td>Bono2:<input type="text" name="bon2"><br></td>
</tr>
<tr>
<td>Descuento AFP:<input type="text" name="desc_afp"><br></td>
</tr>
<td>Descuento Agua:<input type="text" name="desc_agua"><br></td>
</tr>
<tr>
<td>Descuento Luz:<input type="text" name="desc_luz"><br></td>
</tr>
<tr>
<td><input type="submit" name="ingresar" value="enviar"></td>


</tr>
</table>
</form>
<% if(request.getParameter("ingresar")!=null){
int s = Integer.parseInt(request.getParameter("suel"));
int b1 = Integer.parseInt(request.getParameter("bon1"));
int b2 = Integer.parseInt(request.getParameter("bon2"));
int d_afp = Integer.parseInt(request.getParameter("desc_afp"));
int d_agu = Integer.parseInt(request.getParameter("desc_agua"));
int d_luz = Integer.parseInt(request.getParameter("desc_luz"));
%>
<jsp:useBean id="obj" scope="page" class="data.sueldo">
<jsp:setProperty name="obj" property="sueldo_base" value="<%=s%>"></jsp:setProperty>
<jsp:setProperty name="obj" property="bono1" value="<%=b1%>"></jsp:setProperty>
<jsp:setProperty name="obj" property="bono2" value="<%=b2%>"></jsp:setProperty>
<jsp:setProperty name="obj" property="descuento_afp" value="<%=d_afp%>"></jsp:setProperty>
<jsp:setProperty name="obj" property="descuento_agua" value="<%=d_agu%>"></jsp:setProperty>
<jsp:setProperty name="obj" property="descuento_luz" value="<%=d_luz%>"></jsp:setProperty>
<jsp:getProperty name="obj" property="respuesta"></jsp:getProperty>
</jsp:useBean>

<%}%>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------
JAVA CLASS SUELDO:

public class sueldo {

public int sueldo_base;
public int bono1;
public int bono2;
public int descuento_afp;
public int descuento_agua;
public int descuento_luz;

public int getSueldo_base() {
return sueldo_base;
}

public void setSueldo_base(int sueldo_base) {
this.sueldo_base = sueldo_base;
}

public int getBono1() {
return (this.bono1);
}

public void setBono1(int bono1) {
this.bono1 = bono1;
}

public int getBono2() {
return (this.bono2);
}

public void setBono2(int bono2) {
this.bono2 = bono2;
}

public int getDescuento_afp() {
return (this.descuento_afp);
}

public void setDescuento_afp(int descuento_afp) {
this.descuento_afp = descuento_afp;
}

public int getDescuento_agua() {
return (this.descuento_agua);
}

public void setDescuento_agua(int descuento_agua) {
this.descuento_agua = descuento_agua;
}

public int getDescuento_luz() {
return (this.descuento_luz);
}

public void setDescuento_luz(int descuento_luz) {
this.descuento_luz = descuento_luz;
}

public int getBono(){
return (this.bono1 + this.bono2);
}

public int getDescuento(){
return (this.descuento_afp + this.descuento_agua + this.descuento_luz);
}

public int getSueldo_liquido(){
return this.sueldo_base-(getDescuento())+ (getBono());
}

public String getRespuesta(){
ingreso ingresillo = new ingreso();
return ingresillo.Ingresar(getSueldo_base(),getBono(),getDescuento(),getSueldo_liquido());
}

}
-----------------------------------------------------------------------------------------------------------
JAVA CLASS INGRESO:

public class ingreso {
Connection cn;
Statement query;

public void conectar(){
try{

Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bdsueldo?zeroDateTimeBehavior=convertToNull","root","412423");

}catch(ClassNotFoundException ex){
System.out.print(ex.getMessage());
}catch(SQLException ex){
System.out.print(ex.getMessage());
}
}

public String Ingresar(int suel, int bon, int desc, int suel_l){
String mensaje= null;
try{
conectar();
query = cn.createStatement();
int resp = query.executeUpdate("INSERT INTO descuento (sueldo_b,bono,descuento,sueldo_liquido) VALUES ('"+suel+"','"+bon+"','"+desc+"','"+suel_l+"')");
if(resp > 0){
mensaje = "Ingreso Satisfactorio";
}else{
mensaje = "Intentelo de nuevo";
}
}catch(SQLException ex){
mensaje = ex.getMessage();
}catch(Exception ex){
mensaje = ex.getMessage();
}
return mensaje;
}

}
----------------------------------------------------------------------------------------------------------------
SACAR PROMEDIO:

INDEX:

<body>

<jsp:useBean scope="page" id="objeto" class="ramos.notas">

<jsp:setProperty name="objeto" property="nota" value="4.0"></jsp:setProperty>

<%= objeto.getClass().getSuperclass()%>
<jsp:getProperty name="objeto" property="nota"></jsp:getProperty>
<jsp:getProperty name="objeto" property="resultado"></jsp:getProperty>
</jsp:useBean>

</body>
</html>

-------------------------------------------------------------------------------------------------

PROMEDIO JSP:

<body>
<form method="post" action="Promedio.jsp">
<input type="text" name="nom">
<input type="text" name="nom2">
<input type="text" name="nom3">
<input type="submit" name="btn" value="enviar">
</form>
<%
if(request.getParameter("btn")!=null){
float nota1 = Float.parseFloat(request.getParameter("nom"));
float nota2 = Float.parseFloat(request.getParameter("nom2"));
float nota3 = Float.parseFloat(request.getParameter("nom3"));


%>

<jsp:useBean scope="page" id="objeto" class="ramos.promedio">

<jsp:setProperty name="objeto" property="nota" value="<%=nota1%>"></jsp:setProperty>
<jsp:setProperty name="objeto" property="nota2" value="<%=nota2%>"></jsp:setProperty>
<jsp:setProperty name="objeto" property="nota3" value="<%=nota3%>"></jsp:setProperty>
<jsp:getProperty name="objeto" property="prom"></jsp:getProperty>

</jsp:useBean>

<%

}

%>
</body>
</html>
-------------------------------------------------------------------------------------------------------------
JAVA CLASS NOTAS:

public class notas {
private String nombre;
private float nota;

public String getNombre() {
return nombre;
}

public void setNombre(String nombre) {
this.nombre = nombre;
}

public float getNota() {
return nota;
}

public void setNota(float nota) {
this.nota = nota;
}
public String getresultado(){
if(this.nota < 4.0f){
return "reprobado";
}else
return "aprobado";

}
}
--------------------------------------------------------------------------------------------------
JAVA CLASS PROMEDIO:

public class promedio {
private float nota;
private float nota2;
private float nota3;

public float getNota() {
return nota;
}

public void setNota(float nota) {
this.nota = nota;
}

public float getNota2() {
return nota2;
}

public void setNota2(float nota2) {
this.nota2 = nota2;
}

public float getNota3() {
return nota3;
}

public void setNota3(float nota3) {
this.nota3 = nota3;
}

public float getProm() {

return (nota+nota2+nota3)/3;
}



}
 

Los últimos temas