Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.94 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Mapa mapa1 = new Mapa();
  7.         mapa1.setNombre("Brazil");
  8.        
  9.         Punto p1 = new Punto();
  10.         p1.setLn(200);
  11.         p1.setLt(150);
  12.         p1.setKmPuntoOrigen(300);
  13.         p1.setMapa(mapa1);
  14.         mapa1.getNPuntos().add(p1);
  15.        
  16.         Punto p2 = new Punto();    
  17.         p2.setLn(123);
  18.         p2.setLt(345);
  19.         p2.setKmPuntoOrigen(432);
  20.         p2.setMapa(mapa1);
  21.         mapa1.getNPuntos().add(p2);
  22.        
  23.         //GestorDeMapa.AltaMapa(mapa1);
  24.         //GestorDeMapa.insertarPunto(p1);
  25.         //GestorDeMapa.insertarPunto(p2);
  26.         //GestorDeMapa.eliminarPuntosDelMapa(400);
  27.         //GestorDeMapa.listarMapa(mapa1);
  28.         //GestorDeMapa.modificarBaseDeDatos("toti", 1);
  29.        
  30.         ArrayList<Mapa> mapasRecuperadosDeLaBase = GestorDeMapa.recuperarMapa();
  31.        
  32.         for (Mapa mActual : mapasRecuperadosDeLaBase){
  33.             System.out.println("Nombre del mapa: " + mActual.getNombre());
  34.             mActual.listarPuntosDelMapa();
  35.         }
  36.        
  37.     }
  38.    
  39.  
  40. }
  41.  
  42. import java.util.ArrayList;
  43.  
  44.  
  45. public class Mapa {
  46.     private String nombre;
  47.     private ArrayList<Punto> nPuntos = new ArrayList<Punto>();
  48.     private int id;
  49.  
  50.     public void listarPuntosDelMapa(){
  51.         for(Punto pActual : nPuntos){
  52.             System.out.println(pActual);
  53.         }
  54.     }
  55.    
  56.     public int getId() {
  57.         return id;
  58.     }
  59.  
  60.     public void setId(int id) {
  61.         this.id = id;
  62.     }
  63.  
  64.     public ArrayList<Punto> getNPuntos() {
  65.         return nPuntos;
  66.     }
  67.  
  68.     public void setNPuntos(ArrayList<Punto> puntos) {
  69.         nPuntos = puntos;
  70.     }
  71.  
  72.     public String getNombre() {
  73.         return nombre;
  74.     }
  75.  
  76.     public void setNombre(String nombre) {
  77.         this.nombre = nombre;
  78.     }
  79. }
  80.  
  81. public class Punto {
  82.     private double ln;
  83.     private double lt;
  84.     private double kmPuntoOrigen;
  85.     private Mapa mapa;
  86.    
  87.     public String toString(){
  88.         return "Ln: " + ln + " Lt: " + lt + " Kmpuntoorigen: " + kmPuntoOrigen;
  89.     }
  90.    
  91.     public Mapa getMapa() {
  92.         return mapa;
  93.     }
  94.     public void setMapa(Mapa mapa) {
  95.         this.mapa = mapa;
  96.     }
  97.     public double getKmPuntoOrigen() {
  98.         return kmPuntoOrigen;
  99.     }
  100.     public void setKmPuntoOrigen(double kmPuntoOrigen) {
  101.         this.kmPuntoOrigen = kmPuntoOrigen;
  102.     }
  103.     public double getLn() {
  104.         return ln;
  105.     }
  106.     public void setLn(double ln) {
  107.         this.ln = ln;
  108.     }
  109.     public double getLt() {
  110.         return lt;
  111.     }
  112.     public void setLt(double lt) {
  113.         this.lt = lt;
  114.     }
  115. }
  116.  
  117. import java.sql.Connection;
  118. import java.sql.DriverManager;
  119. import java.sql.ResultSet;
  120. import java.sql.SQLException;
  121. import java.sql.Statement;
  122. import java.util.ArrayList;
  123.  
  124. public abstract class GestorDeMapa {
  125.    
  126.     static final String CONTROLADOR = "com.mysql.jdbc.Driver";
  127.    
  128.     static final String URLDB = "jdbc:mysql://diinsoluciones.dontexist.com/Mapa - Punto";
  129.    
  130.     public static void AltaMapa (Mapa m){
  131.         String sqlInsert ="INSERT INTO mapa (Nombre) VALUES ('" + m.getNombre() + "')";
  132.         try {
  133.             Class.forName(CONTROLADOR);
  134.             Connection conexion = DriverManager.getConnection(URLDB, "totaje", "buscandosalidas3");
  135.             Statement instruccion = conexion.createStatement();
  136.             instruccion.execute(sqlInsert);
  137.         } catch (ClassNotFoundException e) {
  138.             System.out.println("ERROR: Problemas con el driver!, fijate abajo gilun...");
  139.             e.printStackTrace();
  140.         } catch (SQLException e) {
  141.             System.out.println("ERROR: Problemas con JDBC!");
  142.             e.printStackTrace();
  143.         }
  144.     }
  145.    
  146.     public static void insertarPunto (Punto p){
  147.         int idMapa = 0;
  148.         String sqlSelect = "SELECT id FROM mapa WHERE Nombre = '"+ p.getMapa().getNombre() +"';";
  149.         try {
  150.             Class.forName(CONTROLADOR);
  151.             Connection conexion = DriverManager.getConnection(URLDB, "totaje", "buscandosalidas3");
  152.             Statement instruccion = conexion.createStatement();
  153.             ResultSet r = instruccion.executeQuery(sqlSelect);
  154.             if (r.next())idMapa = r.getInt("id");
  155.             String sqlInsert = "INSERT INTO punto (Ln, Lt, Kmpuntoorigen, id_mapa) VALUES ("+p.getLn()+", "+p.getLt()+", "+p.getKmPuntoOrigen()+", "+idMapa+")";
  156.             //Statement instruccion1 = conexion.createStatement();
  157.             instruccion.execute(sqlInsert);
  158.         } catch (ClassNotFoundException e) {
  159.             System.out.println("Problema con el driver..!");
  160.             e.printStackTrace();
  161.         } catch (SQLException e) {
  162.             System.out.println("Problema con la consulta o con jdbc..!");
  163.             e.printStackTrace();
  164.         }
  165.     }
  166.    
  167.     public static void listarMapa (Mapa p){
  168.         String sqlSelect = "select *from mapa";
  169.         try {
  170.             Class.forName(CONTROLADOR);
  171.             Connection conexion = DriverManager.getConnection(URLDB, "totaje", "buscandosalidas3");
  172.             Statement instruccion = conexion.createStatement();
  173.             ResultSet r = instruccion.executeQuery(sqlSelect);
  174.             while (r.next()){
  175.                 System.out.println("El id del mapa es: " + r.getInt("id"));
  176.                 System.out.println("El nombre del mapa es: " + r.getString("Nombre"));
  177.                 GestorDeMapa.listarPuntosDelMapa(r.getInt("id"));
  178.             }
  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 o con JDBC..!");
  184.             e.printStackTrace();
  185.         }
  186.     }
  187.  
  188.     public static void listarPuntosDelMapa(int int1) {
  189.         String sqlSelect = "select *from punto where id_mapa = " + int1;
  190.         try {
  191.             Class.forName(CONTROLADOR);
  192.             Connection conexion = DriverManager.getConnection(URLDB, "totaje", "buscandosalidas3");
  193.             Statement instruccion = conexion.createStatement();
  194.             ResultSet r = instruccion.executeQuery(sqlSelect);
  195.             while (r.next()){
  196.                 System.out.println("Longitud: " + r.getDouble("Ln") + " Latitud:" + r.getDouble("Lt") + " KmPuntoOrigen: " + r.getDouble("Kmpuntoorigen"));
  197.             }
  198.             } catch (ClassNotFoundException e) {
  199.             System.out.println("Problemas con el driver..!");
  200.             e.printStackTrace();
  201.         } catch (SQLException e) {
  202.             System.out.println("Problemas con la consulta o con JDBC..!");
  203.             e.printStackTrace();
  204.         }
  205.     }
  206.    
  207.     public static void eliminarPuntosDelMapa (int km){
  208.         String sqlDelete = "DELETE FROM punto WHERE Kmpuntoorigen > " + km;
  209.         try {
  210.             Class.forName(CONTROLADOR);
  211.             Connection conexion = DriverManager.getConnection(URLDB, "totaje", "buscandosalidas3");
  212.             Statement instruccion = conexion.createStatement();
  213.             instruccion.execute(sqlDelete);
  214.         } catch (ClassNotFoundException e) {
  215.             // TODO Auto-generated catch block
  216.             e.printStackTrace();
  217.         } catch (SQLException e) {
  218.             // TODO Auto-generated catch block
  219.             e.printStackTrace();
  220.         }
  221.     }
  222.    
  223.     public static void modificarBaseDeDatos (String nombre, int idAModificar){
  224.         String sqlUpdate = "UPDATE mapa SET nombre = '"+ nombre +"'  WHERE id = "+idAModificar+";";
  225.         try {
  226.             Class.forName(CONTROLADOR);
  227.             Connection conexion = DriverManager.getConnection(URLDB, "totaje", "buscandosalidas3");
  228.             Statement instruccion = conexion.createStatement();
  229.             instruccion.execute(sqlUpdate);
  230.         } catch (ClassNotFoundException e) {
  231.             System.out.println("Problemas con el driver..!");
  232.             e.printStackTrace();
  233.         } catch (SQLException e) {
  234.             System.out.println("Problemas con la consulta de sql o con jdbc..!");
  235.             e.printStackTrace();
  236.         }
  237.     }
  238.    
  239.     public static ArrayList<Mapa> recuperarMapa (){
  240.         String sqlSelect = "SELECT * FROM mapa;";
  241.         ArrayList<Mapa> nMapa = null;
  242.         try {
  243.             Class.forName(CONTROLADOR);
  244.             Connection conexion = DriverManager.getConnection(URLDB, "totaje", "buscandosalidas3");
  245.             Statement instruccion = conexion.createStatement();
  246.             ResultSet cjtoResultados = instruccion.executeQuery(sqlSelect);
  247.             nMapa = new ArrayList<Mapa>();
  248.             Mapa m = null;
  249.             while (cjtoResultados.next()){
  250.                 m = new Mapa();
  251.                 m.setNombre(cjtoResultados.getString("nombre"));
  252.                 //busco los puntos del mapa actual
  253.                 ArrayList<Punto> puntosRecuperadosDeLaBase = GestorDeMapa.recuperarPuntos(cjtoResultados.getInt("id"));
  254.                 //al mapa actual le seteo los puntos
  255.                 m.setNPuntos(puntosRecuperadosDeLaBase);
  256.                 //lo agrego al array
  257.                 nMapa.add(m);
  258.             }
  259.         } catch (ClassNotFoundException e) {
  260.             System.out.println("Problemas con el Driver..!");
  261.             e.printStackTrace();
  262.         } catch (SQLException e) {
  263.             System.out.println("Problemas con la consulta sql o con jdbc");
  264.             e.printStackTrace();
  265.         }
  266.         return nMapa;
  267.     }
  268.    
  269.     public static ArrayList<Punto> recuperarPuntos(int idMapa){
  270.         String sqlSelect = "SELECT *FROM punto WHERE id_mapa = " + idMapa;
  271.         ArrayList<Punto> nPunto = null;
  272.         try {
  273.             Class.forName(CONTROLADOR);
  274.             Connection conexion = DriverManager.getConnection(URLDB, "totaje", "buscandosalidas3");
  275.             Statement instruccion = conexion.createStatement();
  276.             ResultSet cjtoResultados = instruccion.executeQuery(sqlSelect);
  277.             nPunto = new ArrayList<Punto>();
  278.             Punto p = null;
  279.             while (cjtoResultados.next()){
  280.                 p = new Punto();
  281.                 p.setLn(cjtoResultados.getDouble("Ln"));
  282.                 p.setLt(cjtoResultados.getDouble("Lt"));
  283.                 p.setKmPuntoOrigen(cjtoResultados.getDouble("Kmpuntoorigen"));
  284.                 nPunto.add(p);
  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 o con jdbc..!");
  291.             e.printStackTrace();
  292.         }
  293.         return nPunto;
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement