List of usage examples for java.sql ResultSetMetaData getTableName
String getTableName(int column) throws SQLException;
From source file:com.hangum.tadpole.engine.sql.util.resultset.ResultSetUtils.java
/** * ? table name/*from ww w . j a v a 2 s . c om*/ * * @param isShowRowNum * @param rs * @return * @throws Exception */ public static Map<Integer, String> getColumnTableName(final UserDBDAO userDB, boolean isShowRowNum, ResultSet rs) throws Exception { Map<Integer, String> mapColumnName = new HashMap<Integer, String>(); int intStartIndex = 0; if (isShowRowNum) { intStartIndex++; mapColumnName.put(0, "#"); } ResultSetMetaData rsm = rs.getMetaData(); for (int i = 0; i < rsm.getColumnCount(); i++) { // if(userDB.getDBDefine() == DBDefine.POSTGRE_DEFAULT) { // PGResultSetMetaData pgsqlMeta = (PGResultSetMetaData)rsm; // mapColumnName.put(i+intStartIndex, pgsqlMeta.getBaseTableName(i+1)); // //// if(logger.isDebugEnabled()) logger.debug("Table name is " + pgsqlMeta.getBaseTableName(i+1)); // } else if (userDB.getDBDefine() == DBDefine.HIVE_DEFAULT || userDB.getDBDefine() == DBDefine.HIVE2_DEFAULT) { mapColumnName.put(i + intStartIndex, "Apache Hive is not support this method."); } else { if (rsm.getSchemaName(i + 1) == null || "".equals(rsm.getSchemaName(i + 1))) { // if(logger.isDebugEnabled()) logger.debug("Table name is " + rsm.getTableName(i+1) + ", schema name is " + rsm.getSchemaName(i+1)); mapColumnName.put(i + intStartIndex, rsm.getTableName(i + 1)); } else { mapColumnName.put(i + intStartIndex, rsm.getSchemaName(i + 1) + "." + rsm.getTableName(i + 1)); } } } return mapColumnName; }
From source file:solidbase.core.plugins.DBReader.java
public DBReader(ResultSet result, ExportLogger counter, boolean dateAsTimestamp) throws SQLException { this.result = result; this.counter = counter; ResultSetMetaData metaData = result.getMetaData(); int count = metaData.getColumnCount(); this.columns = new Column[count]; for (int i = 0; i < count; i++) { int col = i + 1; String name = metaData.getColumnName(col).toUpperCase(); int type = metaData.getColumnType(col); if (type == Types.DATE && dateAsTimestamp) type = Types.TIMESTAMP; String table = StringUtils.upperCase(StringUtils.defaultIfEmpty(metaData.getTableName(col), null)); String schema = StringUtils.upperCase(StringUtils.defaultIfEmpty(metaData.getSchemaName(col), null)); this.columns[i] = new Column(name, type, table, schema); }//from w w w . ja va 2 s .co m }
From source file:org.apache.ddlutils.platform.ModelBasedResultSetIterator.java
/** * Initializes this iterator from the resultset metadata. * /*from w w w . ja v a 2 s .c o m*/ * @param model The database model */ private void initFromMetaData(Database model) throws SQLException { ResultSetMetaData metaData = _resultSet.getMetaData(); String tableName = null; boolean singleKnownTable = true; for (int idx = 1; idx <= metaData.getColumnCount(); idx++) { String columnName = metaData.getColumnName(idx); String tableOfColumn = metaData.getTableName(idx); Table table = null; if ((tableOfColumn != null) && (tableOfColumn.length() > 0)) { // jConnect might return a table name enclosed in quotes if (tableOfColumn.startsWith("\"") && tableOfColumn.endsWith("\"") && (tableOfColumn.length() > 1)) { tableOfColumn = tableOfColumn.substring(1, tableOfColumn.length() - 1); } // the JDBC driver gave us enough meta data info table = model.findTable(tableOfColumn, _caseSensitive); } if (table == null) { // not enough info in the meta data of the result set, lets try the // user-supplied query hints table = (Table) _preparedQueryHints.get(_caseSensitive ? columnName : columnName.toLowerCase()); tableOfColumn = (table == null ? null : table.getName()); } if (tableName == null) { tableName = tableOfColumn; } else if (!tableName.equals(tableOfColumn)) { singleKnownTable = false; } String propName = columnName; if (table != null) { Column column = table.findColumn(columnName, _caseSensitive); if (column != null) { propName = column.getName(); } } _columnsToProperties.put(columnName, propName); } if (singleKnownTable && (tableName != null)) { _dynaClass = model.getDynaClassFor(tableName); } else { DynaProperty[] props = new DynaProperty[_columnsToProperties.size()]; int idx = 0; for (Iterator it = _columnsToProperties.values().iterator(); it.hasNext(); idx++) { props[idx] = new DynaProperty((String) it.next()); } _dynaClass = new BasicDynaClass("result", BasicDynaBean.class, props); } }
From source file:com.sangupta.fileanalysis.db.DBResultViewer.java
/** * View resutls of a {@link ResultSet}.//w w w . j ava 2 s .c o m * * @param resultSet * @throws SQLException */ public void viewResult(ResultSet resultSet) throws SQLException { if (resultSet == null) { // nothing to do return; } // collect the meta ResultSetMetaData meta = resultSet.getMetaData(); final int numColumns = meta.getColumnCount(); final int[] displaySizes = new int[numColumns + 1]; final int[] colType = new int[numColumns + 1]; for (int index = 1; index <= numColumns; index++) { colType[index] = meta.getColumnType(index); displaySizes[index] = getColumnSize(meta.getTableName(index), meta.getColumnName(index), colType[index]); } // display the header row for (int index = 1; index <= numColumns; index++) { center(meta.getColumnLabel(index), displaySizes[index]); } System.out.println("|"); for (int index = 1; index <= numColumns; index++) { System.out.print("+" + StringUtils.repeat('-', displaySizes[index] + 2)); } System.out.println("+"); // start iterating over the result set int rowsDisplayed = 0; int numRecords = 0; while (resultSet.next()) { // read and display the value rowsDisplayed++; numRecords++; for (int index = 1; index <= numColumns; index++) { switch (colType[index]) { case Types.DECIMAL: case Types.DOUBLE: case Types.REAL: format(resultSet.getDouble(index), displaySizes[index]); continue; case Types.INTEGER: case Types.SMALLINT: format(resultSet.getInt(index), displaySizes[index]); continue; case Types.VARCHAR: format(resultSet.getString(index), displaySizes[index], false); continue; case Types.TIMESTAMP: format(resultSet.getTimestamp(index), displaySizes[index]); continue; case Types.BIGINT: format(resultSet.getBigDecimal(index), displaySizes[index]); continue; } } // terminator for row and new line System.out.println("|"); // check for rows displayed if (rowsDisplayed == 20) { // ask the user if more data needs to be displayed String cont = ConsoleUtils.readLine("Type \"it\" for more: ", true); if (!"it".equalsIgnoreCase(cont)) { break; } // continue; rowsDisplayed = 0; continue; } } System.out.println("\nTotal number of records found: " + numRecords); }
From source file:DbMetaServlet.java
private void printMeta(ResultSetMetaData metaData, String type, java.io.PrintWriter out, int colCount) throws SQLException { if (metaData == null || type == null || out == null) throw new IllegalArgumentException("Illegal args passed to printMeta()"); out.println("<tr>"); if (type.equals("table")) { out.println("<td><strong>Table name</strong></td>"); for (int i = 1; i <= colCount; ++i) { out.println("<td>" + metaData.getTableName(i) + "</td>"); }/* w ww . j a v a 2 s . c o m*/ } else if (type.equals("name")) { out.println("<td><strong>Column name</strong></td>"); for (int i = 1; i <= colCount; ++i) { out.println("<td>" + metaData.getColumnName(i) + "</td>"); } } else if (type.equals("index")) { out.println("<td><strong>Column index</strong></td>"); for (int i = 1; i <= colCount; ++i) { out.println("<td>" + i + "</td>"); } } else if (type.equals("column type")) { out.println("<td><strong>Column type</strong></td>"); for (int i = 1; i <= colCount; ++i) { out.println("<td>" + metaData.getColumnTypeName(i) + "</td>"); } } else if (type.equals("column display")) { out.println("<td><strong>Column display size</strong></td>"); for (int i = 1; i <= colCount; ++i) { out.println("<td>" + metaData.getColumnDisplaySize(i) + "</td>"); } } out.println("</tr>"); }
From source file:org.exolab.castor.jdo.engine.SQLQuery.java
private Object loadSingleField(final int i, final CounterRef counterReference) throws SQLException { String currentTableName = counterReference.getTableName(); int count = counterReference.getCounter(); String fieldTableName = _engine.getInfo()[i].getTableName(); String fieldColumnName = _engine.getInfo()[i].getColumnInfo()[0].getName(); String fieldName = fieldTableName + "." + fieldColumnName; ResultSetMetaData metaData = _rs.getMetaData(); while (true) { String metaTableName = metaData.getTableName(count); String metaColumnName = metaData.getColumnName(count); if (fieldColumnName.equalsIgnoreCase(metaColumnName)) { if (!_isCallSql) { if (fieldTableName.equalsIgnoreCase(metaTableName)) { break; } else if ("".equals(metaTableName)) { break; }/*from w w w . j av a 2 s .co m*/ } else { // if we are running as a result of a CALL SQL statement, let's // relax our checks. break; } } else if (fieldName.equalsIgnoreCase(metaColumnName)) { break; } count++; } SQLFieldInfo info = _engine.getInfo()[i]; Object field; if (!info.isJoined() && (info.getJoinFields() == null)) { field = info.getColumnInfo()[0] .toJava(SQLTypeInfos.getValue(_rs, count, info.getColumnInfo()[0].getSqlType())); count++; } else { boolean notNull = false; Object[] temp = new Object[info.getColumnInfo().length]; for (int j = 0; j < info.getColumnInfo().length; j++) { temp[j] = info.getColumnInfo()[j] .toJava(SQLTypeInfos.getValue(_rs, count, info.getColumnInfo()[j].getSqlType())); count++; if (temp[j] != null) { notNull = true; } } field = ((notNull) ? new Identity(temp) : null); } counterReference.setCounter(count); counterReference.setTableName(currentTableName); return field; }
From source file:jp.co.golorp.emarf.model.Models.java
/** * Model?????IO/* w ww .j a v a 2s. c o m*/ * * @param sql * sql * @param params * params * @return List */ private static List<Map<String, Object>> getDatas(final String sql, final Object... params) { // ?SQL? String rawSql = getRawSql(sql, params); // SQL?????? List<Map<String, Object>> datas = ModelsCache.get(rawSql); if (datas != null) { return datas; } // statementLog(rawSql); // ? PreparedStatement ps = null; try { ps = Connections.get().prepareStatement(sql); for (int i = 0; i < params.length; i++) { ps.setString(i + 1, String.valueOf(params[i])); } // ?? datas = new ArrayList<Map<String, Object>>(); ResultSet rs = null; try { // ??? rs = ps.executeQuery(); while (rs.next()) { // ? Map<String, Object> data = new LinkedHashMap<String, Object>(); // ResultSet?META? ResultSetMetaData meta = rs.getMetaData(); // META???? int columnCount = meta.getColumnCount(); for (int i = 1; i <= columnCount; i++) { // ??? String columnName = meta.getColumnName(i); // ??? String propertyName = StringUtil.toCamelCase(columnName); String key = propertyName; if (data.containsKey(propertyName)) { String modelName = StringUtil.toUpperCamelCase(meta.getTableName(i)); key = modelName + "." + propertyName; } // data.put(key, rs.getObject(columnName)); } // datas.add(data); } } catch (SQLException e) { throw new SystemError(e); } finally { IOUtil.closeQuietly(rs); } } catch (SQLException e) { throw new SystemError(e); } finally { IOUtil.closeQuietly(ps); } // SQL? // ModelsCache.set(rawSql, datas); return datas; }
From source file:org.exolab.castor.jdo.engine.SQLQuery.java
private Object loadMultiField(final int i, final CounterRef counterReference, final Object field) throws SQLException { int count = counterReference.getCounter(); String fieldTableName = _engine.getInfo()[i].getTableName(); String firstColumnOfField = _engine.getInfo()[i].getColumnInfo()[0].getName(); ResultSetMetaData metaData = _rs.getMetaData(); String columnNamePerMetaData = metaData.getColumnName(count); String tableNamePerMetaData = metaData.getTableName(count); while (!(firstColumnOfField.equalsIgnoreCase(columnNamePerMetaData) && (fieldTableName.equalsIgnoreCase(tableNamePerMetaData) || tableNamePerMetaData.startsWith(fieldTableName) || "".equals(tableNamePerMetaData)))) { count++;//from w ww . j a v a 2 s. co m columnNamePerMetaData = metaData.getColumnName(count); tableNamePerMetaData = metaData.getTableName(count); } ArrayList res = ((field == null) ? new ArrayList() : (ArrayList) field); SQLFieldInfo info = _engine.getInfo()[i]; boolean notNull = false; Object[] temp = new Object[info.getColumnInfo().length]; for (int j = 0; j < info.getColumnInfo().length; j++) { temp[j] = info.getColumnInfo()[j] .toJava(SQLTypeInfos.getValue(_rs, count, info.getColumnInfo()[j].getSqlType())); if (temp[j] != null) { notNull = true; } count++; } if (notNull) { Identity identity = new Identity(temp); if (!res.contains(identity)) { res.add(identity); } } counterReference.setCounter(count); return res; }
From source file:org.apache.nifi.processors.standard.util.TestJdbcCommon.java
@Test public void testCreateSchemaNoColumns() throws ClassNotFoundException, SQLException { final ResultSet resultSet = mock(ResultSet.class); final ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); when(resultSet.getMetaData()).thenReturn(resultSetMetaData); when(resultSetMetaData.getColumnCount()).thenReturn(0); when(resultSetMetaData.getTableName(1)).thenThrow(SQLException.class); final Schema schema = JdbcCommon.createSchema(resultSet); assertNotNull(schema);// w w w . jav a 2s . c o m // records name, should be result set first column table name // Notice! sql select may join data from different tables, other columns // may have different table names assertEquals("NiFi_ExecuteSQL_Record", schema.getName()); assertNull(schema.getField("ID")); }
From source file:org.apache.nifi.processors.standard.util.TestJdbcCommon.java
@Test public void testCreateSchemaNoTableName() throws ClassNotFoundException, SQLException { final ResultSet resultSet = mock(ResultSet.class); final ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); when(resultSet.getMetaData()).thenReturn(resultSetMetaData); when(resultSetMetaData.getColumnCount()).thenReturn(1); when(resultSetMetaData.getTableName(1)).thenReturn(""); when(resultSetMetaData.getColumnType(1)).thenReturn(Types.INTEGER); when(resultSetMetaData.getColumnName(1)).thenReturn("ID"); final Schema schema = JdbcCommon.createSchema(resultSet); assertNotNull(schema);//from w ww . ja v a 2 s .co m // records name, should be result set first column table name assertEquals("NiFi_ExecuteSQL_Record", schema.getName()); }