Skip to content

Instantly share code, notes, and snippets.

@MarioCares
Created November 14, 2024 01:41
Show Gist options
  • Save MarioCares/0ca02668f45091180dce22dc974e927a to your computer and use it in GitHub Desktop.
Save MarioCares/0ca02668f45091180dce22dc974e927a to your computer and use it in GitHub Desktop.
Clase 13-11-2024 Fundamentos POO
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package appconsola;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
/**
*
* @author luk0s
*/
public class AppConsola {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Bienvenido! Escriba la opción para continuar:");
System.out.println("1) Menú Usuario");
System.out.println("2) Menú Carrera");
System.out.println("3) Asociar Usuario y Carrera");
menuPrincipal();
}
private static void menuPrincipal() {
Scanner sc = new Scanner(System.in);
Boolean keepRunning = true;
while (keepRunning) {
if (sc.hasNextInt()) {
int opt1 = sc.nextInt();
switch (opt1) {
case 1 -> {
menuUsuario();
keepRunning = false;
break;
}
case 2 -> {
keepRunning = false;
break;
}
case 3 -> {
keepRunning = false;
}
default -> {
System.out.println("La opciones válidas son: 1, 2, 3. Intente nuevamente");
sc.next();
}
}
} else {
System.out.println("Debe ser un número. Intente nuevamente");
sc.next();
}
}
}
private static void menuUsuario() {
limpiarConsola();
System.out.println("Menú Usuario:");
System.out.println("1) Listar Usuario");
System.out.println("2) Agregar Usuario");
System.out.println("3) Editar Usuario");
System.out.println("4) Eliminar");
Scanner sc = new Scanner(System.in);
Boolean keepRunning = true;
while (keepRunning) {
if (sc.hasNextInt()) {
int opt1 = sc.nextInt();
switch (opt1) {
case 1 ->
{
System.out.println("Listamos los usuarios desde BD");
conectarBd();
keepRunning = false;
}
case 2 ->
keepRunning = false;
case 3 ->
keepRunning = false;
case 4 ->
keepRunning = false;
default ->
System.out.println("La opciones válidas son: 1, 2, 3, 4. Intente nuevamente");
}
} else {
System.out.println("Debe ser un número. Intente nuevamente");
}
}
}
private static void limpiarConsola() {
for (int i = 0; i < 50; i++) {
System.out.println();
}
}
private static void conectarBd() {
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "UST";
// Conexión
try (Connection connection = DriverManager.getConnection(url, user, password);
Statement statement = connection.createStatement()) {
System.out.println("Conexión exitosa!");
// Consulta de prueba
String query = "SELECT * FROM personas";
ResultSet resultSet = statement.executeQuery(query);
// Procesar resultados
while (resultSet.next()) {
System.out.println("Columna ID: " + resultSet.getString(1));
System.out.println("Columna Rut: " + resultSet.getString(2));
System.out.println("Columna Edad: " + resultSet.getString(3));
}
} catch (SQLException e) {
System.out.println("Error al conectar con la base de datos.");
e.getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment