Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class Principal {
- private static Scanner teclado = new Scanner(System.in);
- private static Principal conexion = new Principal();
- private static Connection connect = conexion.conectar();
- public Connection conectar() {
- Connection conexion = null;
- try {
- conexion = DriverManager.getConnection(
- "jdbc:mysql://127.0.0.1:3306/m_Prog_Ejer1?useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC",
- "m", "1234");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conexion;
- }
- public static void main(String[] args) throws SQLException {
- Principal conexion = new Principal();
- conexion.listarMenu();
- }
- public void listarMenu() throws SQLException {
- int opcion = 0;
- boolean salir = false;
- do {
- do {
- System.out.println("** Elige un opcion **");
- System.out.println("1. Anyadir persona");
- System.out.println("2. Eliminar persona");
- System.out.println("3. Listar personas");
- System.out.println("4. Salir");
- while (!teclado.hasNextInt()) {
- teclado.next();
- System.out.println("Debe ser entero");
- }
- opcion = teclado.nextInt();
- } while (opcion < 1 || opcion > 4);
- switch (opcion) {
- case 1:
- insertarPersonas();
- break;
- case 2:
- eliminarPersonas();
- break;
- case 3:
- listarPersonas();
- break;
- case 4:
- salir = true;
- break;
- default:
- }
- } while (salir == false);
- }
- public void insertarPersonas() throws SQLException {
- Statement stmt = connect.createStatement();
- String nombre;
- String apellido;
- System.out.println("Indicame el nombre:");
- nombre = teclado.next();
- System.out.println("Indicame el apellido");
- apellido = teclado.next();
- stmt.executeUpdate("insert into personas(nombre,apellidos) values ('" + nombre + "','" + apellido + "')");
- System.out.println("Filas afectadas " + stmt.getUpdateCount());
- }
- public void eliminarPersonas() throws SQLException {
- Statement stmt = connect.createStatement();
- String nombre;
- String apellido;
- System.out.println("Indicame el nombre:");
- nombre = teclado.next();
- System.out.println("Indicame el apellido");
- apellido = teclado.next();
- stmt.executeUpdate("delete from personas(nombre,apellidos) values ('" + nombre + "','" + apellido + "')");
- System.out.println("Filas afectadas " + stmt.getUpdateCount());
- }
- public void listarPersonas() throws SQLException {
- Statement stmt = connect.createStatement();
- ResultSet resultado = stmt.executeQuery("SELECT * FROM personas");
- while (resultado.next()) {
- System.out.println("Fila");
- System.out.println(resultado.getInt("id"));
- System.out.println(resultado.getString("nombre"));
- System.out.println(resultado.getString("apellidos"));
- }
- stmt.close();
- connect.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement