Advertisement
Guest User

Versión PRO

a guest
Jan 12th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. /*
  2.  *
  3.  */
  4. package pruebajdbc;
  5.  
  6. import com.mysql.jdbc.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.Scanner;
  12.  
  13. /**
  14.  *
  15.  * @author jchierro
  16.  */
  17. public class PruebaJDBC {
  18.  
  19.     static Scanner teclado = new Scanner(System.in);
  20.     static Connection con;
  21.     static String sql = null;
  22.     static Statement sentencia;
  23.     static ResultSet rs;
  24.  
  25.     /**
  26.      * @param args the command line arguments
  27.      * @throws java.lang.ClassNotFoundException
  28.      * @throws java.sql.SQLException
  29.      */
  30.     public static void main(String[] args) throws ClassNotFoundException, SQLException {
  31.         int menu = 0;
  32.  
  33.         try {
  34.             Class.forName("com.mysql.jdbc.Driver");
  35.  
  36.             String url = "jdbc:mysql://localhost/Empresa";
  37.             con = (Connection) DriverManager.getConnection(url, "root", "");
  38.  
  39.             do {
  40.                 menu = menu();
  41.  
  42.                 switch (menu) {
  43.                     case 1:
  44.                         mostrarOficinas();
  45.                         break;
  46.                     case 2: {
  47.                         mostrarPrecio();
  48.                         break;
  49.                     }
  50.                 }
  51.             } while (menu != 3);
  52.  
  53.         } catch (ClassNotFoundException | SQLException ex) {
  54.             System.err.println("Error: " + ex);
  55.         }
  56.     }
  57.  
  58.     static int menu() {
  59.         System.out.println("Se ha establecido la conexión...");
  60.         System.out.println("1.Mostrar las oficinas por región: ");
  61.         System.out.println("2.Mostrar productos por precios: ");
  62.         System.out.println("3.Salir. ");
  63.  
  64.         return teclado.nextInt();
  65.     }
  66.  
  67.     static void mostrarOficinas() throws SQLException {
  68.         sql = "SELECT * FROM Oficinas order by region;";
  69.         sentencia = con.createStatement();
  70.         rs = sentencia.executeQuery(sql);
  71.         while (rs.next()) {
  72.             System.out.println(rs.getString("oficina")
  73.                     + "\t " + rs.getString("ciudad")
  74.                     + "\t " + rs.getString("region"));
  75.         }
  76.     }
  77.  
  78.     static void mostrarPrecio() throws SQLException {
  79.         System.out.println("Se ha establecido la conexión...");
  80.         System.out.println("Introduce la cantidad mínima: ");
  81.         int pMin = teclado.nextInt();
  82.         System.out.println("Introduce la cantidad máxima: ");
  83.         int pMax = teclado.nextInt();
  84.  
  85.         sql = "SELECT * FROM Productos where " + pMin + " < precio AND precio < " + pMax + ";";
  86.         sentencia = con.createStatement();
  87.         rs = sentencia.executeQuery(sql);
  88.         while (rs.next()) {
  89.             System.out.println(rs.getString("idfab")
  90.                     + "\t " + rs.getString("precio"));
  91.         }
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement