Example usage for java.sql ResultSetMetaData getColumnClassName

List of usage examples for java.sql ResultSetMetaData getColumnClassName

Introduction

In this page you can find the example usage for java.sql ResultSetMetaData getColumnClassName.

Prototype

String getColumnClassName(int column) throws SQLException;

Source Link

Document

Returns the fully-qualified name of the Java class whose instances are manufactured if the method ResultSet.getObject is called to retrieve a value from the column.

Usage

From source file:org.opoo.oqs.core.mapper.SinglePropertyMapper.java

protected Type guessType(ResultSetMetaData rsmd, String name) {
    try {//from  w  ww  .  j a va  2s.  c om
        int count = rsmd.getColumnCount();
        for (int i = 1; i <= count; i++) {
            if (name.equals(rsmd.getColumnName(i))) {
                String className = rsmd.getColumnClassName(i);
                return TypeFactory.guessType(className);
            }
        }
    } catch (SQLException ex) {
        log.error("cannot guess type from result metadata column classname:" + name, ex);
    }
    return null;
}

From source file:cc.osint.graphd.db.SQLDB.java

private JSONObject jsonizeResultSet(ResultSet rs) throws Exception {
    List<JSONObject> results = new ArrayList<JSONObject>();
    ResultSetMetaData md = rs.getMetaData();
    int colmax = md.getColumnCount();
    int i;/*from   w w w  .  j  a  v a2  s . c om*/
    for (; rs.next();) {
        JSONObject result = new JSONObject();
        for (i = 1; i <= colmax; i++) {
            String colName = md.getColumnName(i).toLowerCase();
            String colClassName = md.getColumnClassName(i);
            String colType = md.getColumnTypeName(i);
            Object obj = rs.getObject(i);
            result.put(colName, obj);
            log.info(colName + ": " + colClassName + ": " + colType + ": " + obj.toString());
        }
        results.add(result);
    }
    JSONObject result = new JSONObject();
    result.put("results", results);
    return result;
}

From source file:gridool.util.csv.CsvWriter.java

public int writeAll(@Nonnull final ResultSet rs, @Nonnull final String nullStr, final boolean includeHeaders)
        throws SQLException {
    final ResultSetMetaData meta = rs.getMetaData();
    if (includeHeaders) {
        writeColumnNames(meta);//w w  w .j  a v a2s.  c o  m
    }
    final int numColumns = meta.getColumnCount();
    final String[] columnClasses = new String[numColumns + 1];
    for (int i = 1; i <= numColumns; i++) {
        String className = meta.getColumnClassName(i);
        columnClasses[i] = JAVA_STRING_CLASS_NAME.equals(className) ? JAVA_STRING_CLASS_NAME : className;
    }
    int numRows = 0;
    while (rs.next()) {
        for (int i = 1; i <= numColumns; i++) {
            if (i != 1) {
                write(separator);
            }
            final String column = rs.getString(i);
            if (column == null) {
                write(nullStr);
            } else if (JAVA_STRING_CLASS_NAME == columnClasses[i]) { // for speed optimization
                write(QUOTE);
                write(quoteData(column));
                write(QUOTE);
            } else {
                write(column);
            }
        }
        write(lineSeparator);
        numRows++;
    }
    flush();
    return numRows;
}

From source file:net.mlw.vlh.adapter.jdbc.dynabean.fix.JDBCDynaClass.java

/**
 * <p>Factory method to create a new DynaProperty for the given index
 * into the result set metadata.</p>
 * //  w  w  w  .  j ava 2 s.c  om
 * @param metadata is the result set metadata
 * @param i is the column index in the metadata
 * @return the newly created DynaProperty instance
 */
protected DynaProperty createDynaProperty(ResultSetMetaData metadata, int i) throws SQLException {

    String name = (useName) ? metadata.getColumnName(i) : metadata.getColumnLabel(i);

    if (lowerCase) {
        name = name.toLowerCase();
    }

    String className = null;
    try {
        className = metadata.getColumnClassName(i);
    } catch (SQLException e) {
        // this is a patch for HsqlDb to ignore exceptions
        // thrown by its metadata implementation
    }

    // Default to Object type if no class name could be retrieved
    // from the metadata
    Class clazz = Object.class;
    if (className != null) {
        clazz = loadClass(className);
    }
    return new DynaProperty(name, clazz);

}

From source file:org.apache.ibatis.jdbc.SqlRunner.java

private List<Map<String, Object>> getResults(ResultSet rs) throws SQLException {
    try {//from  w w  w.j a v  a 2 s .co m
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<String> columns = new ArrayList<String>();
        List<TypeHandler<?>> typeHandlers = new ArrayList<TypeHandler<?>>();
        ResultSetMetaData rsmd = rs.getMetaData();

        for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
            columns.add(rsmd.getColumnLabel(i + 1));

            try {
                Class<?> type = Resources.classForName(rsmd.getColumnClassName(i + 1));
                TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(type);

                if (typeHandler == null) {
                    typeHandler = typeHandlerRegistry.getTypeHandler(Object.class);
                }

                typeHandlers.add(typeHandler);
            } catch (Exception e) {
                typeHandlers.add(typeHandlerRegistry.getTypeHandler(Object.class));
            }
        }

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

            for (int i = 0, n = columns.size(); i < n; i++) {
                String name = columns.get(i);
                TypeHandler<?> handler = typeHandlers.get(i);
                row.put(name.toUpperCase(Locale.ENGLISH), handler.getResult(rs, name));
            }

            list.add(row);
        }

        return list;
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            // ignore
        }
    }
}

