Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package dbapp;
  2. import java.sql.*; // importa a class
  3. import java.util.Scanner;
  4.  
  5. public class DBApp {
  6.  
  7.  
  8.     public static void main(String[] args)  throws SQLException {
  9.  
  10.         Connection conexao = DriverManager.getConnection(
  11.                 "jdbc:oracle:thin:@//camburi.pucrs.br:1521/facin11g",
  12.                 "bd104668", "bd104668");
  13.  
  14.         System.out.println("Conectado.");
  15.  
  16.         ResultSet rs;
  17.         PreparedStatement stmt;
  18.         String sql1 = "select palestra.titulo, p.nome, esc.NOMEESCOLA from palestra palestra inner join palestrante p on palestra.CODPALESTRANTE = p.CODPALESTRANTE inner join escola esc on palestra.CODESCOLA = esc.CODESCOLA";
  19.         String sql2 = "SELECT esc.nomeescola as Nome_Escola, COUNT(pa.codpalestra) as Quantidade_palestras FROM escola esc LEFT JOIN palestra pa ON esc.CODESCOLA = pa.CODESCOLA GROUP BY esc.NOMEESCOLA order by 2 desc";
  20.         String sql3 = "select nome, email from (select * from palestrante where idade >= 16)";
  21.         String sql4 = "delete from aluno where codaluno = any(select CODALUNO from aluno where CODALUNO not in (select CODALUNO from participacao))";
  22.         stmt = conexao.prepareStatement(sql1);
  23.         rs = stmt.executeQuery();
  24.        
  25.         while (rs.next()){
  26.             String titulo = rs.getString("titulo");
  27.             String nome = rs.getString("nome");
  28.             String escola = rs.getString("nomeescola");
  29.            
  30.             System.out.println("Titulo da palestra: " + titulo + " || Nome do Palestrante: " + nome + " || Nome da Escola: " + escola);
  31.         }
  32.        
  33.         System.out.println("---");
  34.        
  35.         stmt = conexao.prepareStatement(sql2);
  36.         rs = stmt.executeQuery();
  37.        
  38.         while (rs.next()){
  39.             String escola = rs.getString("nome_escola");
  40.             Integer quantidade = rs.getInt("quantidade_palestras");
  41.            
  42.             System.out.println("Nome da Escola: " + escola + " || Quantidade de palestras: " + quantidade);
  43.         }
  44.        
  45.         System.out.println("---");
  46.        
  47.         stmt = conexao.prepareStatement(sql3);
  48.         rs = stmt.executeQuery();
  49.        
  50.         while (rs.next()){
  51.             String nome = rs.getString("nome");
  52.             String email = rs.getString("email");
  53.            
  54.             if (email == null) {
  55.                 email = "não possui email";
  56.             }
  57.            
  58.             System.out.println("Nome: " + nome + " || Email: " + email);
  59.         }
  60.        
  61.         System.out.println("---");
  62.        
  63.         stmt = conexao.prepareStatement(sql4);
  64.         stmt.execute();
  65.        
  66.         System.out.println("Alunos que não participaram de palestras removidos.");
  67.        
  68.         stmt.close();
  69.         rs.close();
  70.         conexao.close();
  71.     }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement