开始使用免费开始使用

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.

本练习是课程的一部分

Querying a PostgreSQL Database in Java

查看课程

练习说明

  • Create a Statement from the connection.
  • Execute the query using the Statement to get a ResultSet.

交互式实操练习

通过完成这段示例代码来试试这个练习。

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!");
        }
    }
}
编辑并运行代码