Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 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(“———Marcar Nova Consulta————”);
  57.  
  58.             System.out.println(“Insira o ID do Utente:);
  59.             utenteID = Ler.umInt();
  60.             System.out.println(“Insira o ID do Médico:);
  61.             medicoID = Ler.umInt();
  62.             System.out.println(“Insira o ID do Centro: (1;2-Centro de Saude) (3-Centro de Análises));
  63.             centroID = Ler.umInt();
  64.             System.out.println(“Insira as anotações necessárias:);
  65.             anot = Ler.next();
  66.            
  67.  
  68.                          rs = con.prepareStatement(
  69.                                 "INSERT INTO Consulta(Anotacao,MedicoID,UtenteID,CentroID)  values(anot,medicoID,utenteID,cdntroID)”);
  70.  
  71.                        rs.executeUpdate();
  72.                        rs.close();
  73.            
  74.             System.out.println(“Consulta agendada”);
  75.  
  76.  
  77.                    }
  78.                    continue;
  79.  
  80.                }
  81.            } while (input != 3);
  82.  
  83.        } catch (SQLException e) {
  84.            System.out.println("SQL Exception: " + e.toString());
  85.        } catch (ClassNotFoundException cE) {
  86.            System.out.println("Class Not Found Exception: " + cE.toString());
  87.        }
  88.  
  89.    }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement