Advertisement
Guest User

stis

a guest
May 26th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.21 KB | None | 0 0
  1. conexion
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. package conexion;
  8.  
  9. /**
  10. *
  11. * @author DELL
  12. */
  13. public class Conexion {
  14. private String driver;
  15. private String url;
  16. private String user;
  17. private String clave;
  18.  
  19. public Conexion(){
  20. this.driver="com.mysql.jdbc.Driver";
  21. this.url="jdbc:mysql://localhost:3306/bdplanilla";
  22. this.user="root";
  23. this.clave="";
  24. }
  25.  
  26. public String getDriver() {
  27. return driver;
  28. }
  29.  
  30. public void setDriver(String driver) {
  31. this.driver = driver;
  32. }
  33.  
  34. public String getUrl() {
  35. return url;
  36. }
  37.  
  38. public void setUrl(String url) {
  39. this.url = url;
  40. }
  41.  
  42. public String getUser() {
  43. return user;
  44. }
  45.  
  46. public void setUser(String user) {
  47. this.user = user;
  48. }
  49.  
  50. public String getClave() {
  51. return clave;
  52. }
  53.  
  54. public void setClave(String clave) {
  55. this.clave = clave;
  56. }
  57.  
  58.  
  59. }
  60.  
  61.  
  62.  
  63.  
  64. operacionesEMpleado
  65.  
  66.  
  67. package controlador;
  68. import java.util.List;
  69.  
  70. /**
  71. *
  72. * @author DELL
  73. */
  74. public interface OperacionesEmpleado {
  75.  
  76.  
  77.  
  78. public String agregarEmpleado(Object obj);
  79. public String modificarEmpleado(Object obj);
  80. public String eliminarEmpleado(Object obj);
  81. public List mostrarEmpleado();
  82. }
  83.  
  84.  
  85. Control empleado
  86.  
  87. package controlador;
  88.  
  89. import conexion.Conexion;
  90. import java.util.*;
  91. import java.sql.*;
  92. import modelo.Empleado;
  93.  
  94.  
  95.  
  96. /**
  97. *
  98. * @author DELL
  99. */
  100. public class ControlEmpleado implements OperacionesEmpleado {
  101.  
  102. public ControlEmpleado() {
  103. }
  104.  
  105. @Override
  106. public String agregarEmpleado(Object obj) {
  107. Conexion con=new Conexion();
  108. Connection cn;
  109. ResultSet res;
  110. Statement st;
  111. String sql;
  112. String msj=null;
  113. Empleado em=(Empleado)obj;
  114. try
  115. {
  116. Class.forName(con.getDriver());
  117. cn=DriverManager.getConnection(con.getUrl(),con.getUser(),con.getClave());
  118. st=cn.createStatement();
  119. sql="insert into empleado values("+em.getCodEmpleado()+",'"+em.getNombre()+"',"+em.getEdad()+","+em.getSueldo()+")";
  120. st.executeUpdate(sql);
  121. st.close();
  122. cn.close();
  123. msj="Datos insertados Correctamente";
  124.  
  125. } catch (Exception e) {
  126. msj=e.toString();
  127. }
  128.  
  129. return msj;
  130.  
  131. }
  132.  
  133.  
  134. @Override
  135. public String modificarEmpleado(Object obj) {
  136. Conexion con=new Conexion();
  137. Connection cn;
  138. ResultSet res;
  139. Statement st;
  140. String sql;
  141. String msj=null;
  142. Empleado em=(Empleado)obj;
  143. try
  144. {
  145. Class.forName(con.getDriver());
  146. cn=DriverManager.getConnection(con.getUrl(),con.getUser(),con.getClave());
  147. st=cn.createStatement();
  148. sql="update empleado set nombre='"+em.getNombre()+"', edad="+em.getEdad()+","+"sueldo="+em.getSueldo()+" where codEmpleado="+em.getCodEmpleado();
  149. st.executeUpdate(sql);
  150. st.close();
  151. cn.close();
  152. msj="Datos modificados Correctamente";
  153.  
  154. } catch (Exception e)
  155. {
  156. msj=e.toString();
  157. }
  158.  
  159. return msj;
  160.  
  161. }
  162.  
  163. @Override
  164. public String eliminarEmpleado(Object obj) {
  165. Conexion con=new Conexion();
  166. Connection cn;
  167. ResultSet res;
  168. Statement st;
  169. String sql;
  170. String msj=null;
  171. Empleado em=(Empleado)obj;
  172. try
  173. {
  174. Class.forName(con.getDriver());
  175. cn=DriverManager.getConnection(con.getUrl(),con.getUser(),con.getClave());
  176. st=cn.createStatement();
  177. sql="delete from empleado where codEmpleado="+em.getCodEmpleado();
  178. st.executeUpdate(sql);
  179. st.close();
  180. cn.close();
  181. msj="Datos eliminados Correctamente";
  182.  
  183. } catch (Exception e) {
  184. msj=e.toString();
  185. }
  186.  
  187. return msj;
  188.  
  189. }
  190.  
  191. @Override
  192. public List mostrarEmpleado() {
  193. Conexion con=new Conexion();
  194. Connection cn;
  195. PreparedStatement pre;
  196. ResultSet res;
  197. Statement st;
  198. String sql;
  199. List listaempleados= new ArrayList();
  200.  
  201. try
  202. {
  203. Class.forName(con.getDriver());
  204. cn=DriverManager.getConnection(con.getUrl(),con.getUser(),con.getClave());
  205. st=cn.createStatement();
  206. sql="select codEmpleado,nombre,edad,sueldo,(sueldo*0.03) as 'ISSS',\n"+
  207. "(sueldo*0.625) as 'AFP',(sueldo*0.10) as 'Renta',(sueldo*(1-0.03-0.625-0.1))"
  208. + "as 'sueldoneto' \n"+
  209. "from empleado;";
  210. res=st.executeQuery(sql);
  211. while(res.next()){
  212. listaempleados.add(new Empleado (
  213. res.getInt("codEmpleado"),
  214. res.getString("nombre"),
  215. res.getInt("edad"),
  216. res.getDouble("sueldo")
  217. ));
  218.  
  219. }
  220. res.close();
  221. cn.close();
  222. st.close();
  223. }catch (Exception e)
  224. {
  225.  
  226. e.printStackTrace();
  227.  
  228. }
  229. return listaempleados;
  230. }
  231.  
  232.  
  233. }
  234.  
  235.  
  236. modelo
  237.  
  238. /*
  239. * To change this license header, choose License Headers in Project Properties.
  240. * To change this template file, choose Tools | Templates
  241. * and open the template in the editor.
  242. */
  243. package modelo;
  244.  
  245. /**
  246. *
  247. * @author DELL
  248. */
  249. public class Empleado {
  250. private int codEmpleado;
  251. private String nombre;
  252. private int edad;
  253. private double sueldo;
  254.  
  255. public Empleado() {
  256. }
  257.  
  258. public Empleado(int codEmpleado, String nombre, int edad, double sueldo) {
  259. this.codEmpleado = codEmpleado;
  260. this.nombre = nombre;
  261. this.edad = edad;
  262. this.sueldo = sueldo;
  263. }
  264.  
  265. public int getCodEmpleado() {
  266. return codEmpleado;
  267. }
  268.  
  269. public void setCodEmpleado(int codEmpleado) {
  270. this.codEmpleado = codEmpleado;
  271. }
  272.  
  273. public String getNombre() {
  274. return nombre;
  275. }
  276.  
  277. public void setNombre(String nombre) {
  278. this.nombre = nombre;
  279. }
  280.  
  281. public int getEdad() {
  282. return edad;
  283. }
  284.  
  285. public void setEdad(int edad) {
  286. this.edad = edad;
  287. }
  288.  
  289. public double getSueldo() {
  290. return sueldo;
  291. }
  292.  
  293. public void setSueldo(double sueldo) {
  294. this.sueldo = sueldo;
  295. }
  296.  
  297.  
  298. }
  299.  
  300.  
  301. vista
  302. import modelo.Empleado;
  303. import controlador.ControlEmpleado;
  304. import javax.swing.table.DefaultTableModel;
  305. import javax.swing.JOptionPane;
  306. import java.util.*;
  307. import java.text.*;
  308.  
  309.  
  310. Insertar
  311. public void insertar()
  312. {
  313. Empleado em=new Empleado();
  314. ControlEmpleado ce=new ControlEmpleado();
  315. try {
  316. em.setCodEmpleado(Integer.parseInt(this.jTxtCodigo.getText()));
  317. em.setNombre(this.jTxtNombre.getText());
  318. em.setEdad(Integer.parseInt(this.jTxtEdad.getText()));
  319. em.setSueldo(Double.parseDouble(this.jTxtSueldo.getText()));
  320. String msj=ce.agregarEmpleado(em);
  321. JOptionPane.showMessageDialog(rootPane, msj,"CONFIRMACION",
  322. JOptionPane.INFORMATION_MESSAGE);
  323. tablaE();
  324. limpiar();
  325. } catch (Exception e){
  326. JOptionPane.showMessageDialog(rootPane, e.toString(),"Error",
  327. JOptionPane.ERROR_MESSAGE);
  328. }
  329. }
  330.  
  331.  
  332. modificar
  333. public void modificar(){
  334. Empleado em=new Empleado();
  335. ControlEmpleado ce= new ControlEmpleado();
  336. try {
  337. em.setCodEmpleado(Integer.parseInt(this.jTxtCodigo.getText()));
  338. em.setNombre(this.jTxtNombre.getText());
  339. em.setEdad(Integer.parseInt(this.jTxtEdad.getText()));
  340. em.setSueldo(Double.parseDouble(this.jTxtSueldo.getText()));
  341. int SioNo=JOptionPane.showConfirmDialog(this, "DESEA MODIFICAR EMPLEADO",
  342. "MODIFICAR EMPLEADO",JOptionPane.YES_NO_OPTION);
  343. if (SioNo==0)
  344. {
  345. String msj=ce.modificarEmpleado(em);
  346. JOptionPane.showMessageDialog(rootPane, msj);
  347. tablaE();
  348. limpiar();
  349. }
  350. else
  351. {
  352. limpiar();
  353. }
  354. } catch (Exception e) {
  355. JOptionPane.showConfirmDialog(rootPane, e.toString(),"Error",JOptionPane.ERROR_MESSAGE);
  356. }
  357. }
  358.  
  359. eliminar
  360. public void eliminar(){
  361. Empleado em=new Empleado();
  362. ControlEmpleado ce=new ControlEmpleado();
  363. try {
  364. em.setCodEmpleado(Integer.parseInt(this.jTxtCodigo.getText()));
  365. int SioNo=JOptionPane.showConfirmDialog(this, "Desea Eliminar el empleado",
  366. "Eliminar empleado",JOptionPane.YES_NO_OPTION);
  367. if (SioNo==0) {
  368. String msj=ce.eliminarEmpleado(em);
  369. JOptionPane.showMessageDialog(rootPane, msj,"Confirmacion",
  370. JOptionPane.INFORMATION_MESSAGE);
  371. tablaE();
  372. limpiar();
  373.  
  374. }
  375. else
  376. {
  377. limpiar();
  378. }
  379. } catch (Exception e) {
  380. JOptionPane.showConfirmDialog(rootPane, e.toString(),"Error",
  381. JOptionPane.ERROR_MESSAGE);
  382. }
  383. }
  384.  
  385. tablaE
  386. public void tablaE(){
  387. String[] columnas={"Codigo empleado","Nombre","Edad","Sueldo"};
  388. Object[] obj=new Object[4];
  389. DefaultTableModel tabla=new DefaultTableModel(null,columnas);
  390. ControlEmpleado ce= new ControlEmpleado();
  391. Empleado em=new Empleado();
  392. List ls;
  393. DecimalFormat df=new DecimalFormat("#,###.00");
  394. try {
  395. ls=ce.mostrarEmpleado();
  396. for(int i=0;i<ls.size();i++)
  397. {
  398. em=(Empleado)ls.get(i);
  399. obj[0]=em.getCodEmpleado();
  400. obj[1]=em.getNombre();
  401. obj[2]=em.getEdad();
  402. obj[3]=df.format(em.getSueldo());
  403. tabla.addRow(obj);
  404. }
  405. ls=ce.mostrarEmpleado();
  406. this.tbEmpleado.setModel(tabla);
  407. } catch (Exception e) {
  408. JOptionPane.showConfirmDialog(this, "Error al mostrar Datos "+e.toString());
  409. }
  410. }
  411.  
  412. limpiar
  413.  
  414. public void limpiar(){
  415. this.jTxtCodigo.setText("");
  416. this.jTxtNombre.setText("");
  417. this.jTxtEdad.setText("");
  418. this.jTxtSueldo.setText("");
  419. }
  420.  
  421. llenado tabla
  422.  
  423. private void tbEmpleadoMouseClicked(java.awt.event.MouseEvent evt) {
  424. int fila =this.tbEmpleado.getSelectedRow();
  425. this.jTxtCodigo.setText(String.valueOf(this.tbEmpleado.getValueAt(fila, 0)));
  426. this.jTxtNombre.setText(String.valueOf(this.tbEmpleado.getValueAt(fila, 1)));
  427. this.jTxtEdad.setText(String.valueOf(this.tbEmpleado.getValueAt(fila, 2)));
  428. this.jTxtSueldo.setText(String.valueOf(this.tbEmpleado.getValueAt(fila, 3)));
  429.  
  430. }
  431.  
  432. primer constructor
  433.  
  434. public frmEmpleado() {
  435. initComponents();
  436. tablaE();
  437. this.setLocationRelativeTo(null);
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement