예제 #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));
  }
예제 #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)");
  }
예제 #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));
  }
예제 #4
0
  /**
   * Runs JDBC example.
   *
   * @param args Command line arguments.
   * @throws Exception In case of error.
   */
  public static void main(String[] args) throws Exception {
    Grid grid = G.start("examples/config/spring-cache.xml");

    Connection conn = null;

    try {
      // Populate cache with data.
      populate(grid.cache(CACHE_NAME));

      // Register JDBC driver.
      Class.forName("org.gridgain.jdbc.GridJdbcDriver");

      // Open JDBC connection.
      conn =
          DriverManager.getConnection("jdbc:gridgain://localhost/" + CACHE_NAME, configuration());

      X.println(">>>");

      // Query all persons.
      queryAllPersons(conn);

      X.println(">>>");

      // Query person older than 30 years.
      queryPersons(conn, 30);

      X.println(">>>");

      // Query persons working in GridGain.
      queryPersonsInOrganization(conn, "GridGain");

      X.println(">>>");
    } finally {
      // Close JDBC connection.
      if (conn != null) conn.close();

      G.stop(true);
    }
  }