Example usage for javax.sql RowSet getMetaData

List of usage examples for javax.sql RowSet getMetaData

Introduction

In this page you can find the example usage for javax.sql RowSet getMetaData.

Prototype

ResultSetMetaData getMetaData() throws SQLException;

Source Link

Document

Retrieves the number, types and properties of this ResultSet object's columns.

Usage

From source file:ViewDB.java

 /**
 * Constructs the data panel./*from  w ww.  j  av  a  2  s .  c  om*/
 * @param rs the result set whose contents this panel displays
 */
public DataPanel(RowSet rs) throws SQLException
{
   fields = new ArrayList<JTextField>();
   setLayout(new GridBagLayout());
   GridBagConstraints gbc = new GridBagConstraints();
   gbc.gridwidth = 1;
   gbc.gridheight = 1;

   ResultSetMetaData rsmd = rs.getMetaData();
   for (int i = 1; i <= rsmd.getColumnCount(); i++)
   {
      gbc.gridy = i - 1;

      String columnName = rsmd.getColumnLabel(i);
      gbc.gridx = 0;
      gbc.anchor = GridBagConstraints.EAST;
      add(new JLabel(columnName), gbc);

      int columnWidth = rsmd.getColumnDisplaySize(i);
      JTextField tb = new JTextField(columnWidth);
      if (!rsmd.getColumnClassName(i).equals("java.lang.String"))
         tb.setEditable(false);
               
      fields.add(tb);

      gbc.gridx = 1;
      gbc.anchor = GridBagConstraints.WEST;
      add(tb, gbc);
   }
}

From source file:org.aludratest.cloud.web.report.SqlBean.java

public synchronized void execute() {
    resultColumns = null;//  www  .j  av  a  2  s. com
    resultRows = null;

    if (sql == null) {
        return;
    }

    sql = sql.trim();
    while (sql.endsWith(";")) {
        sql = sql.substring(0, sql.length() - 1);
    }

    // easy basic check
    if (!sql.toLowerCase(Locale.US).startsWith("select ")) {
        FacesContext.getCurrentInstance().addMessage(null,
                JSFUtil.createErrorMessage("Only SELECT statements are allowed"));
        return;
    }

    try {
        RowSet rs = CloudManagerApplicationHolder.getInstance().getDatabase().populateQuery(sql);

        resultColumns = new ArrayList<String>();
        ResultSetMetaData meta = rs.getMetaData();
        for (int i = 1; i <= meta.getColumnCount(); i++) {
            resultColumns.add(meta.getColumnName(i));
        }

        rs.beforeFirst();
        resultRows = new ArrayList<Map<String, String>>();

        while (rs.next()) {
            Map<String, String> row = new HashMap<String, String>();

            // TODO nicer formatting etc.
            for (String col : resultColumns) {
                row.put(col, rs.getString(col));
            }

            resultRows.add(row);
        }
    } catch (SQLException se) {
        FacesContext.getCurrentInstance().addMessage(null,
                JSFUtil.createErrorMessage("SQLException: " + se.getMessage()));
    }
}