Working with Statements
CityBook Libraries wants to query their books table. To execute SQL queries in JDBC, you need to create a Statement object from your connection, then use it to run queries and get results.
The database credentials are preloaded in the Credentials class, and all necessary imports (Connection, DriverManager, SQLException, Statement, ResultSet) are available.
Diese Übung ist Teil des Kurses
Querying a PostgreSQL Database in Java
Anleitung zur Übung
- Create a
Statementfrom the connection. - Execute the query using the
Statementto get aResultSet.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
public class Main {
public static void main(String[] args) throws SQLException {
// Create statement and execute query
try (Connection conn = DriverManager.getConnection(Credentials.URL, Credentials.USER, Credentials.PASSWORD);
Statement stmt = conn.____();
ResultSet rs = ____.____("SELECT * FROM books")) {
System.out.println("Query Executed!");
}
}
}