Advertisement
Guest User

stis2

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