List of usage examples for java.sql ResultSetMetaData getColumnDisplaySize
int getColumnDisplaySize(int column) throws SQLException;
From source file:Main.java
public static void main(String args[]) throws Exception { Connection conn = null;/*from w w w. jav a2 s . c om*/ Statement s = null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};" + "Server=.\\SQLEXPRESS;" + "Trusted_Connection=yes;" + "Database=myDb"); s = conn.createStatement(); s.executeQuery("SELECT * FROM dbo.SalesSummary WHERE 0 = 1"); ResultSet rs = s.getResultSet(); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { System.out.println(String.format("-- Column %d --", i)); System.out.println(String.format("Column name: %s", rsmd.getColumnName(i))); System.out.println(String.format("Database-specific type name: %s", rsmd.getColumnTypeName(i))); System.out.println(String.format("Column size (DisplaySize): %d", rsmd.getColumnDisplaySize(i))); System.out.println(String.format("java.sql.Type of column: %d", rsmd.getColumnType(i))); System.out.println(); } s.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();//from w ww . j ava 2 s. co m 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 w w .java 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); // 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. } 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();/*from w ww . j av a 2s . c o m*/ 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.dashbuilder.dataprovider.backend.sql.JDBCUtils.java
public static List<Column> getColumns(ResultSet resultSet, String[] exclude) throws SQLException { List<Column> columnList = new ArrayList<Column>(); List<String> columnExcluded = exclude == null ? new ArrayList<String>() : Arrays.asList(exclude); ResultSetMetaData meta = resultSet.getMetaData(); for (int i = 1; i <= meta.getColumnCount(); i++) { String name = meta.getColumnName(i); String alias = meta.getColumnLabel(i); if (!columnExcluded.contains(name) && !columnExcluded.contains(alias)) { ColumnType type = JDBCUtils.calculateType(meta.getColumnType(i)); int size = meta.getColumnDisplaySize(i); Column column = column(name, type, size).as(alias); columnList.add(column);/*w w w . j a v a 2 s . co m*/ } } return columnList; }
From source file:org.dashbuilder.dataprovider.sql.JDBCUtils.java
public static List<Column> getColumns(ResultSet resultSet, String[] exclude) throws SQLException { List<Column> columnList = new ArrayList<Column>(); List<String> columnExcluded = exclude == null ? new ArrayList<String>() : Arrays.asList(exclude); ResultSetMetaData meta = resultSet.getMetaData(); for (int i = 1; i <= meta.getColumnCount(); i++) { String name = meta.getColumnName(i); String alias = meta.getColumnLabel(i); if (alias != null && !alias.trim().isEmpty()) { name = alias.trim();/*from ww w .j a v a 2 s.c o m*/ } if (!columnExcluded.contains(name) && !columnExcluded.contains(alias)) { ColumnType type = JDBCUtils.calculateType(meta.getColumnType(i)); if (type != null) { int size = meta.getColumnDisplaySize(i); Column column = SQLFactory.column(name, type, size); columnList.add(column); } } } return columnList; }
From source file:org.jabsorb.ext.DataList.java
/** * Build an array of ColumnMetaData object from a ResultSetMetaData object. * * @param rmd ResultSetMetaData to build ColumnMetaData from. * @return ColumnMetaData array or null if ResultSetMetaData is null. * * @throws SQLException if there is a problem processing the * ResultSetMetaData object.//from ww w. j ava2s .c om */ public static ColumnMetaData[] buildColumnMetaDataFromResultSetMetaData(ResultSetMetaData rmd) throws SQLException { if (rmd == null) { return null; } int j = rmd.getColumnCount(); ColumnMetaData[] cmd = new ColumnMetaData[j]; for (int i = 1; i <= j; i++) { ColumnMetaData c = new ColumnMetaData(); c.setColumnName(rmd.getColumnName(i)); c.setCatalogName(rmd.getCatalogName(i)); c.setColumnClassName(rmd.getColumnClassName(i)); c.setColumnDisplaySize(rmd.getColumnDisplaySize(i)); c.setColumnLabel(rmd.getColumnLabel(i)); c.setColumnType(rmd.getColumnType(i)); c.setColumnTypeName(rmd.getColumnTypeName(i)); c.setPrecision(rmd.getPrecision(i)); c.setScale(rmd.getScale(i)); c.setSchemaName(rmd.getSchemaName(i)); c.setTableName(rmd.getTableName(i)); c.setAutoIncrement(rmd.isAutoIncrement(i)); c.setCaseSensitive(rmd.isCaseSensitive(i)); c.setCurrency(rmd.isCurrency(i)); c.setNullable(rmd.isNullable(i)); c.setReadOnly(rmd.isReadOnly(i)); c.setSearchable(rmd.isSearchable(i)); c.setSigned(rmd.isSigned(i)); c.setWritable(rmd.isWritable(i)); c.setDefinitelyWritable(rmd.isDefinitelyWritable(i)); cmd[i - 1] = c; } return cmd; }
From source file:com.healthmarketscience.jackcess.util.ImportUtil.java
/** * Returns a List of Column instances converted from the given * ResultSetMetaData (this is the same method used by the various {@code * importResultSet()} methods).//from ww w . j a va2s .c o m * * @return a List of Columns */ public static List<ColumnBuilder> toColumns(ResultSetMetaData md) throws SQLException { List<ColumnBuilder> columns = new LinkedList<ColumnBuilder>(); for (int i = 1; i <= md.getColumnCount(); i++) { ColumnBuilder column = new ColumnBuilder(md.getColumnName(i)).escapeName(); int lengthInUnits = md.getColumnDisplaySize(i); column.setSQLType(md.getColumnType(i), lengthInUnits); DataType type = column.getType(); // we check for isTrueVariableLength here to avoid setting the length // for a NUMERIC column, which pretends to be var-len, even though it // isn't if (type.isTrueVariableLength() && !type.isLongValue()) { column.setLengthInUnits((short) lengthInUnits); } if (type.getHasScalePrecision()) { int scale = md.getScale(i); int precision = md.getPrecision(i); if (type.isValidScale(scale)) { column.setScale((byte) scale); } if (type.isValidPrecision(precision)) { column.setPrecision((byte) precision); } } columns.add(column); } return columns; }
From source file:com.healthmarketscience.jackcess.ImportUtil.java
/** * Copy an existing JDBC ResultSet into a new (or optionally existing) table * in this database.//ww w . j a v a2s . com * * @param name Name of the new table to create * @param source ResultSet to copy from * @param filter valid import filter * @param useExistingTable if {@code true} use current table if it already * exists, otherwise, create new table with unique * name * * @return the name of the imported table * * @see Builder */ public static String importResultSet(ResultSet source, Database db, String name, ImportFilter filter, boolean useExistingTable) throws SQLException, IOException { ResultSetMetaData md = source.getMetaData(); name = Database.escapeIdentifier(name); Table table = null; if (!useExistingTable || ((table = db.getTable(name)) == null)) { List<Column> columns = new LinkedList<Column>(); for (int i = 1; i <= md.getColumnCount(); i++) { Column column = new Column(); column.setName(Database.escapeIdentifier(md.getColumnName(i))); int lengthInUnits = md.getColumnDisplaySize(i); column.setSQLType(md.getColumnType(i), lengthInUnits); DataType type = column.getType(); // we check for isTrueVariableLength here to avoid setting the length // for a NUMERIC column, which pretends to be var-len, even though it // isn't if (type.isTrueVariableLength() && !type.isLongValue()) { column.setLengthInUnits((short) lengthInUnits); } if (type.getHasScalePrecision()) { int scale = md.getScale(i); int precision = md.getPrecision(i); if (type.isValidScale(scale)) { column.setScale((byte) scale); } if (type.isValidPrecision(precision)) { column.setPrecision((byte) precision); } } columns.add(column); } table = createUniqueTable(db, name, columns, md, filter); } List<Object[]> rows = new ArrayList<Object[]>(COPY_TABLE_BATCH_SIZE); int numColumns = md.getColumnCount(); while (source.next()) { Object[] row = new Object[numColumns]; for (int i = 0; i < row.length; i++) { row[i] = source.getObject(i + 1); } row = filter.filterRow(row); if (row == null) { continue; } rows.add(row); if (rows.size() == COPY_TABLE_BATCH_SIZE) { table.addRows(rows); rows.clear(); } } if (rows.size() > 0) { table.addRows(rows); } return table.getName(); }
From source file:TerminalMonitor.java
static public void processResults(ResultSet results) throws SQLException { try {// w w w .j ava 2 s. com ResultSetMetaData meta = results.getMetaData(); StringBuffer bar = new StringBuffer(); StringBuffer buffer = new StringBuffer(); int cols = meta.getColumnCount(); int row_count = 0; int i, width = 0; // Prepare headers for each of the columns // The display should look like: // -------------------------------------- // | Column One | Column Two | // -------------------------------------- // | Row 1 Value | Row 1 Value | // -------------------------------------- // create the bar that is as long as the total of all columns for (i = 1; i <= cols; i++) { width += meta.getColumnDisplaySize(i); } width += 1 + cols; for (i = 0; i < width; i++) { bar.append('-'); } bar.append('\n'); buffer.append(bar.toString() + "|"); // After the first bar goes the column labels for (i = 1; i <= cols; i++) { StringBuffer filler = new StringBuffer(); String label = meta.getColumnLabel(i); int size = meta.getColumnDisplaySize(i); int x; // If the label is longer than the column is wide, // then we truncate the column label if (label.length() > size) { label = label.substring(0, size); } // If the label is shorter than the column, pad it with spaces if (label.length() < size) { int j; x = (size - label.length()) / 2; for (j = 0; j < x; j++) { filler.append(' '); } label = filler + label + filler; if (label.length() > size) { label = label.substring(0, size); } else { while (label.length() < size) { label += " "; } } } // Add the column header to the buffer buffer.append(label + "|"); } // Add the lower bar buffer.append("\n" + bar.toString()); // Format each row in the result set and add it on while (results.next()) { row_count++; buffer.append('|'); // Format each column of the row for (i = 1; i <= cols; i++) { StringBuffer filler = new StringBuffer(); Object value = results.getObject(i); int size = meta.getColumnDisplaySize(i); String str; if (results.wasNull()) { str = "NULL"; } else { str = value.toString(); } if (str.length() > size) { str = str.substring(0, size); } if (str.length() < size) { int j, x; x = (size - str.length()) / 2; for (j = 0; j < x; j++) { filler.append(' '); } str = filler + str + filler; if (str.length() > size) { str = str.substring(0, size); } else { while (str.length() < size) { str += " "; } } } buffer.append(str + "|"); } buffer.append("\n"); } // Stick a row count up at the top if (row_count == 0) { buffer = new StringBuffer("No rows selected.\n"); } else if (row_count == 1) { buffer = new StringBuffer("1 row selected.\n" + buffer.toString() + bar.toString()); } else { buffer = new StringBuffer(row_count + " rows selected.\n" + buffer.toString() + bar.toString()); } System.out.print(buffer.toString()); System.out.flush(); } catch (SQLException e) { throw e; } finally { try { results.close(); } catch (SQLException e) { } } }