Skip to content

Instantly share code, notes, and snippets.

@Ginxo
Created May 27, 2022 15:59
Show Gist options
  • Save Ginxo/40d0325538dce2dfb8cb235f78f046b7 to your computer and use it in GitHub Desktop.
Save Ginxo/40d0325538dce2dfb8cb235f78f046b7 to your computer and use it in GitHub Desktop.
import java.sql.*;
public class EjercicioJDBC2 {
public static void main(String... args) throws SQLException {
final String url = "jdbc:mysql://localhost:3306/base";
final String user = "root";
final String password = "1234";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(String.format("Already Connected to %s", url));
Statement statement = connection.createStatement();
System.out.println("Todas las personas creadas entre el 2014-12-15 y el 2014-12-20");
printPeople(statement.executeQuery("SELECT * FROM people WHERE created_date BETWEEN '2014-12-15' AND '2014-12-20';"));
System.out.println("-----------------------------------------");
System.out.println("Todas las personas que viven en un planeta que empiece por “D” y ordénalas por nombre\n");
printPeople(statement
.executeQuery("SELECT pe.* FROM people pe, planet pl WHERE pe.planet_id = pl.id AND pl.name LIKE 'D%';"));
ResultSet resultSet = statement.executeQuery(
"SELECT count(*) as total FROM people pe, planet pl WHERE pe.planet_id = pl.id AND pl.name = \"Tatooine\";\n");
resultSet.next();
System.out.println("-----------------------------------------");
System.out.println(String.format("Hay %d personas en Tatooine", resultSet.getInt(1)));
connection.close();
}
private static void printPeople(ResultSet resultSet) throws SQLException {
while (resultSet.next()) {
System.out.println(
String.format("Nombre: %s, Año: %s"
, resultSet.getString("name")
, resultSet.getString("birth_year")));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment