Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- */
- package pruebajdbc;
- import com.mysql.jdbc.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- /**
- *
- * @author jchierro
- */
- public class PruebaJDBC {
- static Scanner teclado = new Scanner(System.in);
- static Connection con;
- static String sql = null;
- static Statement sentencia;
- static ResultSet rs;
- /**
- * @param args the command line arguments
- * @throws java.lang.ClassNotFoundException
- * @throws java.sql.SQLException
- */
- public static void main(String[] args) throws ClassNotFoundException, SQLException {
- int menu = 0;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost/Empresa";
- con = (Connection) DriverManager.getConnection(url, "root", "");
- do {
- menu = menu();
- switch (menu) {
- case 1:
- mostrarOficinas();
- break;
- case 2: {
- mostrarPrecio();
- break;
- }
- }
- } while (menu != 3);
- } catch (ClassNotFoundException | SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- static int menu() {
- System.out.println("Se ha establecido la conexión...");
- System.out.println("1.Mostrar las oficinas por región: ");
- System.out.println("2.Mostrar productos por precios: ");
- System.out.println("3.Salir. ");
- return teclado.nextInt();
- }
- static void mostrarOficinas() throws SQLException {
- sql = "SELECT * FROM Oficinas order by region;";
- sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- while (rs.next()) {
- System.out.println(rs.getString("oficina")
- + "\t " + rs.getString("ciudad")
- + "\t " + rs.getString("region"));
- }
- }
- static void mostrarPrecio() throws SQLException {
- System.out.println("Se ha establecido la conexión...");
- System.out.println("Introduce la cantidad mínima: ");
- int pMin = teclado.nextInt();
- System.out.println("Introduce la cantidad máxima: ");
- int pMax = teclado.nextInt();
- sql = "SELECT * FROM Productos where " + pMin + " < precio AND precio < " + pMax + ";";
- sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- while (rs.next()) {
- System.out.println(rs.getString("idfab")
- + "\t " + rs.getString("precio"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement