Guest User

dao

a guest
Aug 23rd, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. -----------------------conexion-------------------------
  2.  
  3. package com.conexion;
  4. import java.sql.*;
  5. /**
  6. *
  7. * @author DELL
  8. */
  9. public class Conexion {
  10. private Connection con;
  11.  
  12. public Connection getCon() {
  13. return con;
  14. }
  15.  
  16. public void setCon(Connection con) {
  17. this.con = con;
  18. }
  19.  
  20. public void conectar() throws Exception
  21. {
  22. try {
  23. Class.forName("com.mysql.jdbc.Driver");
  24. con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bdEvaluacion?user=root&password=");
  25. } catch (Exception e) {
  26. throw e;
  27. }
  28. }
  29.  
  30. public void desconectar() throws Exception
  31. {
  32. try {
  33. if(con!=null)
  34. {
  35. if(con.isClosed()==false)
  36. {
  37. con.close();
  38. }
  39. }
  40. } catch (Exception e) {
  41. throw e;
  42. }
  43. }
  44. }
  45.  
  46.  
  47.  
  48. ----------------------------modelo---------------------------
  49.  
  50. /*
  51. * To change this license header, choose License Headers in Project Properties.
  52. * To change this template file, choose Tools | Templates
  53. * and open the template in the editor.
  54. */
  55. package com.modelo;
  56.  
  57. /**
  58. *
  59. * @author DELL
  60. */
  61. public class Empleado {
  62. private int codigo;
  63. private String nombre;
  64. private int edad;
  65. private String dui;
  66. private String genero;
  67. private double salario;
  68. private String puesto;
  69.  
  70.  
  71.  
  72. public Empleado() {
  73. }
  74.  
  75. public int getCodigo() {
  76. return codigo;
  77. }
  78.  
  79. public void setCodigo(int codigo) {
  80. this.codigo = codigo;
  81. }
  82.  
  83. public String getNombre() {
  84. return nombre;
  85. }
  86.  
  87. public void setNombre(String nombre) {
  88. this.nombre = nombre;
  89. }
  90.  
  91. public int getEdad() {
  92. return edad;
  93. }
  94.  
  95. public void setEdad(int edad) {
  96. this.edad = edad;
  97. }
  98.  
  99. public String getDui() {
  100. return dui;
  101. }
  102.  
  103. public void setDui(String dui) {
  104. this.dui = dui;
  105. }
  106.  
  107. public String getGenero() {
  108. return genero;
  109. }
  110.  
  111. public void setGenero(String genero) {
  112. this.genero = genero;
  113. }
  114.  
  115. public double getSalario() {
  116. return salario;
  117. }
  118.  
  119. public void setSalario(double salario) {
  120. this.salario = salario;
  121. }
  122. public String getPuesto() {
  123. return puesto;
  124. }
  125.  
  126. public void setPuesto(String puesto) {
  127. this.puesto = puesto;
  128. }
  129. }
  130.  
  131. --------------------------daoEmpleado--------------------------------
  132.  
  133. /*
  134. * To change this license header, choose License Headers in Project Properties.
  135. * To change this template file, choose Tools | Templates
  136. * and open the template in the editor.
  137. */
  138. package com.dao;
  139. import java.sql.ResultSet;
  140. import java.util.*;
  141. import com.conexion.Conexion;
  142. import com.modelo.Empleado;
  143. import java.sql.PreparedStatement;
  144. /**
  145. *
  146. * @author DELL
  147. */
  148. public class DaoEmpleado extends Conexion{
  149. public List MostrarEmpleado() throws Exception
  150. {
  151. List listaEmpleado=new ArrayList();
  152. ResultSet res;
  153. try {
  154. this.conectar();
  155. String sql="select * from empleado";
  156. PreparedStatement pre= this.getCon().prepareCall(sql);
  157. res=pre.executeQuery();
  158. while(res.next())
  159. {
  160. Empleado em=new Empleado();
  161. em.setCodigo(res.getInt("idEmpleado"));
  162. em.setNombre(res.getString("nombre"));
  163. em.setEdad(res.getInt("edad"));
  164. em.setDui(res.getString("dui"));
  165. em.setGenero(res.getString("genero"));
  166. em.setSalario(res.getDouble("salario"));
  167. em.setPuesto(res.getString("puesto"));
  168. listaEmpleado.add(em);
  169. }
  170.  
  171. } catch (Exception e) {
  172. throw e;
  173. }
  174. return listaEmpleado;
  175. }
  176.  
  177. public void insertarEmpleado(Empleado em) throws Exception
  178. {
  179. try {
  180. this.conectar();
  181. String sql="insert into empleado (nombre,edad,dui,genero,salario,puesto) values (?,?,?,?,?,?)";
  182. PreparedStatement pre= this.getCon().prepareStatement(sql);
  183. pre.setString(1, em.getNombre());
  184. pre.setInt(2, em.getEdad());
  185. pre.setString(3, em.getDui());
  186. pre.setString(4, em.getGenero());
  187. pre.setDouble(5, em.getSalario());
  188. pre.setString(6, em.getPuesto());
  189. pre.executeUpdate();
  190. } catch (Exception e) {
  191. throw e;
  192. }
  193. }
  194.  
  195. public void modificarEmpleado (Empleado em) throws Exception
  196. {
  197. try {
  198. this.conectar();
  199. String sql="update empleado set nombre=?,edad=?,dui=?,genero=?,salario=?,puesto=? where idEmpleado=?";
  200. PreparedStatement pre= this.getCon().prepareStatement(sql);
  201. pre.setString(1, em.getNombre());
  202. pre.setInt(2, em.getEdad());
  203. pre.setString(3, em.getDui());
  204. pre.setString(4, em.getGenero());
  205. pre.setDouble(5, em.getSalario());
  206. pre.setString(6, em.getPuesto());
  207. pre.setInt(7, em.getCodigo());
  208. pre.executeUpdate();
  209. } catch (Exception e) {
  210. throw e;
  211. }
  212. }
  213.  
  214. public void eliminarEmpleado(Empleado em) throws Exception
  215. {
  216. try {
  217. this.conectar();
  218. String sql="delete from empleado where idEmpleado=?";
  219. PreparedStatement pre= this.getCon().prepareStatement(sql);
  220. pre.setInt(1, em.getCodigo());
  221. pre.executeUpdate();
  222. } catch (Exception e) {
  223. throw e;
  224. }
  225. }
  226. }
  227.  
  228.  
  229. -------------------------vista-----------------------------
  230.  
  231. package com.vista;
  232. import com.dao.DaoEmpleado;
  233. import com.modelo.Empleado;
  234. import javax.swing.table.DefaultTableModel;
  235. import java.util.*;
  236. import javax.swing.JOptionPane;
  237.  
  238. /**
  239. *
  240. * @author DELL
  241. */
  242. public class FrmEmpleado extends javax.swing.JFrame {
  243.  
  244. /**
  245. * Creates new form FrmEMpleado
  246. */
  247. public FrmEmpleado() {
  248. initComponents();
  249. tablae();
  250. }
  251. DaoEmpleado daoe=new DaoEmpleado();
  252. Empleado em=new Empleado();
  253.  
  254. public void tablae(){
  255. String [] columnas= {"codigo","nombre","edad","dui","genero","salario","puesto"};
  256. Object [] obj= new Object[7];
  257. DefaultTableModel tabla=new DefaultTableModel(null, columnas);
  258. List ls;
  259. try {
  260. ls=daoe.MostrarEmpleado();
  261. for(int i=0;i<ls.size();i++)
  262. {
  263. em=(Empleado)ls.get(i);
  264. obj[0]=em.getCodigo();
  265. obj[1]=em.getNombre();
  266. obj[2]=em.getEdad();
  267. obj[3]=em.getDui();
  268. obj[4]=em.getGenero();
  269. obj[5]=em.getSalario();
  270. obj[6]=em.getPuesto();
  271. tabla.addRow(obj);
  272. }
  273.  
  274. this.jtbEmpleado.setModel(tabla);
  275. ls=daoe.MostrarEmpleado();
  276.  
  277. } catch (Exception e) {
  278. e.toString();
  279. }
  280. }
  281. public void llenartabla(){
  282. int fila=this.jtbEmpleado.getSelectedRow();
  283. this.jTxtCodigo.setText(String.valueOf(this.jtbEmpleado.getValueAt(fila, 0)));
  284. this.jTxtnombre.setText(String.valueOf(this.jtbEmpleado.getValueAt(fila, 1)));
  285. this.jSEdad.setValue(this.jtbEmpleado.getValueAt(fila, 2));
  286. this.jTxtDui.setText(String.valueOf(this.jtbEmpleado.getValueAt(fila, 3)));
  287. this.jTxtSueldo.setText(String.valueOf(this.jtbEmpleado.getValueAt(fila, 5)));
  288. this.jCPuesto.setSelectedItem(String.valueOf(this.jtbEmpleado.getValueAt(fila, 6)));
  289.  
  290. }
  291.  
  292. public void ingresar()
  293. {
  294. try {
  295. em.setCodigo(ICONIFIED);
  296. em.setNombre(this.jTxtnombre.getText());
  297. em.setEdad(Integer.parseInt(this.jSEdad.getValue().toString()));
  298. em.setDui(this.jTxtDui.getText());
  299.  
  300. String genero;
  301. if(jRF.isSelected())
  302. {
  303. genero="Femenino";
  304.  
  305. }
  306. else
  307. genero="Masculino";
  308. em.setGenero(genero);
  309.  
  310. em.setSalario(Double.parseDouble(this.jTxtSueldo.getText()));
  311. em.setPuesto(this.jCPuesto.getSelectedItem().toString());
  312. daoe.insertarEmpleado(em);
  313. JOptionPane.showMessageDialog(rootPane, "Ingresado correctamente");
  314. daoe.MostrarEmpleado();
  315. tablae();
  316.  
  317. } catch (Exception e) {
  318. e.printStackTrace();
  319. }
  320.  
  321. }
  322.  
  323. public void modificar(){
  324. try {
  325. em.setCodigo(Integer.parseInt(this.jTxtCodigo.getText()));
  326. em.setNombre((this.jTxtnombre.getText()));
  327. em.setEdad(Integer.parseInt(this.jSEdad.getValue().toString()));
  328. em.setDui(this.jTxtDui.getText());
  329. em.setSalario(Double.parseDouble(this.jTxtSueldo.getText()));
  330. em.setPuesto(this.jCPuesto.getSelectedItem().toString());
  331. int SioNo=JOptionPane.showConfirmDialog(rootPane, "Desea Modificar el empleado","modificar",JOptionPane.YES_NO_OPTION);
  332. if (SioNo==0)
  333. {
  334. daoe.modificarEmpleado(em);
  335. JOptionPane.showMessageDialog(rootPane, "Modificado correctamente");
  336. }
  337. daoe.MostrarEmpleado();
  338. tablae();
  339. } catch (Exception e) {
  340. }
  341. }
  342.  
  343. public void eliminar(){
  344. try {
  345. em.setCodigo(Integer.parseInt(this.jTxtCodigo.getText()));
  346. int SioNo=JOptionPane.showConfirmDialog(rootPane, "Desea Eliminar el empleado","Eliminar",JOptionPane.YES_NO_OPTION);
  347. if(SioNo==0){
  348. daoe.eliminarEmpleado(em);
  349. JOptionPane.showMessageDialog(rootPane, "eliminado correctamente",":v",JOptionPane.INFORMATION_MESSAGE);
  350. }
  351.  
  352. tablae();
  353. } catch (Exception e) {
  354. }
  355. }
Add Comment
Please, Sign In to add comment