Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------conexion-------------------------
- package com.conexion;
- import java.sql.*;
- /**
- *
- * @author DELL
- */
- public class Conexion {
- private Connection con;
- public Connection getCon() {
- return con;
- }
- public void setCon(Connection con) {
- this.con = con;
- }
- public void conectar() throws Exception
- {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bdEvaluacion?user=root&password=");
- } catch (Exception e) {
- throw e;
- }
- }
- public void desconectar() throws Exception
- {
- try {
- if(con!=null)
- {
- if(con.isClosed()==false)
- {
- con.close();
- }
- }
- } catch (Exception e) {
- throw e;
- }
- }
- }
- ----------------------------modelo---------------------------
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.modelo;
- /**
- *
- * @author DELL
- */
- public class Empleado {
- private int codigo;
- private String nombre;
- private int edad;
- private String dui;
- private String genero;
- private double salario;
- private String puesto;
- public Empleado() {
- }
- public int getCodigo() {
- return codigo;
- }
- public void setCodigo(int codigo) {
- this.codigo = codigo;
- }
- 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 String getDui() {
- return dui;
- }
- public void setDui(String dui) {
- this.dui = dui;
- }
- public String getGenero() {
- return genero;
- }
- public void setGenero(String genero) {
- this.genero = genero;
- }
- public double getSalario() {
- return salario;
- }
- public void setSalario(double salario) {
- this.salario = salario;
- }
- public String getPuesto() {
- return puesto;
- }
- public void setPuesto(String puesto) {
- this.puesto = puesto;
- }
- }
- --------------------------daoEmpleado--------------------------------
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.dao;
- import java.sql.ResultSet;
- import java.util.*;
- import com.conexion.Conexion;
- import com.modelo.Empleado;
- import java.sql.PreparedStatement;
- /**
- *
- * @author DELL
- */
- public class DaoEmpleado extends Conexion{
- public List MostrarEmpleado() throws Exception
- {
- List listaEmpleado=new ArrayList();
- ResultSet res;
- try {
- this.conectar();
- String sql="select * from empleado";
- PreparedStatement pre= this.getCon().prepareCall(sql);
- res=pre.executeQuery();
- while(res.next())
- {
- Empleado em=new Empleado();
- em.setCodigo(res.getInt("idEmpleado"));
- em.setNombre(res.getString("nombre"));
- em.setEdad(res.getInt("edad"));
- em.setDui(res.getString("dui"));
- em.setGenero(res.getString("genero"));
- em.setSalario(res.getDouble("salario"));
- em.setPuesto(res.getString("puesto"));
- listaEmpleado.add(em);
- }
- } catch (Exception e) {
- throw e;
- }
- return listaEmpleado;
- }
- public void insertarEmpleado(Empleado em) throws Exception
- {
- try {
- this.conectar();
- String sql="insert into empleado (nombre,edad,dui,genero,salario,puesto) values (?,?,?,?,?,?)";
- PreparedStatement pre= this.getCon().prepareStatement(sql);
- pre.setString(1, em.getNombre());
- pre.setInt(2, em.getEdad());
- pre.setString(3, em.getDui());
- pre.setString(4, em.getGenero());
- pre.setDouble(5, em.getSalario());
- pre.setString(6, em.getPuesto());
- pre.executeUpdate();
- } catch (Exception e) {
- throw e;
- }
- }
- public void modificarEmpleado (Empleado em) throws Exception
- {
- try {
- this.conectar();
- String sql="update empleado set nombre=?,edad=?,dui=?,genero=?,salario=?,puesto=? where idEmpleado=?";
- PreparedStatement pre= this.getCon().prepareStatement(sql);
- pre.setString(1, em.getNombre());
- pre.setInt(2, em.getEdad());
- pre.setString(3, em.getDui());
- pre.setString(4, em.getGenero());
- pre.setDouble(5, em.getSalario());
- pre.setString(6, em.getPuesto());
- pre.setInt(7, em.getCodigo());
- pre.executeUpdate();
- } catch (Exception e) {
- throw e;
- }
- }
- public void eliminarEmpleado(Empleado em) throws Exception
- {
- try {
- this.conectar();
- String sql="delete from empleado where idEmpleado=?";
- PreparedStatement pre= this.getCon().prepareStatement(sql);
- pre.setInt(1, em.getCodigo());
- pre.executeUpdate();
- } catch (Exception e) {
- throw e;
- }
- }
- }
- -------------------------vista-----------------------------
- package com.vista;
- import com.dao.DaoEmpleado;
- import com.modelo.Empleado;
- import javax.swing.table.DefaultTableModel;
- import java.util.*;
- import javax.swing.JOptionPane;
- /**
- *
- * @author DELL
- */
- public class FrmEmpleado extends javax.swing.JFrame {
- /**
- * Creates new form FrmEMpleado
- */
- public FrmEmpleado() {
- initComponents();
- tablae();
- }
- DaoEmpleado daoe=new DaoEmpleado();
- Empleado em=new Empleado();
- public void tablae(){
- String [] columnas= {"codigo","nombre","edad","dui","genero","salario","puesto"};
- Object [] obj= new Object[7];
- DefaultTableModel tabla=new DefaultTableModel(null, columnas);
- List ls;
- try {
- ls=daoe.MostrarEmpleado();
- for(int i=0;i<ls.size();i++)
- {
- em=(Empleado)ls.get(i);
- obj[0]=em.getCodigo();
- obj[1]=em.getNombre();
- obj[2]=em.getEdad();
- obj[3]=em.getDui();
- obj[4]=em.getGenero();
- obj[5]=em.getSalario();
- obj[6]=em.getPuesto();
- tabla.addRow(obj);
- }
- this.jtbEmpleado.setModel(tabla);
- ls=daoe.MostrarEmpleado();
- } catch (Exception e) {
- e.toString();
- }
- }
- public void llenartabla(){
- int fila=this.jtbEmpleado.getSelectedRow();
- this.jTxtCodigo.setText(String.valueOf(this.jtbEmpleado.getValueAt(fila, 0)));
- this.jTxtnombre.setText(String.valueOf(this.jtbEmpleado.getValueAt(fila, 1)));
- this.jSEdad.setValue(this.jtbEmpleado.getValueAt(fila, 2));
- this.jTxtDui.setText(String.valueOf(this.jtbEmpleado.getValueAt(fila, 3)));
- this.jTxtSueldo.setText(String.valueOf(this.jtbEmpleado.getValueAt(fila, 5)));
- this.jCPuesto.setSelectedItem(String.valueOf(this.jtbEmpleado.getValueAt(fila, 6)));
- }
- public void ingresar()
- {
- try {
- em.setCodigo(ICONIFIED);
- em.setNombre(this.jTxtnombre.getText());
- em.setEdad(Integer.parseInt(this.jSEdad.getValue().toString()));
- em.setDui(this.jTxtDui.getText());
- String genero;
- if(jRF.isSelected())
- {
- genero="Femenino";
- }
- else
- genero="Masculino";
- em.setGenero(genero);
- em.setSalario(Double.parseDouble(this.jTxtSueldo.getText()));
- em.setPuesto(this.jCPuesto.getSelectedItem().toString());
- daoe.insertarEmpleado(em);
- JOptionPane.showMessageDialog(rootPane, "Ingresado correctamente");
- daoe.MostrarEmpleado();
- tablae();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void modificar(){
- try {
- em.setCodigo(Integer.parseInt(this.jTxtCodigo.getText()));
- em.setNombre((this.jTxtnombre.getText()));
- em.setEdad(Integer.parseInt(this.jSEdad.getValue().toString()));
- em.setDui(this.jTxtDui.getText());
- em.setSalario(Double.parseDouble(this.jTxtSueldo.getText()));
- em.setPuesto(this.jCPuesto.getSelectedItem().toString());
- int SioNo=JOptionPane.showConfirmDialog(rootPane, "Desea Modificar el empleado","modificar",JOptionPane.YES_NO_OPTION);
- if (SioNo==0)
- {
- daoe.modificarEmpleado(em);
- JOptionPane.showMessageDialog(rootPane, "Modificado correctamente");
- }
- daoe.MostrarEmpleado();
- tablae();
- } catch (Exception e) {
- }
- }
- public void eliminar(){
- try {
- em.setCodigo(Integer.parseInt(this.jTxtCodigo.getText()));
- int SioNo=JOptionPane.showConfirmDialog(rootPane, "Desea Eliminar el empleado","Eliminar",JOptionPane.YES_NO_OPTION);
- if(SioNo==0){
- daoe.eliminarEmpleado(em);
- JOptionPane.showMessageDialog(rootPane, "eliminado correctamente",":v",JOptionPane.INFORMATION_MESSAGE);
- }
- tablae();
- } catch (Exception e) {
- }
- }
Add Comment
Please, Sign In to add comment