Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Main {
- public static void main(String[] args) {
- /*Profesor profe1 = new Profesor();
- profe1.setNombre("Dorita");
- Alumnos a1 = new Alumnos();
- a1.setNombre("toti");
- a1.setApellido("schmunck");
- a1.setEdad(20);
- a1.setProfe(profe1);
- profe1.getNAlumnos().add(a1);
- Alumnos a2 = new Alumnos();
- a2.setNombre("andres");
- a2.setApellido("cabrera");
- a2.setEdad(25);
- a2.setProfe(profe1);
- profe1.getNAlumnos().add(a2);
- GestorDeClases.altaDeProfesor(profe1);
- GestorDeClases.altaDeAlumnos(a1);
- GestorDeClases.altaDeAlumnos(a2);
- GestorDeClases.eliminarALumnoDeLaBase(1);
- GestorDeClases.eliminarProfesorDeLaBase(1);
- GestorDeClases.ModicarBaseDeDatos("Alejandro", 2);
- ArrayList<Profesor> profesoresRecuperados = GestorDeClases.recuperarProfesor();
- for (Profesor p : profesoresRecuperados){
- System.out.println("Nombre: " + p.getNombre());
- p.listarAlumnosDelProfesor();
- }
- GestorDeClases.listarProfesor(profe1);
- */
- ArrayList<Alumnos> alumnosRecuperados = GestorDeClases.recuperarAlumnos(2);
- GestorDeClases.generarCSV("CsVAlumnos.csv", alumnosRecuperados);
- }
- }
- import java.util.ArrayList;
- public class Profesor {
- private String nombre;
- private int id;
- private ArrayList<Alumnos> nAlumnos = new ArrayList<Alumnos>();
- public void listarAlumnosDelProfesor(){
- for (Alumnos aActual : nAlumnos){
- System.out.println(aActual);
- }
- }
- public ArrayList<Alumnos> getNAlumnos() {
- return nAlumnos;
- }
- public void setNAlumnos(ArrayList<Alumnos> alumnos) {
- nAlumnos = alumnos;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- }
- public class Alumnos {
- private String nombre;
- private String apellido;
- private int edad;
- private Profesor profe;
- public String toString (){
- return "Nombre :" + nombre + " Apellido: " + apellido + " edad: " + edad;
- }
- public Profesor getProfe() {
- return profe;
- }
- public void setProfe(Profesor profe) {
- this.profe = profe;
- }
- public String getApellido() {
- return apellido;
- }
- public void setApellido(String apellido) {
- this.apellido = apellido;
- }
- public int getEdad() {
- return edad;
- }
- public void setEdad(int edad) {
- this.edad = edad;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- }
- import java.io.FileWriter;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- public abstract class GestorDeClases {
- static final String CONTROLADOR = "com.mysql.jdbc.Driver";
- static final String URLDB = "jdbc:mysql://Home/Profesor - Alumno";
- public static void altaDeProfesor (Profesor p){
- String sqlInsert = "INSERT INTO Profesor (nombre) VALUES ('"+ p.getNombre() +"');";
- try {
- Class.forName(CONTROLADOR);
- Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
- Statement instruccion = conexion.createStatement();
- instruccion.execute(sqlInsert);
- } catch (ClassNotFoundException e) {
- System.out.println("Problemas con el Driver..!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("Problemas con la consulta de sql o con jdbc..!");
- e.printStackTrace();
- }
- }
- public static void altaDeAlumnos (Alumnos a){
- int idProfesor = 0;
- String sqlSelect = "SELECT *FROM Profesor WHERE nombre = '"+ a.getProfe().getNombre() +"';";
- try {
- Class.forName(CONTROLADOR);
- Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
- Statement instruccion = conexion.createStatement();
- ResultSet cjtoDeResultados = instruccion.executeQuery(sqlSelect);
- if (cjtoDeResultados.next()) idProfesor = cjtoDeResultados.getInt("id");
- String sqlInsert = "INSERT INTO Alumnos (nombre, apellido, edad, id_Profesor) VALUES ('"+ a.getNombre() +"', '"+ a.getApellido() +"', '"+ a.getEdad() +"', "+ idProfesor +");";
- instruccion.execute(sqlInsert);
- } catch (ClassNotFoundException e) {
- System.out.println("Problemas con el Driver..!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("Problemas con la consulta de sql o con jdbc..!");
- e.printStackTrace();
- }
- }
- public static void eliminarALumnoDeLaBase (int idProf){
- String sqlDelete = "DELETE FROM Alumnos WHERE id_Profesor = " + idProf + ";";
- try {
- Class.forName(CONTROLADOR);
- Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
- Statement instruccion = conexion.createStatement();
- instruccion.execute(sqlDelete);
- } catch (ClassNotFoundException e) {
- System.out.println("Problemas con el Driver..!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("Problemas con la consulta de sql o con jdbc..!");
- e.printStackTrace();
- }
- }
- public static void eliminarProfesorDeLaBase(int idP){
- String sqlDelete = "DELETE FROM Profesor WHERE id = "+ idP +";";
- try {
- Class.forName(CONTROLADOR);
- Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
- Statement instruccion = conexion.createStatement();
- instruccion.execute(sqlDelete);
- } catch (ClassNotFoundException e) {
- System.out.println("Problemas con el Driver..!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("Problemas con la consulata de sql o con jdbc..!");
- e.printStackTrace();
- }
- }
- public static void ModicarBaseDeDatos (String nombre, int id){
- String sqlUpdate = "UPDATE profesor SET nombre = '"+ nombre +"' WHERE id = "+ id +";";
- try {
- Class.forName(CONTROLADOR);
- Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
- Statement instruccion = conexion.createStatement();
- instruccion.execute(sqlUpdate);
- } catch (ClassNotFoundException e) {
- System.out.println("Problemas con el Driver..!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("Problemas con la consulta de sql o con jdbc..!");
- e.printStackTrace();
- }
- }
- public static ArrayList<Profesor> recuperarProfesor(){
- String sqlSelect = "SELECT *FROM Profesor";
- ArrayList<Profesor> nProfesores = null;
- try {
- Class.forName(CONTROLADOR);
- Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
- Statement instruccion = conexion.createStatement();
- ResultSet cjtoDeResultados = instruccion.executeQuery(sqlSelect);
- nProfesores = new ArrayList<Profesor>();
- Profesor p;
- while (cjtoDeResultados.next()){
- p = new Profesor();
- p.setNombre(cjtoDeResultados.getString("nombre"));
- ArrayList<Alumnos> alumnosRecuperados = GestorDeClases.recuperarAlumnos(cjtoDeResultados.getInt("id"));
- p.setNAlumnos(alumnosRecuperados);
- nProfesores.add(p);
- }
- } catch (ClassNotFoundException e) {
- System.out.println("Problemas con el driver..!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("Problemas con la consulta de sql o con jdbc..!");
- e.printStackTrace();
- }
- return nProfesores;
- }
- public static ArrayList<Alumnos> recuperarAlumnos(int idProfesor){
- String sqlSelect = "SELECT *FROM Alumnos WHERE id_Profesor = "+ idProfesor +";";
- ArrayList<Alumnos> nAlumnos = null;
- try {
- Class.forName(CONTROLADOR);
- Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
- Statement instruccion = conexion.createStatement();
- ResultSet cjtoDeResultados = instruccion.executeQuery(sqlSelect);
- nAlumnos = new ArrayList<Alumnos>();
- Alumnos a;
- while (cjtoDeResultados.next()){
- a = new Alumnos();
- a.setNombre(cjtoDeResultados.getString("nombre"));
- a.setApellido(cjtoDeResultados.getString("apellido"));
- a.setEdad(cjtoDeResultados.getInt("edad"));
- nAlumnos.add(a);
- }
- } catch (ClassNotFoundException e) {
- System.out.println("Problemas con el Driver..!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("Problemas con la consulta de sql o con jdbc..!");
- e.printStackTrace();
- }
- return nAlumnos;
- }
- public static void listarProfesor (Profesor p){
- String sqlSelect = "SELECT *FROM Profesor";
- try {
- Class.forName(CONTROLADOR);
- Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
- Statement instruccion = conexion.createStatement();
- ResultSet cjtoDeResultados = instruccion.executeQuery(sqlSelect);
- while (cjtoDeResultados.next()){
- System.out.println("El id del mapa es: " + cjtoDeResultados.getInt("id"));
- System.out.println("El nombre del profesor es: " + cjtoDeResultados.getString("nombre"));
- GestorDeClases.listarAlumnos(cjtoDeResultados.getInt("id"));
- }
- } catch (ClassNotFoundException e) {
- System.out.println("Problemas con el Driver..!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("Problemas con la consulta de sql o con jdbc..!");
- e.printStackTrace();
- }
- }
- public static void listarAlumnos(int id){
- String sqlSelect = "SELECT *FROM Alumnos WHERE id_Profesor = "+ id +";";
- try {
- Class.forName(CONTROLADOR);
- Connection conexion = DriverManager.getConnection(URLDB, "root", "132456");
- Statement instruccion = conexion.createStatement();
- ResultSet cjtoDeResultados = instruccion.executeQuery(sqlSelect);
- while (cjtoDeResultados.next()){
- System.out.println("Nombre: " + cjtoDeResultados.getString("nombre") + " Apellido: " + cjtoDeResultados.getString("apellido") + " edad: " + cjtoDeResultados.getInt("edad"))
- ;
- }
- } catch (ClassNotFoundException e) {
- System.out.println("Problemas con el Driver..!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("Problemas con la consulta sql o con jdbc..!");
- e.printStackTrace();
- }
- }
- public static void generarCSV (String sFileName, ArrayList<Alumnos> alumnos){
- FileWriter writer;
- try {
- writer = new FileWriter(sFileName);
- writer.append("Nombre");
- writer.append(',');
- writer.append("Apellido");
- writer.append(',');
- writer.append("Edad");
- writer.append('\n');
- for (Alumnos aActual : alumnos){
- writer.append("" + aActual.getNombre());
- writer.append(',');
- writer.append("" + aActual.getApellido());
- writer.append(',');
- writer.append("" + aActual.getEdad());
- writer.append('\n');
- }
- writer.flush();
- writer.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement