Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- conexion
- package conexion;
- /**
- *
- * @author DELL
- */
- public class Conexion {
- private String driver;
- private String url;
- private String user;
- private String clave;
- public Conexion(){
- this.driver="com.mysql.jdbc.Driver";
- this.url="jdbc:mysql://localhost:3306/bdplanilla";
- this.user="root";
- this.clave="";
- }
- 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 getClave() {
- return clave;
- }
- public void setClave(String clave) {
- this.clave = clave;
- }
- }
- operacionesEMpleado
- package controlador;
- import java.util.List;
- /**
- *
- * @author DELL
- */
- public interface OperacionesEmpleado {
- public String agregarEmpleado(Object obj);
- public String modificarEmpleado(Object obj);
- public String eliminarEmpleado(Object obj);
- public List mostrarEmpleado();
- }
- Control empleado
- package controlador;
- import conexion.Conexion;
- import java.util.*;
- import java.sql.*;
- import modelo.Empleado;
- /**
- *
- * @author DELL
- */
- public class ControlEmpleado implements OperacionesEmpleado {
- public ControlEmpleado() {
- }
- @Override
- public String agregarEmpleado(Object obj) {
- Conexion con=new Conexion();
- Connection cn;
- ResultSet res;
- Statement st;
- String sql;
- String msj=null;
- Empleado em=(Empleado)obj;
- try
- {
- Class.forName(con.getDriver());
- cn=DriverManager.getConnection(con.getUrl(),con.getUser(),con.getClave());
- st=cn.createStatement();
- sql="insert into empleado values("+em.getCodEmpleado()+",'"+em.getNombre()+"',"+em.getEdad()+","+em.getSueldo()+")";
- st.executeUpdate(sql);
- st.close();
- cn.close();
- msj="Datos insertados Correctamente";
- } catch (Exception e) {
- msj=e.toString();
- }
- return msj;
- }
- @Override
- public String modificarEmpleado(Object obj) {
- Conexion con=new Conexion();
- Connection cn;
- ResultSet res;
- Statement st;
- String sql;
- String msj=null;
- Empleado em=(Empleado)obj;
- try
- {
- Class.forName(con.getDriver());
- cn=DriverManager.getConnection(con.getUrl(),con.getUser(),con.getClave());
- st=cn.createStatement();
- sql="update empleado set nombre='"+em.getNombre()+"', edad="+em.getEdad()+","+"sueldo="+em.getSueldo()+" where codEmpleado="+em.getCodEmpleado();
- st.executeUpdate(sql);
- st.close();
- cn.close();
- msj="Datos modificados Correctamente";
- } catch (Exception e)
- {
- msj=e.toString();
- }
- return msj;
- }
- @Override
- public String eliminarEmpleado(Object obj) {
- Conexion con=new Conexion();
- Connection cn;
- ResultSet res;
- Statement st;
- String sql;
- String msj=null;
- Empleado em=(Empleado)obj;
- try
- {
- Class.forName(con.getDriver());
- cn=DriverManager.getConnection(con.getUrl(),con.getUser(),con.getClave());
- st=cn.createStatement();
- sql="delete from empleado where codEmpleado="+em.getCodEmpleado();
- st.executeUpdate(sql);
- st.close();
- cn.close();
- msj="Datos eliminados Correctamente";
- } catch (Exception e) {
- msj=e.toString();
- }
- return msj;
- }
- @Override
- public List mostrarEmpleado() {
- Conexion con=new Conexion();
- Connection cn;
- PreparedStatement pre;
- ResultSet res;
- Statement st;
- String sql;
- List listaempleados= new ArrayList();
- try
- {
- Class.forName(con.getDriver());
- cn=DriverManager.getConnection(con.getUrl(),con.getUser(),con.getClave());
- st=cn.createStatement();
- sql="select codEmpleado,nombre,edad,sueldo,(sueldo*0.03) as 'ISSS',\n"+
- "(sueldo*0.625) as 'AFP',(sueldo*0.10) as 'Renta',(sueldo*(1-0.03-0.625-0.1))"
- + "as 'sueldoneto' \n"+
- "from empleado;";
- res=st.executeQuery(sql);
- while(res.next()){
- listaempleados.add(new Empleado (
- res.getInt("codEmpleado"),
- res.getString("nombre"),
- res.getInt("edad"),
- res.getDouble("sueldo")
- ));
- }
- res.close();
- cn.close();
- st.close();
- }catch (Exception e)
- {
- e.printStackTrace();
- }
- return listaempleados;
- }
- }
- modelo
- package modelo;
- /**
- *
- * @author DELL
- */
- public class Empleado {
- private int codEmpleado;
- private String nombre;
- private int edad;
- private double sueldo;
- public Empleado() {
- }
- public Empleado(int codEmpleado, String nombre, int edad, double sueldo) {
- this.codEmpleado = codEmpleado;
- this.nombre = nombre;
- this.edad = edad;
- this.sueldo = sueldo;
- }
- public int getCodEmpleado() {
- return codEmpleado;
- }
- public void setCodEmpleado(int codEmpleado) {
- this.codEmpleado = codEmpleado;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- public int getEdad() {
- return edad;
- }
- public void setEdad(int edad) {
- this.edad = edad;
- }
- public double getSueldo() {
- return sueldo;
- }
- public void setSueldo(double sueldo) {
- this.sueldo = sueldo;
- }
- }
- vista
- import modelo.Empleado;
- import controlador.ControlEmpleado;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.JOptionPane;
- import java.util.*;
- import java.text.*;
- Insertar
- public void insertar()
- {
- Empleado em=new Empleado();
- ControlEmpleado ce=new ControlEmpleado();
- try {
- em.setCodEmpleado(Integer.parseInt(this.jTxtCodigo.getText()));
- em.setNombre(this.jTxtNombre.getText());
- em.setEdad(Integer.parseInt(this.jTxtEdad.getText()));
- em.setSueldo(Double.parseDouble(this.jTxtSueldo.getText()));
- String msj=ce.agregarEmpleado(em);
- JOptionPane.showMessageDialog(rootPane, msj,"CONFIRMACION",
- JOptionPane.INFORMATION_MESSAGE);
- tablaE();
- limpiar();
- } catch (Exception e){
- JOptionPane.showMessageDialog(rootPane, e.toString(),"Error",
- JOptionPane.ERROR_MESSAGE);
- }
- }
- modificar
- public void modificar(){
- Empleado em=new Empleado();
- ControlEmpleado ce= new ControlEmpleado();
- try {
- em.setCodEmpleado(Integer.parseInt(this.jTxtCodigo.getText()));
- em.setNombre(this.jTxtNombre.getText());
- em.setEdad(Integer.parseInt(this.jTxtEdad.getText()));
- em.setSueldo(Double.parseDouble(this.jTxtSueldo.getText()));
- int SioNo=JOptionPane.showConfirmDialog(this, "DESEA MODIFICAR EMPLEADO",
- "MODIFICAR EMPLEADO",JOptionPane.YES_NO_OPTION);
- if (SioNo==0)
- {
- String msj=ce.modificarEmpleado(em);
- JOptionPane.showMessageDialog(rootPane, msj);
- tablaE();
- limpiar();
- }
- else
- {
- limpiar();
- }
- } catch (Exception e) {
- JOptionPane.showConfirmDialog(rootPane, e.toString(),"Error",JOptionPane.ERROR_MESSAGE);
- }
- }
- eliminar
- public void eliminar(){
- Empleado em=new Empleado();
- ControlEmpleado ce=new ControlEmpleado();
- try {
- em.setCodEmpleado(Integer.parseInt(this.jTxtCodigo.getText()));
- int SioNo=JOptionPane.showConfirmDialog(this, "Desea Eliminar el empleado",
- "Eliminar empleado",JOptionPane.YES_NO_OPTION);
- if (SioNo==0) {
- String msj=ce.eliminarEmpleado(em);
- JOptionPane.showMessageDialog(rootPane, msj,"Confirmacion",
- JOptionPane.INFORMATION_MESSAGE);
- tablaE();
- limpiar();
- }
- else
- {
- limpiar();
- }
- } catch (Exception e) {
- JOptionPane.showConfirmDialog(rootPane, e.toString(),"Error",
- JOptionPane.ERROR_MESSAGE);
- }
- }
- tablaE
- public void tablaE(){
- String[] columnas={"Codigo empleado","Nombre","Edad","Sueldo"};
- Object[] obj=new Object[4];
- DefaultTableModel tabla=new DefaultTableModel(null,columnas);
- ControlEmpleado ce= new ControlEmpleado();
- Empleado em=new Empleado();
- List ls;
- DecimalFormat df=new DecimalFormat("#,###.00");
- try {
- ls=ce.mostrarEmpleado();
- for(int i=0;i<ls.size();i++)
- {
- em=(Empleado)ls.get(i);
- obj[0]=em.getCodEmpleado();
- obj[1]=em.getNombre();
- obj[2]=em.getEdad();
- obj[3]=df.format(em.getSueldo());
- tabla.addRow(obj);
- }
- ls=ce.mostrarEmpleado();
- this.tbEmpleado.setModel(tabla);
- } catch (Exception e) {
- JOptionPane.showConfirmDialog(this, "Error al mostrar Datos "+e.toString());
- }
- }
- limpiar
- public void limpiar(){
- this.jTxtCodigo.setText("");
- this.jTxtNombre.setText("");
- this.jTxtEdad.setText("");
- this.jTxtSueldo.setText("");
- }
- llenado tabla
- private void tbEmpleadoMouseClicked(java.awt.event.MouseEvent evt) {
- int fila =this.tbEmpleado.getSelectedRow();
- this.jTxtCodigo.setText(String.valueOf(this.tbEmpleado.getValueAt(fila, 0)));
- this.jTxtNombre.setText(String.valueOf(this.tbEmpleado.getValueAt(fila, 1)));
- this.jTxtEdad.setText(String.valueOf(this.tbEmpleado.getValueAt(fila, 2)));
- this.jTxtSueldo.setText(String.valueOf(this.tbEmpleado.getValueAt(fila, 3)));
- }
- primer constructor
- public frmEmpleado() {
- initComponents();
- tablaE();
- this.setLocationRelativeTo(null);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement