Commit b7af9ff1 by Patryk Czarnik

WypiszKraje

parent 53fbe75e
package zajecia.postgresql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class WypiszKraje {
public static void main(String[] args) {
// W bazie hr znajduje się tabela countries z kolumnami country_id i country_name
// wypisz wszystkie kraje, obie kolumny
// jeśli umiesz, posortuj alfabetycznie
try(Connection c = DriverManager.getConnection("jdbc:postgresql://localhost/hr", "kurs", "abc123");
PreparedStatement stmt = c.prepareStatement("SELECT * FROM countries ORDER BY country_name");
ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
String countryId = rs.getString("country_id");
String countryName = rs.getString("country_name");
System.out.println(countryId + " " + countryName);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment