Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Mantenimiento de Tablas
Mantenimiento de Tablas
PUNTAJE
5
5
5
5
CAPA DE DATOS
La clase HabitacionBd permite la conexin a la Base de Datos y las operaciones de
Mantenimiento(guardar,actualizar,eliminar,buscar).
HabitacionBD
import java.sql.*;
import javax.swing.*;
public class HabitacionBd {
Connection con = null;
Statement stm = null;
ResultSet res = null;
Habitacion hab = null;
public HabitacionBd() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("Jdbc:Odbc:dsnHotel","system","vandres");
stm = con.createStatement();
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error Controlador",
JOptionPane.ERROR_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error Base de Datos",
JOptionPane.ERROR_MESSAGE);
}
}
public void grabar(Habitacion h) {
try {
String sql = "insert into habitacion values('" + h.getNum() + "','" + h.getTip() + "',"+ h.getPre() + ")";
stm.executeUpdate(sql);
stm.close();
con.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error Base de datos",
JOptionPane.ERROR_MESSAGE);
}
}
public void eliminar(String n) {
try {
String sql = "delete from habitacion where num='" + n + "'";
stm.executeUpdate(sql);
stm.close();
con.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error Base de datos",
JOptionPane.ERROR_MESSAGE);
}
}
public void actualizar(Habitacion h) {
try {
String sql ="update habitacion set tipo='"+ h.getTip() + "', precio=" + h.getPre() + " where num='" + h.getNum()
+ "'";
stm.executeUpdate(sql);
stm.close();
con.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error Base de datos",
JOptionPane.ERROR_MESSAGE);
}
}
public void buscar(String n) {
try {
String sql = "select * from habitacion where num='" + n + "'";
res = stm.executeQuery(sql);
while (res.next())
hab = new Habitacion(res.getString(1), res.getString(2), res.getDouble(3));
stm.close();
con.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error Base de datos",
JOptionPane.ERROR_MESSAGE);
}
}
Clase Habitacion
Esta clase permite encapsular los tipos de datos utilizando los mtodos set y get
package client;
public class Habitacion {
String num, tip;
double pre;
public Habitacion(String n, String t, double p) {
this.num = n;
this.tip = t;
this.pre = p;
}
public String getNum() {
return num;
}
public String getTip() {
return tip;
}
public double getPre() {
return pre;
}
}
Clase Hotel
Esta clase permite el acceso a la clase Habitacion y HabitacionBd
package client;
public class Hotel {
public void grabarHabitacion(String n, String t, double p) {
Habitacion h = new Habitacion(n, t, p);
HabitacionBd hb = new HabitacionBd();
hb.grabar(h);
}
public void eliminarHabitacion(String n) {
HabitacionBd hb = new HabitacionBd();
hb.eliminar(n);
}
public void actualizarHabitacion(String n, String t, double p) {
Habitacion h = new Habitacion(n, t, p);
HabitacionBd hb = new HabitacionBd();
hb.actualizar(h);
}
public Habitacion buscarHabitacion(String n) {
HabitacionBd hb = new HabitacionBd();
hb.buscar(n);
Habitacion hab = hb.getHabitacion();
return hab;
}
public void grabarCliente(String dni, String nomcli, String clase, double cons) {
Cliente h = new Cliente(dni, nomcli, clase, cons);
ClienteBd hb = new ClienteBd();
hb.grabar(h);
}
public void eliminarCliente(String dni) {
ClienteBd hb = new ClienteBd();
hb.eliminar(dni);
}
public void actualizarCliente(String dni, String nomcli, String clase, double cons) {
Cliente h = new Cliente(dni, nomcli, clase, cons);
ClienteBd hb = new ClienteBd();
hb.actualizar(h);
}
public Cliente buscarCliente(String dni) {
ClienteBd hb = new ClienteBd();
hb.buscar(dni);
Cliente cli = hb.getCliente();
return cli;
}
}
Clase HabitacionGui
Esta clase muestra la interfase de presentacin
package client;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
});
jLabel1.setText("Numero");
jLabel1.setBounds(new Rectangle(50, 15, 75, 25));
jLabel1.setFont(new Font("Tahoma", 1, 15));
jLabel2.setText("Tipo");
jLabel2.setBounds(new Rectangle(50, 50, 80, 25));
jLabel2.setFont(new Font("Tahoma", 1, 14));
jLabel3.setText("Precio");
jLabel3.setBounds(new Rectangle(50, 90, 80, 25));
jLabel3.setFont(new Font("Tahoma", 1, 14));
btnlistar.setText("Lista");
btnlistar.setBounds(new Rectangle(325, 40, 81, 22));
btnlistar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnlistar_actionPerformed(e);
}
});
jtQuery.setBounds(new Rectangle(20, 190, 375, 140));
jtQuery.setBorder(BorderFactory.createLineBorder(Color.black, 1));
jtQuery.setLayout(borderLayout1);
this.getContentPane().add(jtQuery, null);
this.getContentPane().add(btnlistar, null);
this.getContentPane().add(jLabel3, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jButton5, null);
this.getContentPane().add(jButton4, null);
this.getContentPane().add(jButton3, null);
this.getContentPane().add(t2, null);
this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(jButton2, null);
this.getContentPane().add(Grabar, null);
this.getContentPane().add(t3, null);
this.getContentPane().add(t1, null);
}
Connection con=null;
Statement stm=null;
ResultSet rs=null;
try {
//Para establecer el modelo al JTable
final DefaultTableModel modelo = new DefaultTableModel();
this.jtQuery.setModel(modelo);
//Para conectarnos a nuestra base de datos
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("Jdbc:Odbc:dsnHotel","system","vandres");
stm=con.createStatement();
rs=stm.executeQuery("Select * from habitacion");