コード例 #1
0
  @Override
  public int doEndTag() throws JspException {
    if (query == null && bodyContent != null) {
      query = bodyContent.getString();
    }
    if (query == null || "".equals(query)) {
      throw new JspException("Query must be provided, as an attribute or as BodyContent.");
    }

    final JspWriter out = pageContext.getOut();
    Connection conn = null;
    Statement statement = null;
    JspException exceptionToThrow = null;
    try {
      conn = getConnection();
      statement = conn.createStatement();
      resultSet = statement.executeQuery(query);
      SQLUtils.resultSetToHTML(resultSet, new PrintWriter(out), style1, style1, style2, pkey, link);

    } catch (SQLException e) {
      exceptionToThrow = new JspException("Database error", e);
    } finally {
      SQLUtils.cleanup(resultSet, statement, conn);
      if (exceptionToThrow != null) {
        throw exceptionToThrow;
      }
    }
    resetFields();
    return EVAL_PAGE;
  }
コード例 #2
0
  public static void main(String[] av) throws Throwable {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {

      System.out.println("Getting Connection");
      conn = ConnectionUtil.getConnection("ecom");

      // Turn off auto-commit
      conn.setAutoCommit(false);

      // Any warnings generated by the connect?
      checkForWarning(conn.getWarnings());

      System.out.println("Creating Statement");
      stmt = conn.createStatement();

      System.out.println("Creating table");
      stmt.executeUpdate("create table test (id int, name varchar)");

      System.out.println("Executing Insert");
      stmt.executeUpdate("insert into test values(42, 'ian');");

      System.out.println("Sleeping...");
      Thread.sleep(60 * 1000);

      System.out.println("** Committing **");
      conn.commit();

      System.out.println("Re-reading table");
      rs = stmt.executeQuery("select * from test");
      int i = 0;
      while (rs.next()) {

        System.out.println("Retrieving ID");
        int x = rs.getInt(1);
        System.out.println("Retrieving Name");
        String s = rs.getString(2);

        System.out.println("ROW " + ++i + ": " + x + "; " + s + "; " + ".");
      }

      System.out.println("Removing table");
      stmt.executeUpdate("drop table test");
    } finally {
      SQLUtils.cleanup(rs, stmt, conn);
    }
  }