transacción

20
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

Upload: nicole-rodriguez

Post on 22-Apr-2015

339 views

Category:

Entertainment & Humor


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Transacción

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

Page 2: Transacción

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

Page 3: Transacción

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);

Page 4: Transacción

94 labelXY.setBounds(135, 85, 30, 23);

95 ventana.add(labelXY);

96

97

98 //Label "Haz ingresado...."

99 Label labelpuntos = new Label("Ud ingreso los siguientes puntos");

100 labelpuntos.setBackground(Color.decode("#0A0A2A")); //fondo

101 labelpuntos.setAlignment(Label.CENTER);

102 labelpuntos.setForeground(Color.WHITE);

103 labelpuntos.setBounds(55, 155, 220, 30);

104 labelpuntos.setFont(f);

105 ventana.add(labelpuntos);

106

107 Label label01 = new Label("(X , Y)");

108 label01.setBackground(Color.decode("#0A0A2A")); //fondo

109 label01.setAlignment(Label.CENTER);

110 label01.setForeground(Color.WHITE);

111 label01.setBounds(140, 185, 50, 24);

112 label01.setFont(f);

113 ventana.add(label01);

114

115 /////////////////////////////////////////

116 //Label "suma1"

117 Label labelobtener = new Label("Sumatoria X");

118 labelobtener.setForeground(Color.WHITE);

119 labelobtener.setBounds(330, 50, 80, 23);

120 labelobtener.setFont(f);

121 ventana.add(labelobtener);

122

123 //Label "suma1"

124 Label labelobtener2 = new Label("Promedio X");

125 labelobtener2.setForeground(Color.WHITE);

Page 5: Transacción

126 labelobtener2.setBounds(415, 50, 75, 23);

127 labelobtener2.setFont(f);

128 ventana.add(labelobtener2);

129

130 Label labelobtener3 = new Label("sumatoria X^2 ");

131 labelobtener3.setForeground(Color.WHITE);

132 labelobtener3.setBounds(493, 50, 90, 23);

133 labelobtener3.setFont(f);

134 ventana.add(labelobtener3);

135

136 //Textfield Suma1

137

138 textsumaX.setBounds(340, 75, 60, 23);

139 textsumaX.setForeground(Color.white);

140 textsumaX.setBackground(Color.decode("#0A0A2A"));

141 textsumaX.setFont(f);

142 ventana.add(textsumaX);

143

144 //Textfield Suma2

145

146 textpromeX.setBounds(420, 75, 60, 23);

147 textpromeX.setForeground(Color.white);

148 textpromeX.setBackground(Color.decode("#0A0A2A"));

149 textpromeX.setFont(f);

150 ventana.add(textpromeX);

151

152

153 textsumaX2.setBounds(500, 75, 60, 23);

154 textsumaX2.setForeground(Color.white);

155 textsumaX2.setBackground(Color.decode("#0A0A2A"));

156 textsumaX2.setFont(f);

157 ventana.add(textsumaX2);

Page 6: Transacción

158

159

160

161

162 //Textfield X

163

164 textX.setBounds(100, 110, 45, 23);

165 textX.setForeground(Color.decode("#81F7F3"));

166 textX.setBackground(Color.decode("#0A0A2A"));

167 textX.setFont(f);

168 ventana.add(textX);

169

170 //textField Y

171

172

173 textY.setBounds(150, 110, 45, 23);

174 textY.setForeground(Color.decode("#81F7F3"));

175 textY.setBackground(Color.decode("#0A0A2A"));

176 textY.setFont(f);

177 ventana.add(textY);

178

179

180

181 textAB.setBounds(394, 270, 120, 23);

182 textAB.setForeground(Color.decode("#81F7F3"));

183 textAB.setBackground(Color.decode("#0A0A2A"));

184 textAB.setFont(f);

185 ventana.add(textAB);

186

187

188

189

Page 7: Transacción

190 //Label "suma1"

191 Label labelobtener4 = new Label("Sumatoria Y");

192 labelobtener4.setForeground(Color.WHITE);

193 labelobtener4.setBounds(330, 110, 80, 23);

194 labelobtener4.setFont(f);

195 ventana.add(labelobtener4);

196

197 //Label "suma1"

198 Label labelobtener5 = new Label("Promedio Y");

199 labelobtener5.setForeground(Color.WHITE);

200 labelobtener5.setBounds(415, 110, 75, 23);

201 labelobtener5.setFont(f);

202 ventana.add(labelobtener5);

203

204 Label labelobtener6 = new Label("sumatoria Y^2 ");

205 labelobtener6.setForeground(Color.WHITE);

206 labelobtener6.setBounds(493, 110, 90, 23);

207 labelobtener6.setFont(f);

208 ventana.add(labelobtener6);

209

210 Label labelobtener7 = new Label("A");

211 labelobtener7.setForeground(Color.WHITE);