From source file:org.jumpmind.vaadin.ui.sqlexplorer.SqlRunner.java

protected Class<?> getClass(ResultSetMetaData meta, int i) throws SQLException {
    try {//from  w ww  . j  a  v  a 2s  . c  om
        return Class.forName(meta.getColumnClassName(i));
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:kenh.xscript.database.beans.ResultSetBean.java

/**
 * Use result set to initial a bean.//from   w ww .j a va2 s .  c om
 * 
 * @param rs
 * @param includeFieldName
 * @throws SQLException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public ResultSetBean(ResultSet rs, boolean includeFieldName)
        throws SQLException, IllegalAccessException, InstantiationException {
    include_field_name = includeFieldName;

    LazyDynaClass beanClass = new LazyDynaClass();

    ResultSetMetaData m = rs.getMetaData();
    for (int i = 1; i <= m.getColumnCount(); i++) {
        Column c = new Column();

        try {
            c.catalogName = m.getCatalogName(i);
        } catch (SQLException e) {
        }
        try {
            c.className = m.getColumnClassName(i);
        } catch (SQLException e) {
        }
        try {
            c.displaySize = m.getColumnDisplaySize(i);
        } catch (SQLException e) {
        }
        try {
            c.label = m.getColumnLabel(i);
        } catch (SQLException e) {
        }
        try {
            c.name = m.getColumnName(i);
        } catch (SQLException e) {
        }
        try {
            c.type = m.getColumnType(i);
        } catch (SQLException e) {
        }
        try {
            c.typeName = m.getColumnTypeName(i);
        } catch (SQLException e) {
        }
        try {
            c.precision = m.getPrecision(i);
        } catch (SQLException e) {
        }
        try {
            c.scale = m.getScale(i);
        } catch (SQLException e) {
        }
        try {
            c.schemaName = m.getSchemaName(i);
        } catch (SQLException e) {
        }
        try {
            c.tableName = m.getTableName(i);
        } catch (SQLException e) {
        }

        beanClass.add(m.getColumnLabel(i).toLowerCase());
        beanClass.add("" + i);

        cols.add(c);
    }

    DynaBean colBean = beanClass.newInstance();
    int i = 1;
    for (Column col : cols) {
        String field = col.getLabel().toLowerCase();
        colBean.set(field, col.getLabel());
        colBean.set("" + i, col.getLabel());
        i++;
    }

    if (include_field_name)
        rows.add(colBean);

    while (rs.next()) {
        DynaBean bean = beanClass.newInstance();
        i = 1;
        for (Column c : cols) {
            String field = c.getLabel().toLowerCase();
            Object obj = rs.getObject(field);
            bean.set(field, obj);
            bean.set("" + i, obj);
            i++;
        }
        rows.add(bean);
    }

}

From source file:SeeAccount.java

public void doGet(HttpServletRequest inRequest, HttpServletResponse outResponse)
        throws ServletException, IOException {

    PrintWriter out = null;//  w w w . j  av a  2  s. c o m
    Connection connection = null;
    Statement statement = null;

    ResultSet rs;

    try {
        outResponse.setContentType("text/html");
        out = outResponse.getWriter();

        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
        connection = ds.getConnection();

        statement = connection.createStatement();
        rs = statement.executeQuery("SELECT * FROM acc_acc");
        ResultSetMetaData md = rs.getMetaData();

        out.println("<HTML><HEAD><TITLE>        Thumbnail Identification Record</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("Account Information:<BR>");
        out.println("<table>");
        out.println("<tr><td>");
        for (int i = 1; i <= md.getColumnCount(); i++) {
            out.println("Column #" + i + "<BR>");
            out.println("getColumnName : " + md.getColumnName(i) + "<BR>");
            out.println("getColumnClassName : " + md.getColumnClassName(i) + "<BR>");
            out.println("getColumnDisplaySize : " + md.getColumnDisplaySize(i) + "<BR>");
            out.println("getColumnType : " + md.getColumnType(i) + "<BR>");
            out.println("getTableName : " + md.getTableName(i) + "<BR>");
            out.println("<HR>");
        }
        out.println("</BODY></HTML>");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ViewDB.java

 /**
 * Constructs the data panel./*from w  ww  .j av  a 2 s .  c  o  m*/
 * @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:com.taobao.tddl.common.jdbc.MetaDataQueryForMapHandler.java

/**
 * @param tableName //from w  ww . j a  va2 s  . co m
 */
private void initMetaData(String tableName, ResultSetMetaData rsmd) {
    try {
        int columnCount = rsmd.getColumnCount();
        String[] columnNames = new String[columnCount];
        ColumnMetaData[] columns = new ColumnMetaData[columnCount];
        for (int i = 1; i <= columnCount; i++) {
            columnNames[i - 1] = rsmd.getColumnName(i).toLowerCase();
            int sqlType = rsmd.getColumnType(i);
            if (sqlType == java.sql.Types.DATE) {
                sqlType = java.sql.Types.TIMESTAMP;
            }
            int scale = rsmd.getScale(i);
            String className = rsmd.getColumnClassName(i);
            columns[i - 1] = new ColumnMetaData(sqlType, scale, className);
        }
        TableMetaData tmd = new TableMetaData(columnNames, columns);
        this.tableMetaDatas.putIfAbsent(tableName, tmd);
    } catch (SQLException e) {
        log.warn("Fetch Metadata from resultSet failed.", e);
    }
}