Guest User

Untitled

a guest
May 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.11 KB | None | 0 0
  1. package sistemaventas;
  2.  
  3. import java.sql.Date;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.text.DateFormat;
  8. import java.text.SimpleDateFormat;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.table.DefaultTableModel;
  13.  
  14. public class ventaVer extends javax.swing.JFrame {
  15.  
  16. DefaultTableModel modeloTabla;
  17. conector.conector con = new conector.conector();
  18. public ventaVer() {
  19. modeloTabla = new DefaultTableModel(null, GetColumnas());
  20. setFilas();
  21. initComponents();
  22. jTable1.setDefaultEditor(Object.class, null);
  23. total.setEditable(false);
  24.  
  25. }
  26.  
  27. private String[] GetColumnas() {
  28. String columna[] = new String[]{"Id","Articulo","Nombre","Cantidad","Precio","Fecha" };
  29. return columna;
  30. }
  31.  
  32. private void setFilas()
  33. {
  34.  
  35. if (modeloTabla.getRowCount() > 0) {
  36. for (int i = modeloTabla.getRowCount() - 1; i > -1; i--) {
  37. modeloTabla.removeRow(i);
  38. }
  39. }
  40. try {
  41. String sql = "SELECT VEN.Id, VEN.Articulo, ART.Nombre , VEN.Cantidad, VEN.Precio, VEN.Fecha FROM `ventas' VEN JOIN articulos art ON (art.Id = Articulo) Order by VEN.Id";
  42. PreparedStatement us = con.conector().prepareStatement(sql);
  43. ResultSet res = us.executeQuery();
  44.  
  45.  
  46. Object datos[] = new Object[6];
  47.  
  48. while (res.next()) {
  49. for (int i = 0; i < 6; i++) {
  50. datos[i] = res.getObject(i + 1);
  51. }
  52. modeloTabla.addRow(datos);
  53.  
  54. }
  55.  
  56. res.close();
  57.  
  58. } catch (SQLException ex)
  59. {
  60.  
  61. }
  62. }
  63.  
  64. @SuppressWarnings("unchecked")
  65. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  66. private void initComponents() {
  67.  
  68. jScrollPane1 = new javax.swing.JScrollPane();
  69. jTable1 = new javax.swing.JTable();
  70. borrar = new javax.swing.JButton();
  71. jButton3 = new javax.swing.JButton();
  72. jLabel1 = new javax.swing.JLabel();
  73. fecha = new javax.swing.JButton();
  74. jLabel2 = new javax.swing.JLabel();
  75. jLabel3 = new javax.swing.JLabel();
  76. jLabel4 = new javax.swing.JLabel();
  77. jScrollPane2 = new javax.swing.JScrollPane();
  78. total = new javax.swing.JTextPane();
  79. jLabel5 = new javax.swing.JLabel();
  80. jSeparator1 = new javax.swing.JSeparator();
  81. desde = new org.jdesktop.swingx.JXDatePicker();
  82. hasta = new org.jdesktop.swingx.JXDatePicker();
  83.  
  84. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  85.  
  86. jTable1.setModel(modeloTabla);
  87. jScrollPane1.setViewportView(jTable1);
  88.  
  89. borrar.setText("Eliminar venta");
  90. borrar.addActionListener(new java.awt.event.ActionListener() {
  91. public void actionPerformed(java.awt.event.ActionEvent evt) {
  92. borrarActionPerformed(evt);
  93. }
  94. });
  95.  
  96. jButton3.setText("Actualizar");
  97. jButton3.addActionListener(new java.awt.event.ActionListener() {
  98. public void actionPerformed(java.awt.event.ActionEvent evt) {
  99. jButton3ActionPerformed(evt);
  100. }
  101. });
  102.  
  103. jLabel1.setText("Sus ventas");
  104.  
  105. fecha.setText("Buscar por fecha");
  106. fecha.addActionListener(new java.awt.event.ActionListener() {
  107. public void actionPerformed(java.awt.event.ActionEvent evt) {
  108. fechaActionPerformed(evt);
  109. }
  110. });
  111.  
  112. jLabel2.setText("Desde:");
  113.  
  114. jLabel3.setText("Hasta:");
  115.  
  116. jLabel4.setText("Total fecha");
  117.  
  118. jScrollPane2.setViewportView(total);
  119.  
  120. jLabel5.setText("$");
  121.  
  122. jSeparator1.setForeground(new java.awt.Color(204, 204, 204));
  123.  
  124. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  125. getContentPane().setLayout(layout);
  126. layout.setHorizontalGroup(
  127. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  128. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  129. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  130. .addGroup(layout.createSequentialGroup()
  131. .addGap(11, 11, 11)
  132. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  133. .addGroup(layout.createSequentialGroup()
  134. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  135. .addComponent(jLabel3)
  136. .addComponent(jLabel2))
  137. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  138. .addGroup(layout.createSequentialGroup()
  139. .addComponent(fecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  140. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
  141. .addGroup(layout.createSequentialGroup()
  142. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  143. .addGroup(layout.createSequentialGroup()
  144. .addGap(28, 28, 28)
  145. .addComponent(jLabel5)
  146. .addGap(3, 3, 3)
  147. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  148. .addComponent(jLabel4)
  149. .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
  150. .addGroup(layout.createSequentialGroup()
  151. .addContainerGap()
  152. .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
  153. .addGroup(layout.createSequentialGroup()
  154. .addContainerGap()
  155. .addComponent(desde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  156. .addGroup(layout.createSequentialGroup()
  157. .addContainerGap()
  158. .addComponent(hasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  159. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  160. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 539, javax.swing.GroupLayout.PREFERRED_SIZE)
  161. .addGap(19, 19, 19)
  162. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  163. .addComponent(borrar, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
  164. .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  165. .addGap(26, 26, 26))
  166. .addGroup(layout.createSequentialGroup()
  167. .addGap(388, 388, 388)
  168. .addComponent(jLabel1)
  169. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  170. );
  171. layout.setVerticalGroup(
  172. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  173. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  174. .addGap(6, 6, 6)
  175. .addComponent(jLabel1)
  176. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  177. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  178. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  179. .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
  180. .addGroup(layout.createSequentialGroup()
  181. .addComponent(jLabel2)
  182. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  183. .addComponent(desde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  184. .addGap(11, 11, 11)
  185. .addComponent(jLabel3)
  186. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  187. .addComponent(hasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  188. .addGap(13, 13, 13)
  189. .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  190. .addGap(26, 26, 26)
  191. .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
  192. .addGap(30, 30, 30)
  193. .addComponent(jLabel4)
  194. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  195. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  196. .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  197. .addComponent(jScrollPane2))))
  198. .addGroup(layout.createSequentialGroup()
  199. .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
  200. .addGap(18, 18, 18)
  201. .addComponent(borrar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
  202. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  203. );
  204.  
  205. pack();
  206. }// </editor-fold>
  207.  
  208. private void borrarActionPerformed(java.awt.event.ActionEvent evt) {
  209.  
  210. int reply = JOptionPane.showConfirmDialog(rootPane, "¿Está seguro de eliminar esta venta?", "Eliminar venta", JOptionPane.YES_NO_OPTION);
  211.  
  212. if (reply == JOptionPane.YES_OPTION) {
  213.  
  214. int row = jTable1.getSelectedRow();
  215. String value = jTable1.getModel().getValueAt(row, 0).toString();
  216. try
  217. {
  218. String del = "DELETE from ventas where id = ?";
  219. PreparedStatement stat = con.conector().prepareStatement(del);
  220. stat.setString(1, value);
  221. stat.executeUpdate();
  222.  
  223. } catch (Exception e) {
  224. JOptionPane.showMessageDialog(null,
  225. "No se pudo eliminar",
  226. "Error",
  227. JOptionPane.WARNING_MESSAGE);
  228. }
  229. JOptionPane.showMessageDialog(null, "Venta eliminada", "Éxito al borrar", JOptionPane.INFORMATION_MESSAGE);
  230. }
  231.  
  232. setFilas();
  233. }
  234.  
  235. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  236. setFilas();
  237. }
  238.  
  239.  
  240. private void EliminarTodasLasFilas(DefaultTableModel dtm)
  241. {
  242. if (dtm.getRowCount() > 0) {
  243. for (int i = dtm.getRowCount() - 1; i > -1; i--) {
  244. dtm.removeRow(i);
  245. }
  246. }
  247.  
  248. }
  249. public void totalFecha(){
  250. try {
  251.  
  252.  
  253. java.util.Date desdeD = desde.getDate();
  254. DateFormat dDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  255. String dDate = dDateFormat.format(desdeD);
  256.  
  257. java.util.Date hastaH = hasta.getDate();
  258. DateFormat hDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  259. String hzDate = hDateFormat.format(hastaH);
  260.  
  261. String sql = "SELECT SUM(VEN.Cantidad * VEN.Precio) n" +
  262.  
  263. "FROM `ventas` VEN n" +
  264. //"JOIN articulos ART ON (ART.Id = VEN.articulo)n" +
  265. "WHERE CAST(VEN.Fecha as DATE) BETWEEN '"+dDate+"' AND '"+hzDate+"'";
  266. PreparedStatement us;
  267. us = con.conector().prepareStatement(sql);
  268. ResultSet res = us.executeQuery();
  269. //String resultado = res.getObject(0).toString();
  270.  
  271.  
  272. Object datos[] = new Object[1];
  273. while (res.next()) {
  274. for (int i = 0; i < 1; i++) {
  275. datos[i] = res.getObject(i + 1);
  276. //JOptionPane.showMessageDialog(null, "El total entre el "+ dDate +" y el "+ hzDate +" es: "+datos[i].toString(),"total",JOptionPane.INFORMATION_MESSAGE);
  277. total.setText(datos[i].toString());
  278. }
  279. //modeloTabla.addRow(datos);
  280.  
  281. }
  282.  
  283. res.close();
  284. } catch (SQLException ex) {
  285. Logger.getLogger(ventaVer.class.getName()).log(Level.SEVERE, null, ex);
  286. }
  287.  
  288. }
  289.  
  290. private void fechaActionPerformed(java.awt.event.ActionEvent evt) {
  291.  
  292.  
  293. try {
  294.  
  295. java.util.Date desdeD = desde.getDate();
  296. DateFormat dDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  297. String dDate = dDateFormat.format(desdeD);
  298.  
  299. java.util.Date hastaH = hasta.getDate();
  300. DateFormat hDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  301. String hzDate = hDateFormat.format(hastaH);
  302.  
  303. String sql = "SELECT VEN.Id, VEN.Articulo, art.Nombre, VEN.Cantidad, VEN.Precio,VEN.Fecha FROM `ventas` VEN JOIN articulos art ON (art.Id = Articulo) WHERE CAST(VEN.Fecha as DATE) BETWEEN '"+dDate+"' AND '"+hzDate+"' ORDER BY VEN.Id";
  304. PreparedStatement us;
  305. us = con.conector().prepareStatement(sql);
  306. ResultSet res = us.executeQuery();
  307.  
  308.  
  309. Object datos[] = new Object[6];
  310. EliminarTodasLasFilas(modeloTabla);
  311. while (res.next()) {
  312. for (int i = 0; i < 6; i++) {
  313. datos[i] = res.getObject(i + 1);
  314. }
  315. modeloTabla.addRow(datos);
  316. }
  317.  
  318. res.close();
  319. totalFecha();
  320.  
  321. } catch (SQLException ex) {
  322. Logger.getLogger(ventaVer.class.getName()).log(Level.SEVERE, null, ex);
  323. }
  324.  
  325. }
  326.  
  327. /**
  328. * @param args the command line arguments
  329. */
  330. public static void main(String args[]) {
  331. /* Set the Nimbus look and feel */
  332. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  333. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  334. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  335. */
  336. try {
  337. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  338. if ("Nimbus".equals(info.getName())) {
  339. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  340. break;
  341. }
  342. }
  343. } catch (ClassNotFoundException ex) {
  344. java.util.logging.Logger.getLogger(ventaVer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  345. } catch (InstantiationException ex) {
  346. java.util.logging.Logger.getLogger(ventaVer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  347. } catch (IllegalAccessException ex) {
  348. java.util.logging.Logger.getLogger(ventaVer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  349. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  350. java.util.logging.Logger.getLogger(ventaVer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  351. }
  352. //</editor-fold>
  353.  
  354. /* Create and display the form */
  355. java.awt.EventQueue.invokeLater(new Runnable() {
  356. public void run() {
  357. new ventaVer().setVisible(true);
  358. }
  359. });
  360. }
  361.  
  362. // Variables declaration - do not modify
  363. private javax.swing.JButton borrar;
  364. private org.jdesktop.swingx.JXDatePicker desde;
  365. private javax.swing.JButton fecha;
  366. private org.jdesktop.swingx.JXDatePicker hasta;
  367. private javax.swing.JButton jButton3;
  368. private javax.swing.JLabel jLabel1;
  369. private javax.swing.JLabel jLabel2;
  370. private javax.swing.JLabel jLabel3;
  371. private javax.swing.JLabel jLabel4;
  372. private javax.swing.JLabel jLabel5;
  373. private javax.swing.JScrollPane jScrollPane1;
  374. private javax.swing.JScrollPane jScrollPane2;
  375. private javax.swing.JSeparator jSeparator1;
  376. private javax.swing.JTable jTable1;
  377. private javax.swing.JTextPane total;
  378. // End of variables declaration
  379. }
Add Comment
Please, Sign In to add comment