Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.71 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         /*Profesor profe1 = new Profesor();
  9.         profe1.setNombre("Dorita");
  10.        
  11.         Alumnos a1 = new Alumnos();
  12.         a1.setNombre("toti");
  13.         a1.setApellido("schmunck");
  14.         a1.setEdad(20);
  15.         a1.setProfe(profe1);
  16.         profe1.getNAlumnos().add(a1);
  17.        
  18.         Alumnos a2 = new Alumnos();
  19.         a2.setNombre("andres");
  20.         a2.setApellido("cabrera");
  21.         a2.setEdad(25);
  22.         a2.setProfe(profe1);
  23.         profe1.getNAlumnos().add(a2);
  24.        
  25.         GestorDeClases.altaDeProfesor(profe1);
  26.         GestorDeClases.altaDeAlumnos(a1);
  27.         GestorDeClases.altaDeAlumnos(a2);
  28.        
  29.         GestorDeClases.eliminarALumnoDeLaBase(1);
  30.         GestorDeClases.eliminarProfesorDeLaBase(1);
  31.        
  32.         GestorDeClases.ModicarBaseDeDatos("Alejandro", 2);
  33.        
  34.         ArrayList<Profesor> profesoresRecuperados = GestorDeClases.recuperarProfesor();
  35.        
  36.         for (Profesor p : profesoresRecuperados){
  37.             System.out.println("Nombre: " + p.getNombre());
  38.             p.listarAlumnosDelProfesor();
  39.         }
  40.        
  41.        
  42.         GestorDeClases.listarProfesor(profe1);
  43.         */
  44.        
  45.         ArrayList<Alumnos> alumnosRecuperados = GestorDeClases.recuperarAlumnos(2);
  46.        
  47.         GestorDeClases.generarCSV("CsVAlumnos.csv", alumnosRecuperados);
  48.     }
  49. }
  50.  
  51. import java.util.ArrayList;
  52.  
  53.  
  54. public class Profesor {
  55.     private String nombre;
  56.     private int id;
  57.     private ArrayList<Alumnos> nAlumnos = new ArrayList<Alumnos>();
  58.    
  59.     public void listarAlumnosDelProfesor(){
  60.         for (Alumnos aActual : nAlumnos){
  61.             System.out.println(aActual);
  62.         }
  63.     }
  64.    
  65.     public ArrayList<Alumnos> getNAlumnos() {
  66.         return nAlumnos;
  67.     }
  68.     public void setNAlumnos(ArrayList<Alumnos> alumnos) {
  69.         nAlumnos = alumnos;
  70.     }
  71.     public int getId() {
  72.         return id;
  73.     }
  74.     public void setId(int id) {
  75.         this.id = id;
  76.     }
  77.     public String getNombre() {
  78.         return nombre;
  79.     }
  80.     public void setNombre(String nombre) {
  81.         this.nombre = nombre;
  82.     }
  83. }
  84.  
  85. public class Alumnos {
  86.     private String nombre;
  87.     private String apellido;
  88.     private int edad;
  89.     private Profesor profe;
  90.    
  91.     public String toString (){
  92.         return "Nombre :" + nombre + " Apellido: " + apellido + " edad: " + edad;
  93.     }
  94.    
  95.     public Profesor getProfe() {
  96.         return profe;
  97.     }
  98.     public void setProfe(Profesor profe) {
  99.         this.profe = profe;
  100.     }
  101.     public String getApellido() {
  102.         return apellido;
  103.     }
  104.     public void setApellido(String apellido) {
  105.         this.apellido = apellido;
  106.     }
  107.     public int getEdad() {
  108.         return edad;
  109.     }
  110.     public void setEdad(int edad) {
  111.         this.edad = edad;
  112.     }
  113.     public String getNombre() {
  114.         return nombre;
  115.     }
  116.     public void setNombre(String nombre) {
  117.         this.nombre = nombre;
  118.     }
  119. }
  120.  
  121. import java.io.FileWriter;
  122. import java.io.IOException;
  123. import java.sql.Connection;
  124. import java.sql.DriverManager;
  125. import java.sql.ResultSet;
  126. import java.sql.SQLException;
  127. import java.sql.Statement;
  128. import java.util.ArrayList;
  129.  
  130.  
  131. public abstract class GestorDeClases {
  132.     static final String CONTROLADOR = "com.mysql.jdbc.Driver";
  133.    
  134.     static final String URLDB = "jdbc:mysql://Home/Profesor - Alumno";
  135.    
  136.     public static void altaDeProfesor (Profesor p){
  137.         String sqlInsert = "INSERT INTO Profesor (nombre) VALUES ('"+ p.getNombre() +"');";
  138.         try {
  139.             Class.forName(CONTROLADOR);
  140.             Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
  141.             Statement instruccion = conexion.createStatement();
  142.             instruccion.execute(sqlInsert);
  143.         } catch (ClassNotFoundException e) {
  144.             System.out.println("Problemas con el Driver..!");
  145.             e.printStackTrace();
  146.         } catch (SQLException e) {
  147.             System.out.println("Problemas con la consulta de sql o con jdbc..!");
  148.             e.printStackTrace();
  149.         }
  150.     }
  151.    
  152.     public static void altaDeAlumnos (Alumnos a){
  153.         int idProfesor = 0;
  154.         String sqlSelect = "SELECT *FROM Profesor WHERE nombre = '"+ a.getProfe().getNombre() +"';";
  155.         try {
  156.             Class.forName(CONTROLADOR);
  157.             Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
  158.             Statement instruccion = conexion.createStatement();
  159.             ResultSet cjtoDeResultados = instruccion.executeQuery(sqlSelect);
  160.             if (cjtoDeResultados.next()) idProfesor = cjtoDeResultados.getInt("id");
  161.             String sqlInsert = "INSERT INTO Alumnos (nombre, apellido, edad, id_Profesor) VALUES ('"+ a.getNombre() +"', '"+ a.getApellido() +"', '"+ a.getEdad() +"', "+ idProfesor +");";
  162.             instruccion.execute(sqlInsert);
  163.         } catch (ClassNotFoundException e) {
  164.             System.out.println("Problemas con el Driver..!");
  165.             e.printStackTrace();
  166.         } catch (SQLException e) {
  167.             System.out.println("Problemas con la consulta de sql o con jdbc..!");
  168.             e.printStackTrace();
  169.         }
  170.     }
  171.    
  172.     public static void eliminarALumnoDeLaBase (int idProf){
  173.         String sqlDelete = "DELETE FROM Alumnos WHERE id_Profesor = " + idProf + ";";
  174.         try {
  175.             Class.forName(CONTROLADOR);
  176.             Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
  177.             Statement instruccion = conexion.createStatement();
  178.             instruccion.execute(sqlDelete);
  179.         } catch (ClassNotFoundException e) {
  180.             System.out.println("Problemas con el Driver..!");
  181.             e.printStackTrace();
  182.         } catch (SQLException e) {
  183.             System.out.println("Problemas con la consulta de sql o con jdbc..!");
  184.             e.printStackTrace();
  185.         }
  186.     }
  187.    
  188.     public static void eliminarProfesorDeLaBase(int idP){
  189.         String sqlDelete = "DELETE FROM Profesor WHERE id = "+ idP +";";
  190.         try {
  191.             Class.forName(CONTROLADOR);
  192.             Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
  193.             Statement instruccion = conexion.createStatement();
  194.             instruccion.execute(sqlDelete);
  195.         } catch (ClassNotFoundException e) {
  196.             System.out.println("Problemas con el Driver..!");
  197.             e.printStackTrace();
  198.         } catch (SQLException e) {
  199.             System.out.println("Problemas con la consulata de sql o con jdbc..!");
  200.             e.printStackTrace();
  201.         }
  202.     }
  203.    
  204.     public static void ModicarBaseDeDatos (String nombre, int id){
  205.         String sqlUpdate = "UPDATE profesor SET nombre = '"+ nombre +"' WHERE id = "+ id +";";
  206.         try {
  207.             Class.forName(CONTROLADOR);
  208.             Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
  209.             Statement instruccion = conexion.createStatement();
  210.             instruccion.execute(sqlUpdate);
  211.         } catch (ClassNotFoundException e) {
  212.             System.out.println("Problemas con el Driver..!");
  213.             e.printStackTrace();
  214.         } catch (SQLException e) {
  215.             System.out.println("Problemas con la consulta de sql o con jdbc..!");
  216.             e.printStackTrace();
  217.         }
  218.     }
  219.    
  220.     public static ArrayList<Profesor> recuperarProfesor(){
  221.         String sqlSelect = "SELECT *FROM Profesor";
  222.         ArrayList<Profesor> nProfesores = null;
  223.         try {
  224.             Class.forName(CONTROLADOR);
  225.             Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
  226.             Statement instruccion = conexion.createStatement();
  227.             ResultSet cjtoDeResultados = instruccion.executeQuery(sqlSelect);
  228.             nProfesores = new ArrayList<Profesor>();
  229.             Profesor p;
  230.             while (cjtoDeResultados.next()){
  231.                 p = new Profesor();
  232.                 p.setNombre(cjtoDeResultados.getString("nombre"));
  233.                 ArrayList<Alumnos> alumnosRecuperados = GestorDeClases.recuperarAlumnos(cjtoDeResultados.getInt("id"));
  234.                 p.setNAlumnos(alumnosRecuperados);
  235.                 nProfesores.add(p);
  236.             }
  237.         } catch (ClassNotFoundException e) {
  238.             System.out.println("Problemas con el driver..!");
  239.             e.printStackTrace();
  240.         } catch (SQLException e) {
  241.             System.out.println("Problemas con la consulta de sql o con jdbc..!");
  242.             e.printStackTrace();
  243.         }
  244.         return nProfesores;
  245.     }
  246.    
  247.     public static ArrayList<Alumnos> recuperarAlumnos(int idProfesor){
  248.         String sqlSelect = "SELECT *FROM Alumnos WHERE id_Profesor = "+ idProfesor +";";
  249.         ArrayList<Alumnos> nAlumnos = null;
  250.         try {
  251.             Class.forName(CONTROLADOR);
  252.             Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
  253.             Statement instruccion = conexion.createStatement();
  254.             ResultSet cjtoDeResultados = instruccion.executeQuery(sqlSelect);
  255.             nAlumnos = new ArrayList<Alumnos>();
  256.             Alumnos a;
  257.             while (cjtoDeResultados.next()){
  258.                 a = new Alumnos();
  259.                 a.setNombre(cjtoDeResultados.getString("nombre"));
  260.                 a.setApellido(cjtoDeResultados.getString("apellido"));
  261.                 a.setEdad(cjtoDeResultados.getInt("edad"));
  262.                 nAlumnos.add(a);
  263.             }
  264.         } catch (ClassNotFoundException e) {
  265.             System.out.println("Problemas con el Driver..!");
  266.             e.printStackTrace();
  267.         } catch (SQLException e) {
  268.             System.out.println("Problemas con la consulta de sql o con jdbc..!");
  269.             e.printStackTrace();
  270.         }
  271.         return nAlumnos;
  272.     }
  273.    
  274.     public static void listarProfesor (Profesor p){
  275.         String sqlSelect = "SELECT *FROM Profesor";
  276.         try {
  277.             Class.forName(CONTROLADOR);
  278.             Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
  279.             Statement instruccion = conexion.createStatement();
  280.             ResultSet cjtoDeResultados = instruccion.executeQuery(sqlSelect);
  281.             while (cjtoDeResultados.next()){
  282.                 System.out.println("El id del mapa es: " + cjtoDeResultados.getInt("id"));
  283.                 System.out.println("El nombre del profesor es: " + cjtoDeResultados.getString("nombre"));
  284.                 GestorDeClases.listarAlumnos(cjtoDeResultados.getInt("id"));
  285.             }
  286.         } catch (ClassNotFoundException e) {
  287.             System.out.println("Problemas con el Driver..!");
  288.             e.printStackTrace();
  289.         } catch (SQLException e) {
  290.             System.out.println("Problemas con la consulta de sql o con jdbc..!");
  291.             e.printStackTrace();
  292.         }
  293.     }
  294.    
  295.     public static void listarAlumnos(int id){
  296.         String sqlSelect = "SELECT *FROM Alumnos WHERE id_Profesor = "+ id +";";
  297.         try {
  298.             Class.forName(CONTROLADOR);
  299.             Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
  300.             Statement instruccion  = conexion.createStatement();
  301.             ResultSet cjtoDeResultados = instruccion.executeQuery(sqlSelect);
  302.             while (cjtoDeResultados.next()){
  303.                 System.out.println("Nombre: " + cjtoDeResultados.getString("nombre") + " Apellido: " + cjtoDeResultados.getString("apellido") + " edad: " + cjtoDeResultados.getInt("edad"))
  304.                 ;
  305.             }
  306.         } catch (ClassNotFoundException e) {
  307.             System.out.println("Problemas con el Driver..!");
  308.             e.printStackTrace();
  309.         } catch (SQLException e) {
  310.             System.out.println("Problemas con la consulta sql o con jdbc..!");
  311.             e.printStackTrace();
  312.         }
  313.     }
  314.    
  315.     public static void generarCSV (String sFileName, ArrayList<Alumnos> alumnos){
  316.         FileWriter writer;
  317.         try {
  318.             writer = new FileWriter(sFileName);
  319.             writer.append("Nombre");
  320.             writer.append(',');
  321.             writer.append("Apellido");
  322.             writer.append(',');
  323.             writer.append("Edad"); 
  324.             writer.append('\n');
  325.            
  326.             for (Alumnos aActual : alumnos){
  327.                 writer.append("" + aActual.getNombre());
  328.                 writer.append(',');
  329.                 writer.append("" + aActual.getApellido());
  330.                 writer.append(',');
  331.                 writer.append("" + aActual.getEdad());
  332.                 writer.append('\n');
  333.             }
  334.             writer.flush();
  335.             writer.close();
  336.         } catch (IOException e) {
  337.             // TODO Auto-generated catch block
  338.             e.printStackTrace();
  339.         }
  340.        
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement