Created
May 27, 2022 16:32
-
-
Save Ginxo/238ddc66d313c0fff15912c3db851236 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.sql.*; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
public class EjercicioJDBC3 { | |
public static void main(String... args) throws SQLException, IOException, ParseException { | |
final String url = "jdbc:mysql://127.0.0.1:3306/base"; | |
final String user = "root"; | |
final String password = "1234"; | |
Connection connection = DriverManager.getConnection(url, user, password); | |
System.out.println(String.format("[DEBUG] Already Connected to %s", url)); | |
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.println("Enter the person's starting date"); | |
final String startingDate = bufferedReader.readLine(); | |
System.out.println("Enter the person's ending date"); | |
final String endingDate = bufferedReader.readLine(); | |
PreparedStatement statement = connection.prepareStatement("SELECT * FROM people WHERE created_date BETWEEN ? AND ?;"); | |
statement.setString(1, startingDate); | |
statement.setString(2, endingDate); | |
System.out.println(String.format("Todas las personas creadas entre el %s y el %s", startingDate, endingDate)); | |
printPeople(statement.executeQuery()); | |
System.out.println("Enter the planet name"); | |
final String planetName = bufferedReader.readLine(); | |
System.out.println("-----------------------------------------"); | |
System.out.println(String.format("Todas las personas que viven en un planeta que empiece por '%s'", planetName)); | |
PreparedStatement statement2 = connection | |
.prepareStatement("SELECT pe.* FROM people pe, planet pl WHERE pe.planet_id = pl.id AND pl.name LIKE ?"); | |
statement2.setString(1, String.format("%s%%", planetName)); | |
printPeople(statement2.executeQuery()); | |
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