Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 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 escola;
  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.util.Scanner;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18.  *
  19.  * @author 1189436
  20.  */
  21. public class Escola {
  22.  
  23.     /**
  24.      * @param args the command line arguments
  25.      */
  26.     public static int menu() {
  27.         Scanner in = new Scanner(System.in);
  28.  
  29.         System.out.println("Digite a opção:");
  30.         System.out.println("1 - Cadastrar aluno\n2 - Listar alunos\n0 - Sair\n");
  31.         System.out.print("Opção: ");
  32.         return in.nextInt();
  33.     }
  34.  
  35.     public static void cadastrarAluno(Connection con) {
  36.         String sql = "insert into Alunos values (?, ?, ?, ?, ?)";
  37.         Scanner in = new Scanner(System.in);
  38.  
  39.         try {
  40.             PreparedStatement ps = con.prepareStatement(sql);
  41.             System.out.print("CPF: ");
  42.             ps.setString(1, in.nextLine());
  43.  
  44.             System.out.print("Nome: ");
  45.             ps.setString(2, in.nextLine());
  46.  
  47.             System.out.print("Matricula: ");
  48.             ps.setString(3, in.nextLine());
  49.  
  50.             System.out.print("Idade: ");
  51.             ps.setInt(4, in.nextInt());
  52.  
  53.             System.out.print("Sexo: ");
  54.             String sexo = in.next();
  55.  
  56.             if ("m".equals(sexo) || "M".equals(sexo) || "masculino".equals(sexo)) {
  57.                 ps.setBoolean(5, true);
  58.             } else {
  59.                 ps.setBoolean(5, false);
  60.             }
  61.  
  62.             ps.executeUpdate();
  63.         } catch (SQLException ex) {
  64.             Logger.getLogger(Escola.class.getName()).log(Level.SEVERE, null, ex);
  65.         }
  66.     }
  67.  
  68.     public static void listarAlunos(Connection con) {
  69.         String sql = "select * from Alunos";
  70.        
  71.         try {
  72.             ResultSet rs = con.createStatement().executeQuery(sql);
  73.            
  74.             while (rs.next()) {
  75.                 System.out.print(rs.getString("nome"));
  76.                
  77.                 if (rs.getBoolean("sexo")) {
  78.                     System.out.println(" eh um homem");
  79.                 } else {
  80.                     System.out.println(" eh uma mulher");
  81.                 }
  82.             }
  83.         } catch (SQLException ex) {
  84.             Logger.getLogger(Escola.class.getName()).log(Level.SEVERE, null, ex);
  85.         }
  86.  
  87.     }
  88.    
  89.     public static void main(String[] args) {
  90.         try {
  91.             Class.forName("com.mysql.jdbc.Driver");
  92.  
  93.             String url = "jdbc:mysql://localhost/Escola";
  94.             String user = "root";
  95.             String pass = "macau@2015";
  96.  
  97.             Connection con = DriverManager.getConnection(url, user, pass);
  98.  
  99.             int opcao = menu();
  100.  
  101.             if (opcao == 1) {
  102.                 cadastrarAluno(con);
  103.             } else if (opcao == 2) {
  104.                 listarAlunos(con);
  105.             } else if (opcao == 0) {
  106.                 System.exit(0);
  107.             }
  108.         } catch (ClassNotFoundException ex) {
  109.             Logger.getLogger(Escola.class.getName()).log(Level.SEVERE, null, ex);
  110.         } catch (SQLException ex) {
  111.             Logger.getLogger(Escola.class.getName()).log(Level.SEVERE, null, ex);
  112.         }
  113.     }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement