Crear una base de datos e introducir datos sobre algunas de las casas principales de juego de tronos.
public class Westeros {
public static void main(String[] args) {
String url = "jdbc:sqlite:C:/sqlite/westeros.db";
SQLiteDataSource dataSource = new SQLiteDataSource();
dataSource.setUrl(url);
try (Connection con = dataSource.getConnection()) {
// Creacion de Statement
try (Statement statement = con.createStatement()) {
// ejecucion de Statement
statement.executeUpdate("CREATE TABLE IF NOT EXISTS(" +
"id INTEGER PRIMARY KEY," +
"name TEXT NOT NULL," +
"words TEXT NOT NULL)");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
int i = statement.executeUpdate("INSERT INTO HOUSES VALUES " +
"(1, 'Targaryen of King''s Landing', 'Fire and Blood')," +
"(2, 'Stark of Winterfell', 'Summer is Coming')," +
"(3, 'Lannister of Casterly Rock', 'Hear Me Roar!')");
int u = statement.executeUpdate("UPDATE HOUSES " +
"SET words = 'Winter is coming' " +
"WHERE id = 2");
try (ResultSet greatHouses = statement.executeQuery("SELECT * FROM HOUSES")) {
while (greatHouses.next()) {
// recuperar los valores de la columnas
int id = greatHouses.getInt("id");
String name = greatHouses.getString("name");
String words = greatHouses.getString("words");
System.out.printf("House %d%n", id);
System.out.printf("\tName: %s%n", name);
System.out.printf("\tWords: %s%n", words);
}
}
}
}