212 labelobtener7.setBounds(365, 170, 40, 23);

213 labelobtener7.setFont(f);

214 ventana.add(labelobtener7);

215

216 //Label "suma1"

217 Label labelobtener8 = new Label("B");

218 labelobtener8.setForeground(Color.WHITE);

219 labelobtener8.setBounds(442, 170, 40, 23);

220 labelobtener8.setFont(f);

221 ventana.add(labelobtener8);

Page 8: Transacción

222

223 Label labelobtener9 = new Label("Regresion ");

224 labelobtener9.setForeground(Color.WHITE);

225 labelobtener9.setBounds(493, 170, 90, 23);

226 labelobtener9.setFont(f);

227 ventana.add(labelobtener9);

228 //Textfield SumaY

229

230 Label labelobtener10 = new Label("y = ax + b ");

231 labelobtener10.setForeground(Color.WHITE);

232 labelobtener10.setBounds(428, 250, 110, 23);

233 labelobtener10.setFont(f);

234 ventana.add(labelobtener10);

235

236 textsumaY.setBounds(340, 135, 60, 23);

237 textsumaY.setForeground(Color.white);

238 textsumaY.setBackground(Color.decode("#0A0A2A"));

239 textsumaY.setFont(f);

240 ventana.add(textsumaY);

241

242 //Textfield promeY

243

244 textpromeY.setBounds(420, 135, 60, 23);

245 textpromeY.setForeground(Color.white);

246 textpromeY.setBackground(Color.decode("#0A0A2A"));

247 textpromeY.setFont(f);

248 ventana.add(textpromeY);

249

250

251 textsumaY2.setBounds(500, 135, 60, 23);

252 textsumaY2.setForeground(Color.white);

253 textsumaY2.setBackground(Color.decode("#0A0A2A"));

Page 9: Transacción

254 textsumaY2.setFont(f);

255 ventana.add(textsumaY2);

256

257 textA.setBounds(340, 200, 60, 23);

258 textA.setForeground(Color.white);

259 textA.setBackground(Color.decode("#0A0A2A"));

260 textA.setFont(f);

261 ventana.add(textA);

262

263 //Textfield promeY

264

265 textB.setBounds(420, 200, 60, 23);

266 textB.setForeground(Color.white);

267 textB.setBackground(Color.decode("#0A0A2A"));

268 textB.setFont(f);

269 ventana.add(textB);

270

271

272 textregresion.setBounds(500, 200, 60, 23);

273 textregresion.setForeground(Color.white);

274 textregresion.setBackground(Color.decode("#0A0A2A"));

275 textregresion.setFont(f);

276 ventana.add(textregresion);

277

278

279 //Textfield X

280

281 textX.setBounds(100, 110, 45, 23);

282 textX.setForeground(Color.decode("#81F7F3"));

283 textX.setBackground(Color.decode("#0A0A2A"));

284 textX.setFont(f);

285 ventana.add(textX);

Page 10: Transacción

286

287 //textField Y

288

289

290 textY.setBounds(150, 110, 45, 23);

291 textY.setForeground(Color.decode("#81F7F3"));

292 textY.setBackground(Color.decode("#0A0A2A"));

293 textY.setFont(f);

294 ventana.add(textY);

295

296

297 //boton Ingresar

298 Button botoningresar = new Button("Ingresar");

299 botoningresar.setBounds(200, 105, 80, 30);

300 botoningresar.setForeground(Color.decode("#F4FA58"));

301 botoningresar.setBackground(Color.decode("#000000")); //fondo

302 botoningresar.setFont(f);

303 botoningresar.setActionCommand("Ingresar");

304 botoningresar.addActionListener(this);

305 ventana.add(botoningresar);

306

307

308 //boton calcular regresion Lineal

309 Button botonlineal = new Button("Reiniciar");

310

311 botonlineal.setForeground(Color.decode("#F4FA58"));

312 botonlineal.setBackground(Color.decode("#000000")); //fondo

313 botonlineal.setFont(f);

314 botonlineal.setActionCommand("Reiniciar");

315 botonlineal.addActionListener(this);

316 botonlineal.setBounds(412, 320, 80, 30);

317 ventana.add(botonlineal);

Page 11: Transacción

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.

Page 12: Transacción

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];

Page 13: Transacción

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++;

Page 14: Transacción

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

Page 15: Transacción

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

Page 16: Transacción

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();

Page 17: Transacción

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

Page 18: Transacción

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

Page 19: Transacción

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;

Page 20: Transacción

606 } finally {

607 try {

608 basedatos.setAutoCommit(true);

609 if (psdatos != null) {

610 psdatos.close();

611 }

612

613 basedatos.close();

614 } catch (SQLException ex) {

615 System.out.println("conexion.setAutoCommit(true) no ejecutado: " + ex.getMessage());

616 }

617 }

618

619

620

621

622

623 }

624 }

625

626