Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package consulta;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14.  
  15. /**
  16. *
  17. * @author syd
  18. */
  19. public class ConsultaV2 {
  20.  
  21. /**
  22. * @param args the command line arguments
  23. */
  24. public static void main(String[] args) {
  25. int input;
  26.  
  27. try {
  28. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  29. String connectionUrl = "jdbc:sqlserver://DESKTOP-NFDQP8C;databaseName=projectBD;user=sa;password=123qwe;";
  30.  
  31. Connection con = DriverManager.getConnection(connectionUrl);
  32.  
  33. do {
  34.  
  35. System.out.println("\n*** O que pretende fazer? ***\n1-Consultar dados das Consultas\n2-Marcar nova Consulta\n3-Sair");
  36. input = Ler.umInt();
  37. Statement stmt = null;
  38. ResultSet rs = null;
  39. switch (input) {
  40. case 1: {
  41. System.out.println("Insira ID do utente");
  42. int idUtente= Ler.umInt();
  43. String SQL = "Select *\n"
  44. + "From Consulta\n"
  45. +"Where UtenteID="+idUtente+"\n";
  46.  
  47. stmt = con.createStatement();
  48. rs = stmt.executeQuery(SQL);
  49. while (rs.next()) {
  50. System.out.println("ConsultaID: " + rs.getString(1) + " Anotação: " + rs.getString(2) + " MedicoID: " + rs.getString(3)+ " UtenteID: " + rs.getString(4)+ " CentroID: " + rs.getString(5) );
  51. }
  52.  
  53. }
  54. continue;
  55. case 2: {
  56. System.out.println("*** Insira o nº da Conta ***\n");
  57. int t = Ler.umInt();
  58.  
  59. String SQL = "Select MesaNum\n"
  60. + "From TalaoDeConferencia\n"
  61. + "where TCNum="+t;
  62. stmt = con.createStatement();
  63. rs = stmt.executeQuery(SQL);
  64. int mesa=0;
  65. int af=0;
  66. while (rs.next()) {
  67. mesa=Integer.valueOf(rs.getString(1));
  68.  
  69. }
  70. SQL = "Select AFNum\n"
  71. + "From TalaoDeConferencia\n"
  72. + "where TCNum="+t;
  73. stmt = con.createStatement();
  74. rs = stmt.executeQuery(SQL);
  75.  
  76.  
  77. while (rs.next()) {
  78. af=Integer.valueOf(rs.getString(1));
  79.  
  80. }
  81.  
  82. SQL = "Select SUM(custo) as Total\n"
  83. + "From (Select P.Custo * ATC.Quantidade as custo\n"
  84. + "From TalaoDeConferencia T, AtribuicaoProdTC ATC, Produto P\n"
  85. + "Where T.TCNum=ATC.TCNum and ATC.ProdutoNum=P.ProdutoNum and T.MesaNum=" + mesa + " and T.AFNum=" + af + " and T.Pago=0)asd";
  86. stmt = con.createStatement();
  87. rs = stmt.executeQuery(SQL);
  88. int v=0;
  89. while (rs.next()) {
  90. v=rs.getInt(1);
  91. }
  92.  
  93.  
  94.  
  95.  
  96. PreparedStatement ps = con.prepareStatement(
  97. "UPDATE TalaoDeConferencia SET Pago = 1 WHERE TCNum =" +t);
  98.  
  99. ps.executeUpdate();
  100. ps.close();
  101.  
  102.  
  103.  
  104. ps = con.prepareStatement(
  105. "INSERT INTO Fatura(TCNum,ValorTotal) values("+t+","+v+")");
  106.  
  107. ps.executeUpdate();
  108. ps.close();
  109.  
  110. }
  111. continue;
  112.  
  113. }
  114. } while (input != 3);
  115.  
  116. } catch (SQLException e) {
  117. System.out.println("SQL Exception: " + e.toString());
  118. } catch (ClassNotFoundException cE) {
  119. System.out.println("Class Not Found Exception: " + cE.toString());
  120. }
  121.  
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement