Advertisement
Guest User

Proveedor

a guest
Jun 2nd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.76 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Properties;
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Created by fjcambilr on 26/05/16.
  7.  */
  8. public class Proveidor {
  9.     private String user;
  10.     private String password;
  11.     private String dbClassName;
  12.     private String conection;
  13.     public Connection c;
  14.     Scanner userInput = new Scanner(System.in);
  15.  
  16.     public Proveidor(String user, String password, String dbClassName, String conection) throws SQLException, ClassNotFoundException {
  17.         this.user = user;
  18.         this.password = password;
  19.         this.dbClassName = dbClassName;
  20.         this.conection = conection;
  21.         c = ConexionBBDD();
  22.     }
  23.  
  24.     private Connection ConexionBBDD() throws ClassNotFoundException, SQLException {
  25.         // creates a drivermanager class factory
  26.         Class.forName(dbClassName);
  27.         // Properties for user and password. Here the user and password are both 'paulr'
  28.         Properties p = new Properties();
  29.         p.put("user", user);
  30.         p.put("password", password);
  31.         // Now try to connect
  32.         Connection c = DriverManager.getConnection(conection, p);
  33.         return c;
  34.     }
  35.     //Menu de Proveedores
  36.     public void Menu() {
  37.         System.out.println("MENU PROVEIDOR \n" +
  38.                 "1.- INSERTAR PROVEIDOR \n" +
  39.                 "2.- MÀXIM CODI PROVEIDOR \n" +
  40.                 "3.- CONSULTAR PROVEIDOR \n" +
  41.                 "4.- ELIMINAR PROVEIDOR \n" +
  42.                 "5.- MODIFICACION CLIENT PERSONA \n" +
  43.                 "6.- ATRAS \n" +
  44.                 "");
  45.     }
  46.  
  47.     public void Option(int opcion) throws SQLException, ClassNotFoundException {
  48.         switch (opcion) {
  49.             case 1:
  50.                 InsertarProveedor();
  51.                 break;
  52.             case 2:
  53.                 System.out.println("Codigo mas alto: " + maxCodiProveidor());
  54.                 break;
  55.             case 3:
  56.                 VisualizarProveedor();
  57.                 break;
  58.             case 4:
  59.                 System.out.println("Indique el cif del proveedor que desea eliminar: ");
  60.                 EliminarProveedor(userInput.next());
  61.                 break;
  62.             case 6:
  63.                 break;
  64.             default:
  65.                 System.out.println("No existe la accion");
  66.                 break;
  67.         }
  68.     }
  69.  
  70.     public void VisualizarProveedor() throws SQLException, ClassNotFoundException {
  71.         //suposam que el client és una PERSONA_CLI
  72.         String sql = "";
  73.         System.out.println("Desea filtrar la consulta(y or n): ");
  74.         switch (userInput.next()) {
  75.             case "y":
  76.                 sql = VisualtizarFiltrado();
  77.                 break;
  78.             case "n":
  79.                 sql = VisualitzarAll();
  80.                 break;
  81.         }
  82.         PreparedStatement preparedStatement = c.prepareStatement(sql);
  83.         ResultSet rs = preparedStatement.executeQuery();
  84.         ImprimirConuslta(rs);
  85.     }
  86.  
  87.     public String  VisualitzarAll() throws SQLException {
  88.         String sql = "SELECT * FROM PROVEIDOR WHERE activo='s'";
  89.         return sql;
  90.     }
  91.  
  92.     public String VisualtizarFiltrado(){
  93.         String sql = "SELECT * FROM PROVEIDOR WHERE ";
  94.         System.out.println("Introduce los campos separado por comas: ");
  95.         String cadena =userInput.next();
  96.         String[] campos = cadena.split(",");
  97.         System.out.println("Introduce los valores de busqueda separados por comas: ");
  98.         String cadena2 =userInput.next();
  99.         String[] valores = cadena2.split(",");
  100.         for (int i = 0; i < campos.length; i++) {
  101.             if(i == campos.length-1){
  102.                 sql += campos[i]+"="+valores[i];
  103.             }else {
  104.                 sql += campos[i] + "=" + valores[i] + "and";
  105.             }
  106.         }
  107.         System.out.println(sql);
  108.         return sql;
  109.     }
  110.  
  111.     public void ImprimirConuslta(ResultSet rs) throws SQLException {
  112.         while (rs.next()) {
  113.             int codi = rs.getInt("id_proveidor");
  114.             String nom = rs.getString("nom");
  115.             String telefon = rs.getString("telefon");
  116.             String cif = rs.getString("CIF");
  117.             //Obtener datos de localidad y de adreca
  118.             String[] datos = ObtenerDatosLocalidadyAdreca();
  119.             System.out.println(
  120.                     "id_proveidor: " + codi + "\n" +
  121.                             "nom: " + nom + "\n" +
  122.                             "adreca: "+datos[0]+"\n"+
  123.                             "localidad: "+datos[1]+"\n"+
  124.                             "numero: "+datos[2]+"\n"+
  125.                             "porta: "+datos[3]+"\n"+
  126.                             "pis: "+datos[4]+"\n"+
  127.                             "codiPostal: "+datos[5]+"\n"+
  128.                             "telefon: " + telefon + "\n" +
  129.                             "CiF: " + cif + "\n"
  130.             );
  131.         }
  132.  
  133.     }
  134.  
  135.     public void InsertarProveedor() throws SQLException {
  136.         //Insert que ira dntro de la tabla Proveedor
  137.         System.out.println("Introduce el nombre del proveedor");
  138.         String nomProveedor = userInput.nextLine();
  139.         System.out.println("Introduce el numero de telefono");
  140.         String telefono = userInput.next();
  141.         System.out.println("Introduce el cif");
  142.         String cif = userInput.next();
  143.  
  144.         int tipoVia = ObtenerIdTipusAdreca();
  145.         int id_adreca = ObtenerIdAdreca();
  146.  
  147.         userInput.nextLine();
  148.         System.out.println("Introduce el nombre de la calle: ");
  149.         String descripcion = userInput.nextLine();
  150.         System.out.println("Introduce el numero del portal: ");
  151.         String numerPortal = userInput.next();
  152.         System.out.println("Introduce la letra del portal si tiene: ");
  153.         String letraPortal = userInput.next();
  154.         System.out.println("Introduce el piso con su letra: ");
  155.         String pisoYletra = userInput.next();
  156.         System.out.println("Introduce el codigo postal: ");
  157.         String codigoPostal = userInput.next();
  158.  
  159.         int codiLoc = ObtenerIdLocalidad();
  160.  
  161.         InsertAdr(id_adreca,tipoVia,codiLoc,descripcion,numerPortal,letraPortal,pisoYletra,codigoPostal);
  162.         InsertPro(nomProveedor,id_adreca,telefono,cif);
  163.  
  164.     }
  165.  
  166.     private String[] ObtenerDatosLocalidadyAdreca() throws SQLException {
  167.         String[] datos = new String[6];
  168.         String sql = "SELECT a.descripcio as adrecaDescripcio,l.descripcio as locDescripcio,a.numero,a.porta,a.pis,a.codi_postal "+
  169.                 "FROM (ADRECA as a INNER JOIN LOCALITAT as l ON a.id_localitat = l.id_localitat) INNER JOIN PROVEIDOR as p on p.id_adreça = a.id_adreca";
  170.         PreparedStatement ps = c.prepareStatement(sql);
  171.         ResultSet rs2 = ps.executeQuery();
  172.         rs2.next();
  173.         datos[0] = rs2.getString("adrecaDescripcio");
  174.         datos[1] = rs2.getString("locDescripcio");
  175.         datos[2]= rs2.getString("numero");
  176.         datos[3] = rs2.getString("porta");
  177.         datos[4] = rs2.getString("pis");
  178.         datos[5] = rs2.getString("codi_postal");
  179.         return datos;
  180.     }
  181.  
  182.     private void InsertAdr(int id_adreca,int tipoVia,int codiLoc,String descripcion,String numerPortal,
  183.                            String letraPortal,String pisoYletra,String codigoPostal){
  184.         try {
  185.             //Hacemos el insert dentro de ADRECA.
  186.             String InsertAdreca = "INSERT INTO ADRECA VALUES(?,?,?,?,?,?,?,?)";
  187.             PreparedStatement inAdreca = c.prepareStatement(InsertAdreca);
  188.             inAdreca.setInt(1, id_adreca);
  189.             inAdreca.setInt(2, tipoVia);
  190.             inAdreca.setInt(3, codiLoc);
  191.             inAdreca.setString(4, descripcion);
  192.             inAdreca.setString(5, numerPortal);
  193.             inAdreca.setString(6, letraPortal);
  194.             inAdreca.setString(7,pisoYletra);
  195.             inAdreca.setString(8, codigoPostal);
  196.             inAdreca.execute();
  197.         }catch (Exception e){
  198.             System.out.println("ha haabido un error");
  199.         }
  200.     }
  201.  
  202.     private void InsertPro(String nomProveedor,int id_adreca,String telefono,String cif){
  203.         //Hacemos el insert dentro de ADRECA.
  204.         try {
  205.             String InsertProveedor = "INSERT INTO PROVEIDOR (nom,id_adreça,telefon,CIF,activo) VALUES(?,?,?,?,?)";
  206.             PreparedStatement inProveidor = c.prepareStatement(InsertProveedor);
  207.             inProveidor.setString(1, nomProveedor);
  208.             inProveidor.setInt(2, id_adreca);
  209.             inProveidor.setString(3, telefono);
  210.             inProveidor.setString(4, cif);
  211.             inProveidor.setString(5,"s");
  212.             inProveidor.execute();
  213.         }catch (Exception e ){
  214.  
  215.         }
  216.     }
  217.  
  218.     private int ObtenerIdLocalidad() throws SQLException {
  219.         //Obtenemos el idetificador de la localidad.
  220.         System.out.println("Introduce la localidad: ");
  221.         String nomLocaldad = userInput.next();
  222.         userInput.nextLine();
  223.         System.out.println(nomLocaldad+".");
  224.         String consultaidLocalida = "SELECT id_localitat FROM LOCALITAT WHERE descripcio=?";
  225.         PreparedStatement ps2 = c.prepareStatement(consultaidLocalida);
  226.         ps2.setString(1, nomLocaldad);
  227.         ResultSet rs2 = ps2.executeQuery();
  228.         rs2.next();
  229.         return rs2.getInt("id_localitat");
  230.  
  231.     }
  232.  
  233.     private int ObtenerIdAdreca() throws SQLException {
  234.         //Obtenemos los datos para insertar en la tabala ADRECA
  235.         String ConsultaMaxAdreca = "SELECT MAX(id_adreca)+1 as adreca FROM ADRECA";
  236.         PreparedStatement prs3 = c.prepareStatement(ConsultaMaxAdreca);
  237.         ResultSet rs3 = prs3.executeQuery();
  238.         rs3.next();
  239.         return rs3.getInt("adreca");
  240.     }
  241.  
  242.     private int ObtenerIdTipusAdreca() throws SQLException {
  243.         //Obtenemos identificador del tipo de adreca
  244.         String consutaTipoVia = "SELECT descripcio FROM TIPUS_ADRECA";
  245.         PreparedStatement ps = c.prepareStatement(consutaTipoVia);
  246.         ResultSet rs = ps.executeQuery();
  247.         int i = 1;
  248.         System.out.println("Selecciona un tipo de via: ");
  249.         while (rs.next()){
  250.             String descripcio = rs.getString("descripcio");
  251.             System.out.println(i+"."+descripcio);
  252.             i++;
  253.         }
  254.         return userInput.nextInt();
  255.     }
  256.  
  257.     public void EliminarProveedor(String cif) throws SQLException {
  258.         String sql = "UPDATE PROVEIDOR SET activo='n' WHERE cif=?";
  259.         PreparedStatement ps = c.prepareStatement(sql);
  260.         ps.setString(1,cif);
  261.         ps.execute();
  262.     }
  263.  
  264.     public int maxCodiProveidor() throws ClassNotFoundException, SQLException {
  265.         String sql = "select max(id_proveidor) as max from PROVEIDOR";
  266.         PreparedStatement preparedStatement = c.prepareStatement(sql);
  267.         // execute select SQL stetement
  268.         ResultSet rs = preparedStatement.executeQuery();
  269.         rs.next();
  270.         int max = rs.getInt("max");
  271.         c.close();
  272.         return max;
  273.  
  274.     }
  275.  
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement