List of usage examples for java.sql ResultSetMetaData getTableName
String getTableName(int column) throws SQLException;
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);/*ww w. j ava 2s. c o m*/ Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); String query = "SELECT a.id, a.username, a.country_id, b.country_name " + "FROM users a " + "LEFT JOIN countries b ON a.country_id = b.id"; ResultSet resultSet = statement.executeQuery(query); ResultSetMetaData metadata = resultSet.getMetaData(); String tableName = metadata.getTableName(1); System.out.println("Table name of column 'id' = " + tableName); tableName = metadata.getTableName(4); System.out.println("Table name of column 'country name' = " + tableName); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,null)"); st.executeUpdate("insert into survey (id,name ) values (3,'Tom')"); st = conn.createStatement();/*from www . j av a2 s . c o m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); // get the column names; column indexes start from 1 for (int i = 1; i < numberOfColumns + 1; i++) { String columnName = rsMetaData.getColumnName(i); String tableName = rsMetaData.getTableName(i); System.out.println(columnName); System.out.println(tableName); } rs.close(); st.close(); conn.close(); }
From source file:RSMetaDataMethods.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;//from w w w.j a v a 2s .c o m Statement stmt; try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from COFFEES"); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); for (int i = 1; i <= numberOfColumns; i++) { String colName = rsmd.getColumnName(i); String tableName = rsmd.getTableName(i); String name = rsmd.getColumnTypeName(i); boolean caseSen = rsmd.isCaseSensitive(i); boolean writable = rsmd.isWritable(i); System.out.println("Information for column " + colName); System.out.println(" Column is in table " + tableName); System.out.println(" DBMS name for type is " + name); System.out.println(" Is case sensitive: " + caseSen); System.out.println(" Is possibly writable: " + writable); System.out.println(""); } while (rs.next()) { for (int i = 1; i <= numberOfColumns; i++) { String s = rs.getString(i); System.out.print(s + " "); } System.out.println(""); } stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
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 . j a va 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); // get the designated column's table name. System.out.println(rsMetaData.getTableName(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();//from w w w . java2s . 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 w w . j a va 2 s .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:Main.java
private static void addChildren(Writer writer, ResultSet rs) throws SQLException, IOException { ResultSetMetaData metaData = rs.getMetaData(); int nbColumns = metaData.getColumnCount(); StringBuffer buffer = new StringBuffer(); while (rs.next()) { buffer.setLength(0);// w w w . j a v a 2 s. c o m buffer.append("<" + metaData.getTableName(1) + ">"); for (int i = 1; i <= nbColumns; i++) { buffer.append("<" + metaData.getColumnName(i) + ">"); buffer.append(rs.getString(i)); buffer.append("</" + metaData.getColumnName(i) + ">"); } buffer.append("</" + metaData.getTableName(1) + ">"); writer.write(buffer.toString()); } }
From source file:GetColumnNamesFromResultSet_MySQL.java
public static void getColumnNames(ResultSet rs) throws SQLException { if (rs == null) { return;/*from w w w. jav a 2 s . c o m*/ } ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); // get the column names; column indexes start from 1 for (int i = 1; i < numberOfColumns + 1; i++) { String columnName = rsMetaData.getColumnName(i); // Get the name of the column's table name String tableName = rsMetaData.getTableName(i); System.out.println("column name=" + columnName + " table=" + tableName + ""); } }
From source file:GetColumnNamesFromResultSet_Oracle.java
public static void getColumnNames(ResultSet rs) throws SQLException { if (rs == null) { return;/*w w w . j a va 2s.c om*/ } // get result set meta data ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); // get the column names; column indexes start from 1 for (int i = 1; i < numberOfColumns + 1; i++) { String columnName = rsMetaData.getColumnName(i); // Get the name of the column's table name String tableName = rsMetaData.getTableName(i); System.out.println("column name=" + columnName + " table=" + tableName + ""); } }
From source file:com.thinkbiganalytics.util.JdbcCommon.java
/** * Examines the result set of a JDBC query and creates an Avro schema with appropriately mapped types to accept rows from the JDBC result. * * @param rs A result set used to obtain data type information * @return an instance of Avro Schema/*www. j a va 2s. co m*/ * @throws SQLException if errors occur while reading data from the database */ public static Schema createSchema(final ResultSet rs) throws SQLException { final ResultSetMetaData meta = rs.getMetaData(); final int nrOfColumns = meta.getColumnCount(); String tableName = ""; try { tableName = meta.getTableName(1); } catch (SQLException e) { } if (StringUtils.isBlank(tableName)) { tableName = "NiFi_ExecuteSQL_Record"; } final FieldAssembler<Schema> builder = SchemaBuilder.record(tableName).namespace("any.data").fields(); /** * Some missing Avro types - Decimal, Date types. May need some additional work. */ for (int i = 1; i <= nrOfColumns; i++) { switch (meta.getColumnType(i)) { case CHAR: case LONGNVARCHAR: case LONGVARCHAR: case NCHAR: case NVARCHAR: case VARCHAR: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().stringType() .endUnion().noDefault(); break; case BIT: case BOOLEAN: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().booleanType() .endUnion().noDefault(); break; case INTEGER: if (meta.isSigned(i)) { builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().intType() .endUnion().noDefault(); } else { builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().longType() .endUnion().noDefault(); } break; case SMALLINT: case TINYINT: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().intType() .endUnion().noDefault(); break; case BIGINT: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().longType() .endUnion().noDefault(); break; // java.sql.RowId is interface, is seems to be database // implementation specific, let's convert to String case ROWID: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().stringType() .endUnion().noDefault(); break; case FLOAT: case REAL: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().floatType() .endUnion().noDefault(); break; case DOUBLE: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().doubleType() .endUnion().noDefault(); break; // Did not find direct suitable type, need to be clarified!!!! case DECIMAL: case NUMERIC: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().stringType() .endUnion().noDefault(); break; // Did not find direct suitable type, need to be clarified!!!! case DATE: case TIME: case TIMESTAMP: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().stringType() .endUnion().noDefault(); break; case BINARY: case VARBINARY: case LONGVARBINARY: case ARRAY: case BLOB: case CLOB: builder.name(meta.getColumnName(i)).type().unionOf().nullBuilder().endNull().and().bytesType() .endUnion().noDefault(); break; default: throw new IllegalArgumentException("createSchema: Unknown SQL type " + meta.getColumnType(i) + " cannot be converted to Avro type"); } } return builder.endRecord(); }