Ejemplo n.º 1
0
  /**
   * Queries all persons and shows their names.
   *
   * @param conn JDBC connection.
   * @throws SQLException In case of SQL error.
   */
  private static void queryAllPersons(Connection conn) throws SQLException {
    Statement stmt = conn.createStatement();

    ResultSet rs = stmt.executeQuery("select name from Person");

    X.println(">>> All persons:");

    while (rs.next()) X.println(">>>     " + rs.getString(1));
  }
Ejemplo n.º 2
0
  /**
   * Queries persons older than provided age.
   *
   * @param conn JDBC connection.
   * @param minAge Minimum age.
   * @throws SQLException In case of SQL error.
   */
  private static void queryPersons(Connection conn, int minAge) throws SQLException {
    PreparedStatement stmt = conn.prepareStatement("select name, age from Person where age >= ?");

    stmt.setInt(1, minAge);

    ResultSet rs = stmt.executeQuery();

    X.println(">>> Persons older than " + minAge + ":");

    while (rs.next())
      X.println(">>>     " + rs.getString("NAME") + " (" + rs.getInt("AGE") + " years old)");
  }
Ejemplo n.º 3
0
  /**
   * Queries persons working in provided organization.
   *
   * @param conn JDBC
   * @param orgName Organization name.
   * @throws SQLException In case of SQL error.
   */
  private static void queryPersonsInOrganization(Connection conn, String orgName)
      throws SQLException {
    PreparedStatement stmt =
        conn.prepareStatement(
            "select p.name from Person p, Organization o where p.orgId = o.id and o.name = ?");

    stmt.setString(1, orgName);

    ResultSet rs = stmt.executeQuery();

    X.println(">>> Persons working in " + orgName + ":");

    while (rs.next()) X.println(">>>     " + rs.getString(1));
  }