Advertisement
Guest User

dao1

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