package capitulo08.bloque01.operaciones;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Select {
    public static void mostrarTabla(Connection conn, String tabla) throws SQLException {
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("select * from tutorialjavacoches." + tabla + ";");

        switch (tabla) {
            case "fabricante": {
                System.out.println("ID\t | CIF\t\t\t | Nombre");
                while (rs.next()) {
                    System.out.println(rs.getInt(1) + "\t | " + rs.getString(2) + "\t | " + rs.getString(3));
                }
            }
        }
    }

    public static int getNextId(Connection conn, String tabla) throws SQLException {
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("select max(id) from tutorialjavacoches." + tabla + ";");
        rs.next();
        return (rs.getInt(1) + 1);
    }

    public static boolean isIdValid(Connection conn, String tabla, int id) throws SQLException {
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("select id from tutorialjavacoches." + tabla + " where id=" + id + ";");
        if (rs.next()) {
            return rs.getInt(1) == id;
        } else {
            return false;
        }
    }


}
