Advertisement
Guest User

codigo

a guest
Dec 8th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.22 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class TransactionMySQL {
  6.  
  7. Connection conn = null;
  8. Statement stmt = null;
  9. BufferedReader in = null;
  10.  
  11. static final String URL = "jdbc:mysql://localhost/";
  12. static final String BD = "ponchito"; // especificar: el nombre de la BD,
  13. static final String USER = "test"; // el nombre de usuario
  14. static final String PASSWD = "password";// el password del usuario
  15.  
  16. public TransactionMySQL() throws SQLException, Exception {
  17.  
  18. // this will load the MySQL driver, each DB has its own driver
  19. Class.forName( "com.mysql.jdbc.Driver" );
  20. System.out.print( "Connecting to the database... " );
  21.  
  22. // setup the connection with the DB
  23. conn = DriverManager.getConnection( URL+BD, USER, PASSWD );
  24. System.out.println( "connected\n\n" );
  25.  
  26. conn.setAutoCommit( false ); // inicio de la 1a transacción
  27. stmt = conn.createStatement();
  28. in = new BufferedReader( new InputStreamReader(System.in) );
  29. }
  30.  
  31. private void dumpResultSet( ResultSet rset ) throws SQLException {
  32.  
  33. ResultSetMetaData rsetmd = rset.getMetaData();
  34. int i = rsetmd.getColumnCount();
  35.  
  36. while( rset.next() ) {
  37.  
  38. for( int j = 1; j <= i; j++ ) {
  39. System.out.print( rset.getString(j) + "\t" );
  40. }
  41. System.out.println();
  42. }
  43. }
  44.  
  45. private void query( String statement ) throws SQLException {
  46.  
  47. ResultSet rset = stmt.executeQuery( statement );
  48. System.out.println( "Results:" );
  49. dumpResultSet( rset );
  50.  
  51. System.out.println();
  52. rset.close();
  53. }
  54.  
  55. private String simpleQuery( String statement )
  56. throws SQLException {
  57.  
  58. ResultSet rset = stmt.executeQuery( statement );
  59. String result = rset.getString(1);
  60. rset.close();
  61. return result;
  62. }
  63.  
  64. private void close() throws SQLException {
  65.  
  66. stmt.close();
  67. conn.close();
  68. }
  69.  
  70. private boolean menuAgent() throws SQLException, IOException {
  71. System.out.println( "\nIngrese su nombre" );
  72. String nombre = in.readLine();
  73. System.out.println( "\nIngrese su contrasena" );
  74. String contrasena = in.readLine();
  75. statement = "select * from agente where nombre = \"" + nombre + "\" and clave = \"" + contrasena + "\"";
  76. String test = simpleQuery(statement);
  77. if (test.equals("")){
  78. return true;
  79. }
  80.  
  81. String statement;
  82. System.out.println( "(1) Mostrar lista completa de ciudades\n" );
  83. System.out.println( "(2) Mostrar lista completa de circuitos\n" );
  84. System.out.println( "(3) Filtrar hoteles por ciudad\n" );
  85. System.out.println( "(4) Filtrar sitios a visitar por ciudad\n" );
  86. System.out.println( "(5) Filtrar circuitos a visitar por disponibilidad\n" );
  87. System.out.println( "(6) Generar simulacion\n" );
  88. //buscar simulacion existente
  89. System.out.println( "(7) Validar todos los cambios\n" );
  90. System.out.println( "(8) Abortar todos los cambios\n" );
  91. System.out.println( "(9) Consultar\n\n" );
  92. System.out.println( "(10) Salir\n\n" );
  93. System.out.print( "Option: " );
  94.  
  95. switch( Integer.parseInt( "0" + in.readLine() ) ) {
  96.  
  97. case 1:
  98. if (test.equals("")){
  99. System.out.println("Query failed try again");
  100. break;
  101. }
  102. query( "select * from ciudad group by country" );
  103. break;
  104.  
  105. case 2:
  106. if (test.equals("")){
  107. System.out.println("Query failed try again");
  108. break;
  109. }
  110. query( "select * from circuito group by country" );
  111. break;
  112.  
  113. case 3:
  114. System.out.println( "\nSintax: ciudad, pais" );
  115. String parts = in.readLine().split(", ");
  116.  
  117. statement = "select * from hotel where ciudad=\"" + parts[0] + "\" and pais=\"" + parts[1] + "\"";
  118. String test = simpleQuery(statement);
  119. if (test.equals("")){
  120. System.out.println("Query failed try again");
  121. break;
  122. }
  123. query( statement );
  124. break;
  125.  
  126. case 4: System.out.println( "\nSintax: ciudad, pais" );
  127. String parts = in.readLine().split(", ");
  128. statement = "select * from lugaravisitar where ciudad=\"" + parts[0] + "\" and pais=\"" + parts[1] + "\"";
  129. if (test.equals("")){
  130. System.out.println("Query failed try again");
  131. break;
  132. }
  133. query( statement );
  134. break;
  135.  
  136. case 5: System.out.println( "\nFecha de salida (sintax: YYYY-MM-DD)" );
  137. String fechaSalida = in.readLine();
  138. System.out.println( "\nFecha de regreso (sintax: YYYY-MM-DD)" );
  139. String fechaRegreso = in.readLine();
  140. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.fechasalida=\"" + fechaSalida + "\" and DATE_ADD(f.fechasalida, INTERVAL c.duracion DAY)=\"" + fechaRegreso + "\"";
  141. if (test.equals("")){
  142. System.out.println("Query failed try again");
  143. break;
  144. }
  145. query( statement );
  146. break;
  147.  
  148. case 6: System.out.println( "\nPara abortar deje en blanco" );
  149.  
  150. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  151. Date today = new Date();
  152. date = dateFormat.format(today);
  153.  
  154. System.out.println( "\nIngresa tu nombre" );
  155. String nombre = in.readLine();
  156. if (nombre==""){
  157. break;
  158. }
  159. System.out.println( "\nIngresa el numero de personas que viajaran" );
  160. String numero = in.readLine();
  161. if (numero==""){
  162. break;
  163. }
  164. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.nbpersonas=" + numero;
  165. if (test.equals("")){
  166. System.out.println("Query failed try again");
  167. break;
  168. }
  169. query( statement );
  170.  
  171.  
  172. System.out.println( "\nFecha de salida (sintax: YYYY-MM-DD)" );
  173. String fechaSalida = in.readLine();
  174. if (fechaSalida==""){
  175. break;
  176. }
  177. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.fechasalida=\"" + fechaSalida + "\"and f.nbpersonas=" + numero;
  178. if (test.equals("")){
  179. System.out.println("Query failed try again");
  180. break;
  181. }
  182. query( statement );
  183.  
  184.  
  185. System.out.println( "\nFecha de regreso (sintax: YYYY-MM-DD)" );
  186. String fechaRegreso = in.readLine();
  187. if (fechaRegreso==""){
  188. break;
  189. }
  190. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.fechasalida=\"" + fechaSalida + "\" and DATE_ADD(f.fechasalida, INTERVAL c.duracion DAY)=\"" + fechaRegreso + "\"" + fechaSalida + "\"and f.nbpersonas=" + numero;
  191. if (test.equals("")){
  192. System.out.println("Query failed try again");
  193. break;
  194. }
  195. query( statement );
  196.  
  197.  
  198. System.out.println( "\nIngresa el pais deseado" );
  199. String pais = in.readLine();
  200. if (pais==""){
  201. break;
  202. }
  203. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.fechasalida=\"" + fechaSalida + "\" and c.paisllegada =\"" +pais + "\"and f.nbpersonas=" + numero;
  204. if (test.equals("")){
  205. System.out.println("Query failed try again");
  206. break;
  207. }
  208. query( statement );
  209.  
  210.  
  211. System.out.println( "\nIngresa el ID del tour deseado" );
  212. String id = in.readLine();
  213. if (id==""){
  214. break;
  215. }
  216. statement = "insert into relacion2 values ( "+ today+name +", '"+ id +"', '" + fechaSalida + "')";
  217. stmt.executeUpdate( statement );
  218.  
  219.  
  220. statement = "select * from hotel where ciudad=\"" + ciudad + "\" and pais=\"" + pais + "\"";
  221. if (test.equals("")){
  222. System.out.println("Query failed try again");
  223. break;
  224. }
  225. query( statement );
  226.  
  227.  
  228. System.out.println( "\nSelecciona un hotel para tu estancia" );
  229. String hotel = in.readLine();
  230. if (hotel==""){
  231. break;
  232. }
  233.  
  234. statement = "insert into relacion values ( "+ today+name +", '"+ hotel +"', '" + ciudad + "', '" + pais + "')";
  235. stmt.executeUpdate( statement );
  236.  
  237.  
  238.  
  239. System.out.println( "\nDeseas agregar mas lugares a tu simulacion: si (1), no (0)" );
  240.  
  241. int selection = Integer.parseInt(in.readLine());
  242.  
  243. if (selection==1){
  244. statement = "select * from lugaravisitar where ciudad=\"" + ciudad + "\" and pais=\"" + pais + "\"";
  245. if (test.equals("")){
  246. System.out.println("Query failed try again");
  247. break;
  248. }
  249. query( statement );
  250. System.out.println( "\nSelecciona un lugar a visitar para añadir" );
  251. String lugar = in.readLine();
  252. statement = "insert into relacion values ( "+ today+name +", '"+ lugar +"', '" + ciudad + "', '" + pais + "')";
  253. stmt.executeUpdate( statement );
  254. while (selection==1){
  255. System.out.println( "\nDeseas agregar mas lugar a tu simulacion: si (1), no (0)" );
  256. selection = Integer.parseInt(in.readLine());
  257. statement = "select * from lugaravisitar where ciudad=\"" + ciudad + "\" and pais=\"" + pais + "\"";
  258. if (test.equals("")){
  259. System.out.println("Query failed try again");
  260. break;
  261. }
  262. query( statement );
  263. System.out.println( "\nSelecciona un lugar a visitar para añadir" );
  264. lugar = in.readLine();
  265. statement = "insert into relacion values ( "+ today+name +", '"+ lugar +"', '" + ciudad + "', '" + pais + "')";
  266. stmt.executeUpdate( statement );
  267. }
  268. }
  269.  
  270. statement = "select precio from hotel where ciudad=\"" + ciudad + "\" and pais=\"" + pais + "\" and nombre=\"" + hotel + "\"";
  271. int precioHotel = Integer.parseInte(simpleQuery( statement ));
  272.  
  273. statement = "select preciodesayuno from hotel where ciudad=\"" + ciudad + "\" and pais=\"" + pais + "\" and nombre=\"" + hotel + "\"";
  274. int precioDesayunoHotel = Integer.parseInte(simpleQuery( statement ));
  275.  
  276. statement = "select sum(lugaravisitar.precio) from lugaravisitar, relacion where relacion.nombre=lugaravisitar.nombre and relacion.ciudad=lugaravisitar.ciudad and relacion.pais=lugaravisitar.pais and relacion.id=\"" + today+name + "\"";
  277. int precioLugares = Integer.parseInte(simpleQuery( statement ));
  278.  
  279.  
  280. statement = "select circuito.precio from circuito, relacion2, fechacircuito where relacion2.idcircuito=circuito.id and fechacircuito.idcircuito=circuito.id and relacion2.fechasalida=fechacircuito.fechasalida and relacion2.id=\"" + today+name + "\"";
  281. int precioCircuito = Integer.parseInteger(simpleQuery( statement ));
  282.  
  283. int precio = precioHotel + precioDesayunoHotel + precioLugares + precioCircuito;
  284. statement = "insert into simulacion values ( "+ today+name +", '"+ nombre +"', '"+ fechaSalida +"', '"+ fechaRegreso +"', " + numero + ", " + precio + ")";
  285. stmt.executeUpdate( statement );
  286.  
  287. System.out.println("Su reservacion es: " + today+name);
  288. break;
  289.  
  290. case 7: conn.commit(); // fin de la transacción e inicio de la siguiente
  291. break;
  292.  
  293. case 8: conn.rollback(); // fin de la transacción e inicio de la siguiente
  294. break;
  295.  
  296.  
  297. case 9: System.out.println( "\nIngrese su ID" );
  298. String id = in.readLine();
  299. statement = "select * from simulacion where id=\"" + id + "\"";
  300. if (test.equals("")){
  301. System.out.println("Query failed try again");
  302. break;
  303. }
  304. query( statement );
  305. statement = "select * from relacion where id=\"" + id + "\"";
  306. query( statement );
  307. statement = "select * from relacion2 where id=\"" + id + "\"";
  308. query( statement );
  309. break;
  310.  
  311. case 10: return false;
  312. }
  313.  
  314. return true;
  315.  
  316. }
  317.  
  318. private boolean menuUser() throws SQLException, IOException {
  319.  
  320. System.out.println( "\nIngrese su nombre" );
  321. String nombre = in.readLine();
  322. System.out.println( "\nIngrese su contrasena" );
  323. String contrasena = in.readLine();
  324. statement = "select * from cliente where nombre = \"" + nombre + "\" and clave = \"" + contrasena + "\"";
  325. String test = simpleQuery(statement);
  326. if (test.equals("")){
  327. return true;
  328. }
  329.  
  330. String statement;
  331. System.out.println( "(1) Mostrar lista completa de ciudades\n" );
  332. System.out.println( "(2) Mostrar lista completa de circuitos\n" );
  333. System.out.println( "(3) Filtrar hoteles por ciudad\n" );
  334. System.out.println( "(4) Filtrar sitios a visitar por ciudad\n" );
  335. System.out.println( "(5) Filtrar circuitos a visitar por disponibilidad\n" );
  336. System.out.println( "(6) Consultar\n\n" );
  337. System.out.println( "(7) Salir\n\n" );
  338. System.out.print( "Option: " );
  339.  
  340. switch( Integer.parseInt( "0" + in.readLine() ) ) {
  341.  
  342. case 1:
  343. if (test.equals("")){
  344. System.out.println("Query failed try again");
  345. break;
  346. }
  347. query( "select * from ciudad group by country" );
  348. break;
  349.  
  350. case 2:
  351. query( "select * from circuito group by country" );
  352. break;
  353.  
  354. case 3: System.out.println( "\nSintax: ciudad, pais" );
  355. String parts = in.readLine().split(", ");
  356. statement = "select * from hotel where ciudad=\"" + parts[0] + "\" and pais=\"" + parts[1] + "\"";
  357. if (test.equals("")){
  358. System.out.println("Query failed try again");
  359. break;
  360. }
  361. query( statement );
  362. break;
  363.  
  364. case 4: System.out.println( "\nSintax: ciudad, pais" );
  365. String parts = in.readLine().split(", ");
  366. statement = "select * from lugaravisitar where ciudad=\"" + parts[0] + "\" and pais=\"" + parts[1] + "\"";
  367. if (test.equals("")){
  368. System.out.println("Query failed try again");
  369. break;
  370. }
  371. query( statement );
  372. break;
  373.  
  374. case 5: System.out.println( "\nFecha de salida (sintax: YYYY-MM-DD)" );
  375. String fechaSalida = in.readLine();
  376. System.out.println( "\nFecha de regreso (sintax: YYYY-MM-DD)" );
  377. String fechaRegreso = in.readLine();
  378. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.fechasalida=\"" + fechaSalida + "\" and DATE_ADD(f.fechasalida, INTERVAL c.duracion DAY)=\"" + fechaRegreso + "\"";
  379. if (test.equals("")){
  380. System.out.println("Query failed try again");
  381. break;
  382. }
  383. query( statement );
  384. break;
  385.  
  386. case 6: System.out.println( "\nIngrese su ID" );
  387. String id = in.readLine();
  388. statement = "select * from reservacion where nombre=\"" + nombre + "\"";
  389. query( statement );
  390. statement = "select * from reservacion, relacion where reservacion.id=relacion.id and reservacion.nombre=\"" + nombre + "\"";
  391. query( statement );
  392. statement = "select * from reservacion, relacion2 where reservacion.id=relacion2.id and reservacion.nombre=\"" + nombre + "\"";
  393. query( statement );
  394. break;
  395.  
  396. case 7: return false;
  397. }
  398.  
  399. return true;
  400. }
  401.  
  402. private boolean menuPublic() throws SQLException, IOException {
  403.  
  404. String statement;
  405. System.out.println( "(1) Mostrar lista completa de ciudades\n" );
  406. System.out.println( "(2) Mostrar lista completa de circuitos\n" );
  407. System.out.println( "(3) Filtrar hoteles por ciudad\n" );
  408. System.out.println( "(4) Filtrar sitios a visitar por ciudad\n" );
  409. System.out.println( "(5) Filtrar circuitos a visitar por disponibilidad\n" );
  410. System.out.println( "(6) Generar simulacion\n" );
  411. System.out.println( "(7) Validar todos los cambios\n" );
  412. System.out.println( "(8) Abortar todos los cambios\n" );
  413. System.out.println( "(9) Consultar\n\n" );
  414. System.out.println( "(10) Salir\n\n" );
  415. System.out.print( "Option: " );
  416.  
  417. switch( Integer.parseInt( "0" + in.readLine() ) ) {
  418.  
  419. case 1:
  420. if (test.equals("")){
  421. System.out.println("Query failed try again");
  422. break;
  423. }
  424. query( "select * from ciudad group by country" );
  425. break;
  426.  
  427. case 2:
  428. if (test.equals("")){
  429. System.out.println("Query failed try again");
  430. break;
  431. }
  432. query( "select * from circuito group by country" );
  433. break;
  434.  
  435. case 3:
  436. System.out.println( "\nSintax: ciudad, pais" );
  437. String parts = in.readLine().split(", ");
  438.  
  439. statement = "select * from hotel where ciudad=\"" + parts[0] + "\" and pais=\"" + parts[1] + "\"";
  440. String test = simpleQuery(statement);
  441. if (test.equals("")){
  442. System.out.println("Query failed try again");
  443. break;
  444. }
  445. query( statement );
  446. break;
  447.  
  448. case 4: System.out.println( "\nSintax: ciudad, pais" );
  449. String parts = in.readLine().split(", ");
  450. statement = "select * from lugaravisitar where ciudad=\"" + parts[0] + "\" and pais=\"" + parts[1] + "\"";
  451. if (test.equals("")){
  452. System.out.println("Query failed try again");
  453. break;
  454. }
  455. query( statement );
  456. break;
  457.  
  458. case 5: System.out.println( "\nFecha de salida (sintax: YYYY-MM-DD)" );
  459. String fechaSalida = in.readLine();
  460. System.out.println( "\nFecha de regreso (sintax: YYYY-MM-DD)" );
  461. String fechaRegreso = in.readLine();
  462. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.fechasalida=\"" + fechaSalida + "\" and DATE_ADD(f.fechasalida, INTERVAL c.duracion DAY)=\"" + fechaRegreso + "\"";
  463. if (test.equals("")){
  464. System.out.println("Query failed try again");
  465. break;
  466. }
  467. query( statement );
  468. break;
  469.  
  470. case 6: System.out.println( "\nPara abortar deje en blanco" );
  471.  
  472. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  473. Date today = new Date();
  474. date = dateFormat.format(today);
  475.  
  476. System.out.println( "\nIngresa tu nombre" );
  477. String nombre = in.readLine();
  478. if (nombre==""){
  479. break;
  480. }
  481. System.out.println( "\nIngresa el numero de personas que viajaran" );
  482. String numero = in.readLine();
  483. if (numero==""){
  484. break;
  485. }
  486. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.nbpersonas=" + numero;
  487. if (test.equals("")){
  488. System.out.println("Query failed try again");
  489. break;
  490. }
  491. query( statement );
  492.  
  493.  
  494. System.out.println( "\nFecha de salida (sintax: YYYY-MM-DD)" );
  495. String fechaSalida = in.readLine();
  496. if (fechaSalida==""){
  497. break;
  498. }
  499. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.fechasalida=\"" + fechaSalida + "\"and f.nbpersonas=" + numero;
  500. if (test.equals("")){
  501. System.out.println("Query failed try again");
  502. break;
  503. }
  504. query( statement );
  505.  
  506.  
  507. System.out.println( "\nFecha de regreso (sintax: YYYY-MM-DD)" );
  508. String fechaRegreso = in.readLine();
  509. if (fechaRegreso==""){
  510. break;
  511. }
  512. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.fechasalida=\"" + fechaSalida + "\" and DATE_ADD(f.fechasalida, INTERVAL c.duracion DAY)=\"" + fechaRegreso + "\"" + fechaSalida + "\"and f.nbpersonas=" + numero;
  513. if (test.equals("")){
  514. System.out.println("Query failed try again");
  515. break;
  516. }
  517. query( statement );
  518.  
  519.  
  520. System.out.println( "\nIngresa el pais deseado" );
  521. String pais = in.readLine();
  522. if (pais==""){
  523. break;
  524. }
  525. statement = "select c.id, c.descripcion, c.ciudadsalida, c.paissalida, c.ciudadllegada, c.paisllegada, c.duracion, c.precio from circuito c , fecha f where c.id = f.id and f.fechasalida=\"" + fechaSalida + "\" and c.paisllegada =\"" +pais + "\"and f.nbpersonas=" + numero;
  526. if (test.equals("")){
  527. System.out.println("Query failed try again");
  528. break;
  529. }
  530. query( statement );
  531.  
  532.  
  533. System.out.println( "\nIngresa el ID del tour deseado" );
  534. String id = in.readLine();
  535. if (id==""){
  536. break;
  537. }
  538. statement = "insert into relacion2 values ( "+ today+name +", '"+ id +"', '" + fechaSalida + "')";
  539. stmt.executeUpdate( statement );
  540.  
  541.  
  542. statement = "select * from hotel where ciudad=\"" + ciudad + "\" and pais=\"" + pais + "\"";
  543. if (test.equals("")){
  544. System.out.println("Query failed try again");
  545. break;
  546. }
  547. query( statement );
  548.  
  549.  
  550. System.out.println( "\nSelecciona un hotel para tu estancia" );
  551. String hotel = in.readLine();
  552. if (hotel==""){
  553. break;
  554. }
  555.  
  556. statement = "insert into relacion values ( "+ today+name +", '"+ hotel +"', '" + ciudad + "', '" + pais + "')";
  557. stmt.executeUpdate( statement );
  558.  
  559.  
  560.  
  561. System.out.println( "\nDeseas agregar mas lugares a tu simulacion: si (1), no (0)" );
  562.  
  563. int selection = Integer.parseInt(in.readLine());
  564.  
  565. if (selection==1){
  566. statement = "select * from lugaravisitar where ciudad=\"" + ciudad + "\" and pais=\"" + pais + "\"";
  567. if (test.equals("")){
  568. System.out.println("Query failed try again");
  569. break;
  570. }
  571. query( statement );
  572. System.out.println( "\nSelecciona un lugar a visitar para añadir" );
  573. String lugar = in.readLine();
  574. statement = "insert into relacion values ( "+ today+name +", '"+ lugar +"', '" + ciudad + "', '" + pais + "')";
  575. stmt.executeUpdate( statement );
  576. while (selection==1){
  577. System.out.println( "\nDeseas agregar mas lugar a tu simulacion: si (1), no (0)" );
  578. selection = Integer.parseInt(in.readLine());
  579. statement = "select * from lugaravisitar where ciudad=\"" + ciudad + "\" and pais=\"" + pais + "\"";
  580. if (test.equals("")){
  581. System.out.println("Query failed try again");
  582. break;
  583. }
  584. query( statement );
  585. System.out.println( "\nSelecciona un lugar a visitar para añadir" );
  586. lugar = in.readLine();
  587. statement = "insert into relacion values ( "+ today+name +", '"+ lugar +"', '" + ciudad + "', '" + pais + "')";
  588. stmt.executeUpdate( statement );
  589. }
  590. }
  591.  
  592. statement = "select precio from hotel where ciudad=\"" + ciudad + "\" and pais=\"" + pais + "\" and nombre=\"" + hotel + "\"";
  593. int precioHotel = Integer.parseInte(simpleQuery( statement ));
  594.  
  595. statement = "select preciodesayuno from hotel where ciudad=\"" + ciudad + "\" and pais=\"" + pais + "\" and nombre=\"" + hotel + "\"";
  596. int precioDesayunoHotel = Integer.parseInte(simpleQuery( statement ));
  597.  
  598. statement = "select sum(lugaravisitar.precio) from lugaravisitar, relacion where relacion.nombre=lugaravisitar.nombre and relacion.ciudad=lugaravisitar.ciudad and relacion.pais=lugaravisitar.pais and relacion.id=\"" + today+name + "\"";
  599. int precioLugares = Integer.parseInte(simpleQuery( statement ));
  600.  
  601.  
  602. statement = "select circuito.precio from circuito, relacion2, fechacircuito where relacion2.idcircuito=circuito.id and fechacircuito.idcircuito=circuito.id and relacion2.fechasalida=fechacircuito.fechasalida and relacion2.id=\"" + today+name + "\"";
  603. int precioCircuito = Integer.parseInteger(simpleQuery( statement ));
  604.  
  605. int precio = precioHotel + precioDesayunoHotel + precioLugares + precioCircuito;
  606. statement = "insert into simulacion values ( "+ today+name +", '"+ nombre +"', '"+ fechaSalida +"', '"+ fechaRegreso +"', " + numero + ", " + precio + ")";
  607. stmt.executeUpdate( statement );
  608.  
  609. System.out.println("Su reservacion es: " + today+name);
  610. break;
  611.  
  612. case 7: conn.commit(); // fin de la transacción e inicio de la siguiente
  613. break;
  614.  
  615. case 8: conn.rollback(); // fin de la transacción e inicio de la siguiente
  616. break;
  617.  
  618.  
  619. case 9: System.out.println( "\nIngrese su ID" );
  620. String id = in.readLine();
  621. statement = "select * from simulacion where id=\"" + id + "\"";
  622. if (test.equals("")){
  623. System.out.println("Query failed try again");
  624. break;
  625. }
  626. query( statement );
  627. statement = "select * from relacion where id=\"" + id + "\"";
  628. query( statement );
  629. statement = "select * from relacion2 where id=\"" + id + "\"";
  630. query( statement );
  631. break;
  632.  
  633. case 10: return false;
  634. }
  635.  
  636. return true;
  637. }
  638.  
  639.  
  640.  
  641.  
  642. public static void main( String arg[] ) throws SQLException, Exception {
  643.  
  644. if( arg.length != 0 ) {
  645.  
  646. System.err.println( "Use: java TransactionMySQL" );
  647. System.exit( 1 );
  648. }
  649.  
  650. TransactionMySQL transaction = new TransactionMySQL();
  651.  
  652. while( true )
  653.  
  654. try {
  655. if( ! transaction.menu() )
  656. break;
  657.  
  658. } catch( Exception e ) {
  659.  
  660. System.err.println( "failed" );
  661. e.printStackTrace( System.err );
  662. }
  663.  
  664. transaction.close();
  665. }
  666. }
  667.  
  668.  
  669.  
  670. ///manejar errores
  671. ///nombres tablas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement