List of usage examples for java.sql ResultSetMetaData getColumnName
String getColumnName(int column) throws SQLException;
From source file:com.kumarvv.setl.core.Extractor.java
/** * parses data row to {@link Row} object * * @param jrs/*from ww w . j a v a 2 s . c o m*/ * @param meta * @return {@link Row} * @throws SQLException */ Row parseDataRow(JdbcRowSet jrs, ResultSetMetaData meta) throws SQLException { if (jrs == null || meta == null) { return null; } int colCount = meta.getColumnCount(); Map<String, Object> row = new HashMap<>(); for (int c = 1; c <= colCount; c++) { row.put(meta.getColumnName(c).toLowerCase(), jrs.getObject(c)); } Row ro = new Row(fromColumns, row); addToQueue(ro); return ro; }
From source file:com.streamsets.pipeline.stage.executor.jdbc.TestJdbcQueryExecutor.java
/** * Validate structure of the result set (column names and types). *//*ww w . j a va 2 s .com*/ public void assertResultSetStructure(ResultSet rs, Pair<String, Integer>... columns) throws Exception { ResultSetMetaData metaData = rs.getMetaData(); Assert.assertEquals(Utils.format("Unexpected number of columns"), columns.length, metaData.getColumnCount()); int i = 1; for (Pair<String, Integer> column : columns) { Assert.assertEquals(Utils.format("Unexpected name for column {}", i), column.getLeft(), metaData.getColumnName(i)); Assert.assertEquals(Utils.format("Unexpected type for column {}", i), (int) column.getRight(), metaData.getColumnType(i)); i++; } }
From source file:org.syncope.core.util.ImportExport.java
private void doExportTable(final TransformerHandler handler, final Connection conn, final String tableName) throws SQLException, SAXException { AttributesImpl atts = new AttributesImpl(); PreparedStatement stmt = null; ResultSet rs = null;//from w w w .j a va 2 s . c o m try { stmt = conn.prepareStatement("SELECT * FROM " + tableName + " a"); rs = stmt.executeQuery(); for (int rowNo = 0; rs.next(); rowNo++) { atts.clear(); ResultSetMetaData metaData = rs.getMetaData(); for (int i = 0; i < metaData.getColumnCount(); i++) { String columnName = metaData.getColumnName(i + 1); String value = rs.getString(columnName); if (value != null) { atts.addAttribute("", "", columnName, "CDATA", value); } } handler.startElement("", "", tableName, atts); handler.endElement("", "", tableName); } } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { LOG.error("While closing result set", e); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { LOG.error("While closing result set", e); } } } }
From source file:com.abixen.platform.service.businessintelligence.multivisualisation.service.impl.AbstractDatabaseService.java
private DataSourceColumnWeb prepareDataSourceColumns(ResultSetMetaData rsmd, int i) throws SQLException { DataValueType dataValueType = DataValueType.valueOf(getValidColumnTypeName(i, rsmd)); String name = rsmd.getColumnName(i); return new DataSourceColumnWeb() { @Override/*w ww . j av a2s . c o m*/ public Long getId() { return null; } @Override public String getName() { return name; } @Override public Integer getPosition() { return null; } @Override public DataValueType getDataValueType() { return dataValueType; } }; }
From source file:SeeAccount.java
public void doGet(HttpServletRequest inRequest, HttpServletResponse outResponse) throws ServletException, IOException { PrintWriter out = null;/*from w w w.j a v 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:com.abixen.platform.service.businessintelligence.multivisualization.service.impl.AbstractDatabaseService.java
public List<String> getColumns(Connection connection, String tableName) { List<String> columns = new ArrayList<>(); try {/* w w w . ja v a 2 s. com*/ ResultSetMetaData rsmd = getDatabaseMetaData(connection, tableName); int columnCount = rsmd.getColumnCount(); IntStream.range(1, columnCount + 1).forEach(i -> { try { columns.add(rsmd.getColumnName(i)); } catch (SQLException e) { e.printStackTrace(); } }); } catch (SQLException e) { e.printStackTrace(); } return columns; }
From source file:com.handu.open.dubbo.monitor.dao.base.DubboInvokeBaseDAO.java
/** * SQL?//from w w w . java 2s .c o m * * @param sql SQL? * @return List<Map> */ public List<Map> querySql(String sql) { List<Map> list = Lists.newArrayList(); try { ResultSet rs = getSqlSession().getConnection() .prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE) .executeQuery(); try { ResultSetMetaData rsm = rs.getMetaData(); // int col = rsm.getColumnCount(); // String[] colName = new String[col]; //???, colName for (int i = 0; i < col; i++) { colName[i] = rsm.getColumnName(i + 1); } rs.beforeFirst(); while (rs.next()) { Map<String, String> map = Maps.newHashMap(); for (String aColName : colName) { map.put(aColName, rs.getString(aColName)); } list.add(map); } } catch (SQLException e) { e.printStackTrace(); return null; } } catch (SQLException e) { e.printStackTrace(); } return list; }
From source file:kr.co.bitnine.octopus.engine.CursorHive.java
@Override public TupleDesc describe() throws PostgresException { if (tupDesc != null) return tupDesc; prepareConnection();//from w w w . j a v a 2 s .c om prepareStatement(0); try { checkCancel(); ResultSet rs = stmt.executeQuery(); checkCancel(); ResultSetMetaData rsmd = rs.getMetaData(); int colCnt = rsmd.getColumnCount(); PostgresAttribute[] attrs = new PostgresAttribute[colCnt]; for (int i = 0; i < colCnt; i++) { String colName = getColumnName(rsmd.getColumnName(i + 1)); int colType = rsmd.getColumnType(i + 1); LOG.debug("JDBC type of column '" + colName + "' is " + colType); PostgresType type = TypeInfo.postresTypeOfJdbcType(colType); int typeInfo = -1; if (type == PostgresType.VARCHAR) typeInfo = rsmd.getColumnDisplaySize(i + 1); attrs[i] = new PostgresAttribute(colName, type, typeInfo); } rs.close(); stmt.close(); stmt = null; tupDesc = new TupleDesc(attrs, getResultFormats()); return tupDesc; } catch (SQLException e) { PostgresErrorData edata = new PostgresErrorData(PostgresSeverity.ERROR, "failed to execute by-pass query: " + e.getMessage()); throw new PostgresException(edata, e); } }
From source file:org.apache.hadoop.sqoop.manager.SqlManager.java
@Override public Map<String, Integer> getColumnTypes(String tableName) { String stmt = "SELECT t.* FROM " + tableName + " AS t WHERE 1 = 1"; ResultSet results = execute(stmt); if (null == results) { return null; }//from w w w . j ava 2 s .c o m try { Map<String, Integer> colTypes = new HashMap<String, Integer>(); int cols = results.getMetaData().getColumnCount(); ResultSetMetaData metadata = results.getMetaData(); for (int i = 1; i < cols + 1; i++) { int typeId = metadata.getColumnType(i); String colName = metadata.getColumnName(i); if (colName == null || colName.equals("")) { colName = metadata.getColumnLabel(i); } colTypes.put(colName, Integer.valueOf(typeId)); } return colTypes; } catch (SQLException sqlException) { LOG.error("Error reading from database: " + sqlException.toString()); return null; } }
From source file:kr.co.bitnine.octopus.engine.CursorByPass.java
private void execute(int numRows) throws PostgresException { if (getState() == State.DONE || getState() == State.FAILED) setState(State.READY);//from w ww. j ava2 s .c om if (getState() != State.READY) return; LOG.debug("execute CursorByPass (rows=" + numRows + ")"); try { // NOTE: some JDBC drivers do not ignore setFetchSize(0) if (numRows > 0) stmt.setFetchSize(numRows); checkCancel(); ResultSet rs = stmt.executeQuery(); checkCancel(); ResultSetMetaData rsmd = rs.getMetaData(); int colCnt = rsmd.getColumnCount(); PostgresAttribute[] attrs = new PostgresAttribute[colCnt]; for (int i = 0; i < colCnt; i++) { String colName = rsmd.getColumnName(i + 1); int colType = rsmd.getColumnType(i + 1); LOG.info("JDBC type of column '" + colName + "' is " + colType); PostgresType type = TypeInfo.postresTypeOfJdbcType(colType); int typeInfo = -1; if (type == PostgresType.VARCHAR) typeInfo = rsmd.getColumnDisplaySize(i + 1); attrs[i] = new PostgresAttribute(colName, type, typeInfo); } tupDesc = new TupleDesc(attrs, getResultFormats()); tupSetByPass = new TupleSetByPass(this, rs, tupDesc); setState(State.ACTIVE); } catch (SQLException e) { setState(State.FAILED); close(); PostgresErrorData edata = new PostgresErrorData(PostgresSeverity.ERROR, "failed to execute by-pass query: " + e.getMessage()); throw new PostgresException(edata, e); } }