1. CLASE TRANSACCION
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5 package transacciones;
6
7
8 import java.applet.*;
9 import java.awt.*;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 import java.sql.SQLException;
13 import java.sql.PreparedStatement;
14 import utilidades.conexion;
15 import java.lang.NumberFormatException;
16 import java.sql.Connection;
17 import java.sql.ResultSet;
18 import javax.swing.ImageIcon;
19 import javax.swing.JFrame;
20 import javax.swing.JLabel;
21 import javax.swing.JOptionPane;
22 import transacciones.transaccion_1;
23
24
25 public class transaccion_1 extends JFrame implements ActionListener {
26
27 public JFrame ventana = new JFrame("Ingresar Empleados");
28 //Interfaz
29
2. 30
31 //TextFields que estan en el applet en
32 TextField textX = new TextField(); //Ingresa puntos X
33 TextField textY = new TextField();//Ingresa puntos Y
34 TextArea areapuntos = new TextArea();
35 TextField textsumaX = new TextField();
36 TextField textsumaY = new TextField();
37 TextField textpromeX = new TextField();
38 TextField textpromeY = new TextField();
39 TextField textsumaX2 = new TextField();
40 TextField textsumaY2 = new TextField();
41 TextField textAB = new TextField();
42 TextField textA = new TextField();
43 TextField textB = new TextField();
44 TextField textregresion = new TextField();
45 Font f = new Font("Arial", Font.LAYOUT_RIGHT_TO_LEFT, 12);
46 Font g = new Font("Arial", Font.LAYOUT_RIGHT_TO_LEFT, 20);
47
48 public transaccion_1() {
49
50
51 ventana.setSize(630, 500);
52 ventana.setResizable(false);
53 ventana.setLayout(null);
54 ventana.getContentPane().setBackground(Color.decode("#086A87"));
55 ventana.setVisible(true);
56
57 //Fuentes
58
59
60 //Applet
61
3. 62
63
64
65
66 //Label Titulo
67 Label labeltitulo = new Label("R E G R E S I O N L I N E A L");
68 labeltitulo.setBackground(Color.decode("#086A87")); //fondo
69 labeltitulo.setForeground(Color.decode("#FFFFFF"));//Color de fuente
70 labeltitulo.setFont(g); //agregamos la fuente G para el label
71 labeltitulo.setAlignment(Label.CENTER); //Para que el label quede centrado
72 labeltitulo.setBounds(55, 10, 550, 23); // Coordenadas para posicionar el label, x, y tamaño x,y
73 ventana.add(labeltitulo); //adherimos el label al applet
74
75 //Es el mismo formato para toda la interfaz....
76
77
78 //Label Ingrese valores
79 Label textoingrese = new Label("Ingrese valores para los puntos X , Y ");
80 textoingrese.setBackground(Color.decode("#0A0A2A")); //fondo
81 textoingrese.setForeground(Color.white); //letras
82 textoingrese.setFont(f);
83 textoingrese.setAlignment(Label.CENTER);
84 textoingrese.setBounds(55, 55, 220, 23);
85 ventana.add(textoingrese);
86
87
88 //Label (X,Y)
89 Label labelXY = new Label("(X,Y)");
90 labelXY.setBackground(Color.decode("#086A87")); //fondo
91 labelXY.setForeground(Color.WHITE);
92 labelXY.setFont(f);
93 labelXY.setAlignment(Label.LEFT);
11. 318
319 //tex area1
320
321 areapuntos.setBounds(55, 210, 220, 250);
322 areapuntos.setForeground(Color.decode("#FFFFFF")); //letras
323 areapuntos.setBackground(Color.decode("#000000")); //fondo
324 areapuntos.setEditable(false);
325 ventana.add(areapuntos);
326
327
328
329
330
331
332
333 }
334
335 public static void main (String[]Args){
336
337 new transaccion_1();
338
339
340 }
341
342
343 String X = "";
344 String Y = "";
345 int contador = 0;
346
347
348 public void actionPerformed(ActionEvent ae) { //accion al presionar el boton
349 // Declaramos los datos en bd.
12. 350
351
352 String comando = ae.getActionCommand(); //Diferencia los botones del aplet
353
354
355 int i = 0;
356 int x = 0;
357 int y = 0;
358
359 String X1 = "";
360 String Y1 = "";
361 try{
362
363 Double.valueOf(textX.getText());
364 Double.valueOf(textY.getText());
365 X1 = textX.getText() + "-"; //guaradamos los datos ingresados con un guion para serparar c/numero
366 Y1 = textY.getText() + "-";//guaradamos los datos ingresados
367 }catch(NumberFormatException e){
368 textY.setText("");
369 textX.setText("");
370 }
371
372 if(textX.getText().length()!=0 && textY.getText().length()!=0){
373
374 X = X1 + X; //acumulador de datos
375 Y = Y1 + Y;
376 String coma = "-";
377 int largoX = X.length() / 2; // ajustamos el largo de nuestro arreglo
378 int largoY = Y.length() / 2;
379 //declaramos variables
380 double ArregloX[] = new double[largoX];
381 double ArregloY[] = new double[largoY];
13. 382
383 String numerox = "";
384 String numeroy = "";
385 double sumax = 0;
386 double sumay = 0;
387 double sumax2 = 0;
388 double sumay2 = 0;
389 double sumaxy = 0;
390 //GUARDA X %%%%%%%%%%%%%%%%%%%%%%
391
392 if (comando.equals("Ingresar")){
393
394
395 for (i = 0; i < X.length(); i++) {
396 if (coma.indexOf(X.charAt(i)) >= 0) {// si se encuantra el guion dentro del acumlador X
397 ArregloX[x] = Double.valueOf(numerox);// guardar el String numeros dentro del arreglo;
398 x++;//aumentamos la posicion del arreglo
399 numerox = "";// reiniciamos nuestro string numero;
400 } else {
401 numerox = numerox + X.substring(i, i + 1);// si la posicion revisada no es un guion guardar dentro de un string
402 }
403 }
404
405
406
407
408
409 /// GUARDA Y %%%%%%%%%%%%%%%%%%%%%%
410 for (i = 0; i < Y.length(); i++) {
411 if (coma.indexOf(Y.charAt(i)) >= 0) {
412 ArregloY[y] = Double.valueOf(numeroy);
413 y++;
14. 414 numeroy = "";
415 } else {
416 numeroy = numeroy + Y.substring(i, i + 1);
417 }
418 }
419
420 // usar contructor datos para poder guardar los puntos
421 datos puntos = new datos( Double.valueOf(textX.getText()), Double.valueOf(textY.getText()));
422 guardarDatosenSql(puntos);
423
424 ///////////////Imprimir todos los datos //////////////////////
425
426 String nuevo = "";
427 String output = "";
428 for (i = 0; i < x; i++) {
429 // if (ArregloX[i] != 0.0 || ArregloY[i] != 0.0) { //imprime el arreglo solo si con distintos de 0.0
430 nuevo = String.valueOf(ArregloX[i] + " " + ArregloY[i]);
431 output = nuevo + "n" + output; //acumador de valores X e Y
432 // }
433
434 }
435 areapuntos.setText(output); // muestra el acumulador en el textArea los valore que se han ido ingresando
436
437
438 for (i = 0; i < y; i++) {//realiza cualculos para la regreson
439 sumax += ArregloX[i];
440 sumay += ArregloY[i];
441 sumax2 += ArregloX[i] * ArregloX[i];
442 sumay2 += ArregloY[i] * ArregloY[i];
443 sumaxy += ArregloX[i] * ArregloY[i];
444 }
445
15. 446 double promsumax = (double) Math.round((sumax / x) * 100) / 100;// promedio de x
447 double promsumay = (double) Math.round((sumay / y) * 100) / 100; //promedio de Y
448 double a = (double) Math.round((sumaxy / sumax2) * 100) / 100; //valor de A
449 double b = (double) Math.round((promsumay - a * sumax) * 100) / 100;//valor de B
450 double R = (double) Math.round(((sumax * sumax) / (sumax * sumay)) * 1000) / 1000;// valor de la reggresion
451
452
453 // transforma la varible double en string
454 String sumatoriax = String.valueOf(sumax);
455 String sumatoriay = String.valueOf(sumay);
456 String sumatoriax2 = String.valueOf(sumax2);
457 String sumatoriay2 = String.valueOf(sumay2);
458 String promsumaX = String.valueOf(promsumax);
459 String promsumaY = String.valueOf(promsumay);
460 String A = String.valueOf(a);
461 String B = String.valueOf(b);
462 String Regresion = String.valueOf(R);
463
464 // se imprime la variable en la interfaz cada una en el text fiel o Areas
465 textsumaX.setText(sumatoriax);
466 textsumaY.setText(sumatoriay);
467 textpromeX.setText(promsumaX);
468 textpromeY.setText(promsumaY);
469 textsumaX2.setText(sumatoriax2);
470 textsumaY2.setText(sumatoriay2);
471 textA.setText(A);
472 textB.setText(B);
473 textregresion.setText(Regresion);
474 textAB.setText("y ="+A+"x +"+B);
475 textX.setText("");
476 textY.setText("");
477
16. 478
479 }
480
481
482
483
484 if (comando.equals("Reiniciar")) {// si el boton reiniciar es presionado
485 //
486 X = "";
487 Y = "";
488 for (i = 0; i < x; i++) {
489 ArregloX[i] = 0.0;// guardar el String numeros dentro del arreglo;
490 //aumentamos la posicion del arreglo
491 ArregloY[i] = 0.0;// reiniciamos nuestro string numero;
492 }
493 // contador = 0;
494 //deja en blanco todos los textfiel o texAreas
495 textX.setText("");
496 textY.setText("");
497 textsumaX.setText(""); // se imprime la variable en la interfaz
498 textsumaY.setText("");
499 textpromeX.setText("");
500 textpromeY.setText("");
501 textsumaX2.setText("");
502 textsumaY2.setText("");
503 textA.setText("");
504 textB.setText("");
505 textregresion.setText("");
506 areapuntos.setText("");
507 textAB.setText("");
508 conexion cone = new conexion();
509 Connection basedatos = (Connection) cone.getConexionMysql();
17. 510 String sqldatos = "DELETE from datos";
511
512 //insertamos los datos a la tabla datos , ingresando x e y
513 PreparedStatement psdatos = null;
514
515
516 try {
517 if (basedatos.getAutoCommit()) { //BUSCAR QE ES getAutoCommit
518 basedatos.setAutoCommit(false);
519 }
520 psdatos = basedatos.prepareStatement(sqldatos);
521 System.out.println("prueba");
522 psdatos.executeUpdate(); // ejecutar la sentencia en la base de datos
523 JOptionPane.showMessageDialog(null,"se eliminaron correctamente");
524
525
526 basedatos.commit();
527
528 } catch (SQLException ex) {
529
530
531 try {
532 basedatos.rollback();
533 } catch (SQLException ex1) {
534 System.out.println("Error al ejecutar el rollback");
535 }
536
537
538
539 String respuesta = "Error al guardar Los Datos: " + ex.getMessage();
540 System.out.println(respuesta);
541
18. 542 } finally {
543 try {
544 basedatos.setAutoCommit(true);
545 if (psdatos != null) {
546 psdatos.close();
547 }
548
549 basedatos.close();
550 } catch (SQLException ex) {
551
552 System.out.println("conexion.setAutoCommit(true) no ejecutado: " + ex.getMessage());
553 }
554 }
555
556
557
558 }
559
560
561 } else JOptionPane.showMessageDialog(null, "A ingresado datos nulos");}
562
563
564
565 //Mdetodo que guarda los puntos en la base de datos;
566 public String guardarDatosenSql(datos puntos) {
567 conexion cone = new conexion();
568 Connection basedatos = (Connection) cone.getConexionMysql();
569 String sqldatos = "insert into datos values(?,?)";
570 //insertamos los datos a la tabla datos , ingresando x e y
571 PreparedStatement psdatos = null;
572
573
19. 574 try {
575 if (basedatos.getAutoCommit()) { //BUSCAR QE ES getAutoCommit
576 basedatos.setAutoCommit(false);
577 }
578
579 psdatos = basedatos.prepareStatement(sqldatos);
580 // psdatos.setInt(1, puntos.contador); // usa metodos para introducir a la bd
581 psdatos.setDouble(1, puntos.x);///Guarda puntos en la base de datos
582 psdatos.setDouble(2, puntos.y);
583 psdatos.executeUpdate(); // ejecutar la sentencia en la base de datos
584
585
586
587 JOptionPane.showMessageDialog(null,"Los datos han sido guardados correctamente");
588
589 System.out.println(psdatos);
590 basedatos.commit();
591 return "ok.";
592 } catch (SQLException ex) {
593
594
595 try {
596 basedatos.rollback();
597 } catch (SQLException ex1) {
598 System.out.println("Error al ejecutar el rollback");
599 }
600
601
602
603 String respuesta = "Error al guardar Los Datos: " + ex.getMessage();
604 System.out.println(respuesta);
605 return respuesta;