Advertisement
Guest User

FK com 3 tabelas - by: Matheus Picioli

a guest
May 7th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5.  
  6.  
  7.  
  8. public class testeJava {
  9.  
  10.     public static void main(String[] args) {
  11.         String url = "jdbc:mysql://localhost/foreignkey";
  12.        
  13.         String precoProduto = null;
  14.         String nomeProduto = null;
  15.        
  16.         String dataVenda = null;
  17.         String destinoVenda = null;
  18.        
  19.         float precoTotal = 0;
  20.        
  21.         try{
  22.             Connection conexao = DriverManager.getConnection(url, "root", "vertrigo");
  23.             PreparedStatement pesquisa = conexao.prepareStatement("SELECT * FROM itemVenda");
  24.             ResultSet resultado = pesquisa.executeQuery();
  25.            
  26.             while(resultado.next()){
  27.                 String id = resultado.getString("idItemVenda");
  28.                 String quantidade = resultado.getString("quantidade");
  29.                 String codVenda = resultado.getString("codVenda");
  30.                 String codProduto = resultado.getString("codProduto");
  31.                
  32.                 PreparedStatement pesquisa2 = conexao.prepareStatement("SELECT * FROM produto WHERE idProduto = '"+codProduto+"'");
  33.                 ResultSet resultado2 = pesquisa2.executeQuery();
  34.                
  35.                 while(resultado2.next()){
  36.                     precoProduto = resultado2.getString("preco");
  37.                     nomeProduto = resultado2.getString("nome");
  38.                 }
  39.                
  40.                 PreparedStatement pesquisa3 = conexao.prepareStatement("SELECT * FROM venda WHERE idVenda = '"+codVenda+"'");
  41.                 ResultSet resultado3 = pesquisa3.executeQuery();
  42.                
  43.                 while(resultado3.next()){
  44.                     dataVenda = resultado3.getString("data");
  45.                     destinoVenda = resultado3.getString("destino");
  46.                 }
  47.                
  48.                 precoTotal = Float.parseFloat(precoProduto) * Float.parseFloat(quantidade);
  49.                
  50.                 System.out.println("---Detalhes sobre a venda---");
  51.                 System.out.println("\tNome do produto: " + nomeProduto + ", quantidade:" + quantidade);
  52.                 System.out.println("\tDestino da venda: " + destinoVenda + ", data da venda: " + dataVenda);
  53.                 System.out.println("\tPreço total: " + precoTotal);
  54.                
  55.             }
  56.         } catch(Exception e){
  57.             System.out.println("Erro: " + e);
  58.         }
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement