Download pararecapitular
Document related concepts
no text concepts found
Transcript
Dominio
package dominio;
import almacenamiento.*;
import java.sql.*;
public class Persona
{
protected String mPKNIF, mNombre;
public Persona() {
mPKNIF=new String();
mNombre=new String();
}
public Persona(String nif) throws Exception {
String SQL="Select NIF, Nombre from Persona “+
“where NIF='" + nif + "'";
ResultSet r=Agente.select(SQL);
if (r.next()) {
mPKNIF=r.getString(1);
mNombre=r.getString(2);
}
r.close();
}
public void insert() throws Exception {
String SQL="Insert into Persona (NIF, Nombre) values ('" +
mPKNIF + "', '" + mNombre + "')";
Agente.insert(SQL);
}
public void update() throws Exception {
throw new Exception("Este método tiene una cierta complejidad adicional: " +
"piénsese que ocurre si cambiamos el valor de la clave principal");
}
public void delete() throws Exception {
String SQL="Delete from Personas where NIF='" + mPKNIF + "'";
Agente.delete(SQL);
}
public String getNIF() {
return mPKNIF;
}
public String getNombre() {
return mNombre;
}
public void setNIF(String nif) {
mPKNIF=nif;
}
public void setNombre(String nombre) {
mNombre=nombre;
}
}
Dominio
package dominio;
import almacenamiento.*;
import java.sql.*;
public class Empleado extends Persona
{
protected String mPKNSS;
protected Double mSalario;
public Empleado() {
super();
mPKNSS=new String();
mSalario=new Double(0.0);
}
public Empleado(String NSS) throws Exception {
this();
String SQL="Select Persona.NIF, Persona.Nombre, " +
"Empleado.NSS, Empleado.Salario from Persona, Empleado " +
"where Persona.NIF=Empleado.NIF and NSS='" + NSS + "'";
ResultSet r=Agente.select(SQL);
if (r.next()) {
mPKNIF=r.getString(1);
mNombre=r.getString(2);
mPKNSS=r.getString(3);
mSalario=new Double(r.getDouble(4));
}
r.close();
}
public void insert() throws Exception {
super.insert();
String SQL="Insert into Empleado (NSS, NIF, Salario) values ('" +
mPKNSS + "', '" + mPKNIF + "', " + mSalario + ")";
Agente.insert(SQL);
}
public void update() throws Exception {
throw new Exception("Este método tiene una cierta complejidad adicional: " +
"piénsese que ocurre si cambiamos el valor de la clave principal, o si" +
"lo que cambiamos es un campo de una clase padre.\nPiénsese también " +
"qué pasa si hemos o no indicado ON DELETE CASCADE u " +
"ON UPDATE CASCADE en la base de datos");
}
public void delete() throws Exception {
// Como hay ON DELETE CASCADE, lo borramos de personas y el SGBD ya
// se encarga de borrarlo de Empleado. Si el borrado de un empleado no
// implica borrarlo de Persona, habría que replantearse el diseño de la BD.
String SQL="Delete from Persona where NIF='" + mPKNIF + "'";
Agente.delete(SQL);
}
public void setNSS(String NSS) {
mPKNSS=NSS;
}
public void setSalario(Double salario) {
mSalario=salario;
}
}
Dominio
package dominio;
import almacenamiento.*;
import java.sql.*;
public class Coche
{
protected String mPKMatricula, mMarca;
protected Persona mDueno;
public Coche() {
mPKMatricula=new String();
mMarca=new String();
mDueno=new Persona();
}
public Coche(String nif) throws Exception {
String SQL="Select Matricula, NIFDueño, Marca “+
“from Coche where Matricula='" + mPKMatricula + "'";
ResultSet r=Agente.select(SQL);
if (r.next()) {
mPKMatricula=r.getString(1);
mDueno=new Persona(r.getString(2));
mMarca=r.getString(3);
}
r.close();
}
public void insert() throws Exception {
String SQL="Insert into Coche (Matricula, NIFDueño, Marca) values ('" +
mPKMatricula + "', '" + mDueno.getNIF() + "', '" + mMarca + "')";
Agente.insert(SQL);
}
public void update() throws Exception {
throw new Exception("Este método tiene una cierta complejidad adicional: " +
"piénsese que ocurre si cambiamos el valor de la clave principal");
}
public void delete() throws Exception {
String SQL="Delete from Coches where Matricula='" + mPKMatricula + "'";
Agente.delete(SQL);
}
public String getMatricula() {
return mPKMatricula;
}
public String getMarca() {
return mMarca;
}
public Persona getDueno() {
return mDueno;
}
public void setMatricula(String matricula) {
mPKMatricula=matricula;
}
public void setMarca(String marca) {
mMarca=marca;
}
public void setDueno(Persona p) {
mDueno=p;
}
Dominio
}
package presf;
import java.awt.*;
import java.beans.*;
import dominio.Empleado;
public class FEmpleado extends java.awt.Frame
{
protected Empleado mEmpleado;
int operacion;
public FEmpleado()
{
...
}
...
private void cargaObjeto() throws Exception {
mEmpleado.setNIF(textFieldNombre.getText());
mEmpleado.setNombre(textFieldNombre.getText());
mEmpleado.setNSS(textFieldNSS.getText());
mEmpleado.setSalario(new Double(textFieldSalario.getText()));
}
void buttonNuevo_ActionPerformed(java.awt.event.ActionEvent event)
{
operacion=1;
mEmpleado=new Empleado();
vacia();
habilita(true);
}
void buttonModificar_ActionPerformed(java.awt.event.ActionEvent event)
{
operacion=0;
habilita(true);
}
void buttonGuardar_ActionPerformed(java.awt.event.ActionEvent event)
{
try {
cargaObjeto();
if (operacion==0) {
mEmpleado.update();
} else {
mEmpleado.insert();
}
habilita(false);
}
catch (Exception e) {
(new DError(this, e.toString())).setVisible(true);
habilita(false);
Presentación (frames)
}
}
void buttonBorrar_ActionPerformed(java.awt.event.ActionEvent event)
{
try {
mEmpleado.delete();
vacia();
}
catch (Exception e) {
(new DError(this, e.toString())).setVisible(true);
}
}
void buttonSalir_ActionPerformed(java.awt.event.ActionEvent event)
{
System.exit(0);
}
}
Presentación (web)
package press;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import almacenamiento.*;
import dominio.*;
public class SEmpleado extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html");
PrintWriter out = new PrintWriter(resp.getOutputStream());
out.println("<HTML>");
out.println("<HEAD><TITLE>Aulas Manuel Castells</TITLE></HEAD>");
out.println("<BODY bgcolor='yellow'>");
String boton=req.getParameter("boton");
if (boton==null || boton.length()!=0) {
try {
conecta();
out.println(getFormulario());
}
catch (Exception e) {
out.println("Error: " + e);
}
} else {
if (req.getParameter("boton").equals("Nuevo")) {
out.println(getFormulario());
} else if (req.getParameter("boton").equals("Guardar")) {
Empleado e=new Empleado();
e.setNombre(req.getParameter("nombre"));
e.setNIF(req.getParameter("nif"));
e.setNSS(req.getParameter("nss"));
e.setSalario(new Double(req.getParameter("nombre")));
try {
e.insert();
}
catch (Exception ex) {
out.println("Error: " + ex);
}
}
}
out.println("</font>");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
Presentación (web)
private void conecta() throws Exception {
Agente a=Agente.getAgente();
}
private String getFormulario() {
String r="<form action=press.SEmpleado method=get>" +
"Nombre: <input type=text name=nombre><br>" +
"NIF: <input type=text name=nif><br>" +
"Nº de SS: <input type=text name=nss><br>" +
"Salario: <input type=text name=salario><br>" +
"<input type=submit name=boton value=Nuevo>" +
"<input type=submit name=boton value=Modificar>" +
"<input type=submit name=boton value=Guardar>" +
"<input type=submit name=boton value=Borrar>" +
"</form>";
return r;
}
}
Almacenamiento
package almacenamiento;
import java.sql.*;
public class Agente
{
protected static Connection mBD;
protected static Agente mInstancia;
protected Agente() throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
mBD=DriverManager.getConnection("jdbc:odbc:ejemplo1");
}
public static Agente getAgente() throws Exception {
if (mInstancia==null) {
mInstancia=new Agente();
}
return mInstancia;
}
public static ResultSet select(String SQL) throws Exception {
return mBD.createStatement().executeQuery(SQL);
}
public static void insert(String SQL) throws Exception {
mBD.createStatement().executeUpdate(SQL);
}
public static void update(String SQL) throws Exception {
mBD.createStatement().executeUpdate(SQL);
}
public static void delete(String SQL) throws Exception {
mBD.createStatement().executeUpdate(SQL);
}
}
Related documents