Presentation is loading. Please wait.

Presentation is loading. Please wait.

2.4 Bases de Dados Estudo de Caso. Caso: Caixa Eletrônico Caixa Eletrônico com acesso à Base de Dados; Cada cliente possui:  Um número de cliente  Uma.

Similar presentations


Presentation on theme: "2.4 Bases de Dados Estudo de Caso. Caso: Caixa Eletrônico Caixa Eletrônico com acesso à Base de Dados; Cada cliente possui:  Um número de cliente  Uma."— Presentation transcript:

1 2.4 Bases de Dados Estudo de Caso

2 Caso: Caixa Eletrônico Caixa Eletrônico com acesso à Base de Dados; Cada cliente possui:  Um número de cliente  Uma senha  Uma conta corrente  Uma conta poupança Os dados serão armazenados em duas tabelas;

3 Tabelas do Caixa Eletrônico

4 Estudo de Caso A classe Banco precisa se conectar à base para encontrar os cliente existentes; Criar um método EncontraCliente  Se conecta a base de dados  Seleciona um cliente apartir de um número  Verifica a senha do cliente  Cria um objeto cliente apartir dos dados vindos do banco; SELECT * FROM BankCustomer WHERE Customer_Number =...

5 Estudo de Caso A classe ContaBancaria terá seus métodos alterados; O método getSaldo deve pegar o saldo da base de dados; Os métodos saque e deposito devem atualizar a base de dados;

6 Case Study: A Bank Database public double getBalance() throws SQLException { Connection conn = SimpleDataSource.getConnection(); try { double balance = 0 PreparedStatement stat = conn.prepareStatement( "SELECT Balance FROM Account WHERE Account_Number = ?"); stat.setInt(1, accountNumber); ResultSet result = stat.executeQuery(); if (result.next()) balance = result.getDouble(1); return balance; } finally { conn.close(); }

7 Case Study: A Bank Database public void deposit(double amount) throws SQLException { Connection conn = SimpleDataSource.getConnection(); try { PreparedStatement stat = conn.prepareStatement( "UPDATE Account" + " SET Balance = Balance + ?" + " WHERE Account_Number = ?"); stat.setDouble(1, amount); stat.setInt(2, accountNumber); stat.executeUpdate(); } finally { conn.close(); }

8 File Bank.java 01: import java.sql.Connection; 02: import java.sql.ResultSet; 03: import java.sql.PreparedStatement; 04: import java.sql.SQLException; 05: 06: /** 07: A bank consisting of multiple bank accounts. 08: */ 09: public class Bank 10: { 11: /** 12: Finds a customer with a given number and PIN. 13: @param customerNumber the customer number 14: @param pin the personal identification number 15: @return the matching customer, or null if none found 16: */ Continued

9 File Bank.java 17: public Customer findCustomer(int customerNumber, int pin) 18: throws SQLException 19: { 20: Connection conn = SimpleDataSource.getConnection(); 21: try 22: { 23: Customer c = null; 24: PreparedStatement stat = conn.prepareStatement( 25: "SELECT * FROM BankCustomer WHERE Customer_Number = ?"); 26: stat.setInt(1, customerNumber); 27: 28: ResultSet result = stat.executeQuery(); 29: if (result.next() && pin == result.getInt("PIN")) 30: c = new Customer(customerNumber, 31: result.getInt("Checking_Account_Number"), 32: result.getInt("Savings_Account_Number")); 33: return c; Continued

10 File Bank.java 34: } 35: finally 36: { 37: conn.close(); 38: } 39: } 40: } 41: 42:

11 File BankAccount.java 01: import java.sql.Connection; 02: import java.sql.ResultSet; 03: import java.sql.PreparedStatement; 04: import java.sql.SQLException; 05: 06: /** 07: A bank account has a balance that can be changed by 08: deposits and withdrawals. 09: */ 10: public class BankAccount 11: { 12: /** 13: Constructs a bank account with a given balance. 14: @param anAccountNumber the account number 15: */ 16: public BankAccount(int anAccountNumber) 17: { Continued

12 File BankAccount.java 18: accountNumber = anAccountNumber; 19: } 20: 21: /** 22: Deposits money into a bank account. 23: @param amount the amount to deposit 24: */ 25: public void deposit(double amount) 26: throws SQLException 27: { 28: Connection conn = SimpleDataSource.getConnection(); 29: try 30: { 31: PreparedStatement stat = conn.prepareStatement( 32: "UPDATE Account" 33: + " SET Balance = Balance + ?" 34: + " WHERE Account_Number = ?"); 35: stat.setDouble(1, amount); Continued

13 File BankAccount.java 36: stat.setInt(2, accountNumber); 37: stat.executeUpdate(); 38: } 39: finally 40: { 41: conn.close(); 42: } 43: } 44: 45: /** 46: Withdraws money from a bank account. 47: @param amount the amount to withdraw 48: */ 49: public void withdraw(double amount) 50: throws SQLException 51: { 52: Connection conn = SimpleDataSource.getConnection(); Continued

14 File BankAccount.java 53: try 54: { 55: PreparedStatement stat = conn.prepareStatement( 56: "UPDATE Account" 57: + " SET Balance = Balance - ?" 58: + " WHERE Account_Number = ?"); 59: stat.setDouble(1, amount); 60: stat.setInt(2, accountNumber); 61: stat.executeUpdate(); 62: } 63: finally 64: { 65: conn.close(); 66: } 67: } 68: Continued

15 File BankAccount.java 69: /** 70: Gets the balance of a bank account. 71: @return the account balance 72: */ 73: public double getBalance() 74: throws SQLException 75: { 76: Connection conn = SimpleDataSource.getConnection(); 77: try 78: { 79: double balance = 0; 80: PreparedStatement stat = conn.prepareStatement( 81: "SELECT Balance FROM Account WHERE Account_Number = ?"); 82: stat.setInt(1, accountNumber); 83: ResultSet result = stat.executeQuery(); 84: if (result.next()) 85: balance = result.getDouble(1); Continued

16 File BankAccount.java 86: return balance; 87: } 88: finally 89: { 90: conn.close(); 91: } 92: } 93: 94: private int accountNumber; 95: } 96:


Download ppt "2.4 Bases de Dados Estudo de Caso. Caso: Caixa Eletrônico Caixa Eletrônico com acesso à Base de Dados; Cada cliente possui:  Um número de cliente  Uma."

Similar presentations


Ads by Google