List of usage examples for java.sql ResultSetMetaData getColumnLabel
String getColumnLabel(int column) throws SQLException;
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st = conn.createStatement();//from w w w. jav a 2 s .c om ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); System.out.println("resultSet MetaData column Count=" + numberOfColumns); for (int i = 1; i <= numberOfColumns; i++) { System.out.println("column MetaData "); System.out.println("column number " + i); System.out.println(rsMetaData.getColumnDisplaySize(i)); System.out.println(rsMetaData.getColumnLabel(i)); System.out.println(rsMetaData.getColumnName(i)); System.out.println(rsMetaData.getColumnType(i)); System.out.println(rsMetaData.getColumnTypeName(i)); System.out.println(rsMetaData.getColumnClassName(i)); System.out.println(rsMetaData.getTableName(i)); System.out.println(rsMetaData.getPrecision(i)); System.out.println(rsMetaData.getScale(i)); System.out.println(rsMetaData.isAutoIncrement(i)); System.out.println(rsMetaData.isCurrency(i)); System.out.println(rsMetaData.isWritable(i)); System.out.println(rsMetaData.isDefinitelyWritable(i)); System.out.println(rsMetaData.isNullable(i)); System.out.println(rsMetaData.isReadOnly(i)); System.out.println(rsMetaData.isCaseSensitive(i)); System.out.println(rsMetaData.isSearchable(i)); System.out.println(rsMetaData.isSigned(i)); System.out.println(rsMetaData.getCatalogName(i)); System.out.println(rsMetaData.getSchemaName(i)); } st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st = conn.createStatement();// w ww. j ava 2s . c om ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); System.out.println("resultSet MetaData column Count=" + numberOfColumns); for (int i = 1; i <= numberOfColumns; i++) { System.out.println("column MetaData "); System.out.println("column number " + i); // indicates the designated column's normal maximum width in // characters System.out.println(rsMetaData.getColumnDisplaySize(i)); // gets the designated column's suggested title // for use in printouts and displays. System.out.println(rsMetaData.getColumnLabel(i)); // get the designated column's name. System.out.println(rsMetaData.getColumnName(i)); // get the designated column's SQL type. System.out.println(rsMetaData.getColumnType(i)); // get the designated column's SQL type name. System.out.println(rsMetaData.getColumnTypeName(i)); // get the designated column's class name. System.out.println(rsMetaData.getColumnClassName(i)); // get the designated column's table name. System.out.println(rsMetaData.getTableName(i)); // get the designated column's number of decimal digits. System.out.println(rsMetaData.getPrecision(i)); // gets the designated column's number of // digits to right of the decimal point. System.out.println(rsMetaData.getScale(i)); // indicates whether the designated column is // automatically numbered, thus read-only. System.out.println(rsMetaData.isAutoIncrement(i)); // indicates whether the designated column is a cash value. System.out.println(rsMetaData.isCurrency(i)); // indicates whether a write on the designated // column will succeed. System.out.println(rsMetaData.isWritable(i)); // indicates whether a write on the designated // column will definitely succeed. System.out.println(rsMetaData.isDefinitelyWritable(i)); // indicates the nullability of values // in the designated column. System.out.println(rsMetaData.isNullable(i)); // Indicates whether the designated column // is definitely not writable. System.out.println(rsMetaData.isReadOnly(i)); // Indicates whether a column's case matters // in the designated column. System.out.println(rsMetaData.isCaseSensitive(i)); // Indicates whether a column's case matters // in the designated column. System.out.println(rsMetaData.isSearchable(i)); // indicates whether values in the designated // column are signed numbers. System.out.println(rsMetaData.isSigned(i)); // Gets the designated column's table's catalog name. System.out.println(rsMetaData.getCatalogName(i)); // Gets the designated column's table's schema name. System.out.println(rsMetaData.getSchemaName(i)); } st.close(); conn.close(); }
From source file:org.compass.gps.device.jdbc.JdbcUtils.java
/** * Returns the column index for the guven column name. Note that if there * are two columns with the same name, the first onde index will be * returned./*w ww .j a va2 s .c om*/ * <p> * <code>-1</code> is returned if none is found. */ public static int getColumnIndexFromColumnName(ResultSetMetaData metaData, String columnName) throws SQLException { for (int i = 1; i <= metaData.getColumnCount(); i++) { String tmpName = metaData.getColumnLabel(i); if (tmpName.equalsIgnoreCase(columnName)) { return i; } } return -1; }
From source file:Student.java
public static void checkData() throws Exception { Class.forName("org.hsqldb.jdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", ""); Statement st = conn.createStatement(); ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); while (mrs.next()) { String tableName = mrs.getString(3); System.out.println("\n\n\n\nTable Name: "+ tableName); ResultSet rs = st.executeQuery("select * from " + tableName); ResultSetMetaData metadata = rs.getMetaData(); while (rs.next()) { System.out.println(" Row:"); for (int i = 0; i < metadata.getColumnCount(); i++) { System.out.println(" Column Name: "+ metadata.getColumnLabel(i + 1)+ ", "); System.out.println(" Column Type: "+ metadata.getColumnTypeName(i + 1)+ ": "); Object value = rs.getObject(i + 1); System.out.println(" Column Value: "+value+"\n"); }/*from w w w .j a v a 2 s.co m*/ } } }
From source file:org.silverpeas.core.test.util.SQLRequester.java
/** * Finds one and only one entity by executing the specified SQL query with the given parameters. * If there is more than one entity found then an {@link IllegalArgumentException} is thrown. * @param query the query to use to find one entity. * @param parameters the parameters to apply with the query. * @return a {@link Map} with the persisted attributes of the asked entity. The name of the * attributes are all in uppercase.//from w w w . j a v a2s . c o m * @throws SQLException if an error occurs while executing the given SQL query. */ public static Map<String, Object> findOne(final String query, final Object... parameters) throws SQLException { Map<String, Object> results = new HashMap<>(); try (Connection connection = DataSourceProvider.getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement(query)) { for (int i = 1; i <= parameters.length; i++) { statement.setObject(i, parameters[i - 1]); } try (ResultSet rs = statement.executeQuery()) { if (rs.next()) { ResultSetMetaData metaData = rs.getMetaData(); for (int i = 1; i <= metaData.getColumnCount(); i++) { results.put(metaData.getColumnLabel(i).toUpperCase(), rs.getObject(i)); } if (rs.next()) { throw new IllegalArgumentException("The query fetch more than one entity!"); } } } } return results; }
From source file:com.hangum.tadpole.commons.sql.util.SQLUtil.java
/** * metadata ./*from w ww. j a va 2 s . c o m*/ * * @param rs * @return index? */ public static HashMap<Integer, String> mataDataToMap(ResultSet rs) throws Exception { HashMap<Integer, String> map = new HashMap<Integer, String>(); ResultSetMetaData rsm = rs.getMetaData(); int columnCount = rsm.getColumnCount(); for (int i = 0; i < columnCount; i++) { map.put(i, rsm.getColumnLabel(i + 1)); } return map; }
From source file:architecture.common.adaptor.connector.jdbc.AbstractJdbcConnector.java
public static String lookupColumnName(ResultSetMetaData resultSetMetaData, int columnIndex) throws SQLException { String name = resultSetMetaData.getColumnLabel(columnIndex); if (name == null || name.length() < 1) { name = resultSetMetaData.getColumnName(columnIndex); }/*from www. j av a2 s .c om*/ return name; }
From source file:Student.java
public static void checkData() throws Exception { Class.forName("org.hsqldb.jdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", ""); Statement st = conn.createStatement(); ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); while (mrs.next()) { String tableName = mrs.getString(3); System.out.println("\n\n\n\nTable Name: " + tableName); ResultSet rs = st.executeQuery("select * from " + tableName); ResultSetMetaData metadata = rs.getMetaData(); while (rs.next()) { System.out.println(" Row:"); for (int i = 0; i < metadata.getColumnCount(); i++) { System.out.println(" Column Name: " + metadata.getColumnLabel(i + 1) + ", "); System.out.println(" Column Type: " + metadata.getColumnTypeName(i + 1) + ": "); Object value = rs.getObject(i + 1); System.out.println(" Column Value: " + value + "\n"); }/* www .j av a 2s. c o m*/ } } }
From source file:io.cloudslang.content.database.services.SQLQueryLobService.java
public static boolean executeSqlQueryLob(SQLInputs sqlInputs) throws Exception { if (StringUtils.isEmpty(sqlInputs.getSqlCommand())) { throw new Exception("command input is empty."); }/*from ww w .ja va 2s . c o m*/ boolean isLOB = false; ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { StringBuilder strColumns = new StringBuilder(sqlInputs.getStrColumns()); connection.setReadOnly(true); Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency()); statement.setQueryTimeout(sqlInputs.getTimeout()); ResultSet results = statement.executeQuery(sqlInputs.getSqlCommand()); ResultSetMetaData mtd = results.getMetaData(); int iNumCols = mtd.getColumnCount(); for (int i = 1; i <= iNumCols; i++) { if (i > 1) strColumns.append(sqlInputs.getStrDelim()); strColumns.append(mtd.getColumnLabel(i)); } sqlInputs.setStrColumns(strColumns.toString()); int nr = -1; while (results.next()) { nr++; final StringBuilder strRowHolder = new StringBuilder(); for (int i = 1; i <= iNumCols; i++) { if (i > 1) strRowHolder.append(sqlInputs.getStrDelim()); Object columnObject = results.getObject(i); if (columnObject != null) { String value; if (columnObject instanceof java.sql.Clob) { isLOB = true; final File tmpFile = File.createTempFile("CLOB_" + mtd.getColumnLabel(i), ".txt"); copyInputStreamToFile( new ReaderInputStream(results.getCharacterStream(i), StandardCharsets.UTF_8), tmpFile); if (sqlInputs.getLRowsFiles().size() == nr) { sqlInputs.getLRowsFiles().add(nr, new ArrayList<String>()); sqlInputs.getLRowsNames().add(nr, new ArrayList<String>()); } sqlInputs.getLRowsFiles().get(nr).add(tmpFile.getAbsolutePath()); sqlInputs.getLRowsNames().get(nr).add(mtd.getColumnLabel(i)); value = "(CLOB)..."; } else { value = results.getString(i); if (sqlInputs.isNetcool()) value = SQLUtils.processNullTerminatedString(value); } strRowHolder.append(value); } else strRowHolder.append("null"); } sqlInputs.getLRows().add(strRowHolder.toString()); } } return isLOB; }
From source file:com.adaptris.jdbc.JdbcResultSetImpl.java
private static JdbcResultRow mapRow(ResultSet resultSet) throws SQLException { ResultSetMetaData rsmd = resultSet.getMetaData(); int columnCount = rsmd.getColumnCount(); JdbcResultRow row = new JdbcResultRow(); for (int counter = 1; counter <= columnCount; counter++) { row.setFieldValue(StringUtils.defaultIfEmpty(rsmd.getColumnLabel(counter), rsmd.getColumnName(counter)), resultSet.getObject(counter), rsmd.getColumnType(counter)); }//from w w w . j a v a 2s . c o m return row; }