List of usage examples for java.sql ResultSet getMetaData
ResultSetMetaData getMetaData() throws SQLException;
ResultSet
object's columns. 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. ja v a2 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); // 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
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = null;/*w w w. ja va2 s. co m*/ ResultSet rs = null; conn = getConnection(); stmt = conn.createStatement(); stmt.executeUpdate("insert into animals_table (name) values('newName')"); rs = stmt.getGeneratedKeys(); while (rs.next()) { ResultSetMetaData rsMetaData = rs.getMetaData(); int columnCount = rsMetaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { String key = rs.getString(i); System.out.println("key " + i + " is " + key); } } rs.close(); stmt.close(); conn.close(); }
From source file:TypeMapDemo.java
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException { Properties p = new Properties(); p.load(new FileInputStream("db.properties")); Class c = Class.forName(p.getProperty("db.driver")); System.out.println("Loaded driverClass " + c.getName()); Connection con = DriverManager.getConnection(p.getProperty("db.url"), "student", "student"); System.out.println("Got Connection " + con); Statement s = con.createStatement(); int ret;/* w ww . j ava2 s . c o m*/ try { s.executeUpdate("drop table MR"); s.executeUpdate("drop type MUSICRECORDING"); } catch (SQLException andDoNothingWithIt) { // Should use "if defined" but not sure it works for UDTs... } ret = s.executeUpdate("create type MUSICRECORDING as object (" + " id integer," + " title varchar(20), " + " artist varchar(20) " + ")"); System.out.println("Created TYPE! Ret=" + ret); ret = s.executeUpdate("create table MR of MUSICRECORDING"); System.out.println("Created TABLE! Ret=" + ret); int nRows = s.executeUpdate("insert into MR values(123, 'Greatest Hits', 'Ian')"); System.out.println("inserted " + nRows + " rows"); // Put the data class into the connection's Type Map // If the data class were not an inner class, // this would likely be done with Class.forName(...); Map map = con.getTypeMap(); map.put("MUSICRECORDING", MusicRecording.class); con.setTypeMap(map); ResultSet rs = s.executeQuery("select * from MR where id = 123"); //"select musicrecording(id,artist,title) from mr"); rs.next(); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { Object o = rs.getObject(i); System.out.print(o + "(Type " + o.getClass().getName() + ")\t"); } System.out.println(); }
From source file:DemoGetGeneratedKeysMySQL.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = null;// www . j av a2s . c o m ResultSet rs = null; try { conn = getConnection(); stmt = conn.createStatement(); stmt.executeUpdate("insert into animals_table (name) values('newName')"); rs = stmt.getGeneratedKeys(); while (rs.next()) { ResultSetMetaData rsMetaData = rs.getMetaData(); int columnCount = rsMetaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { String key = rs.getString(i); System.out.println("key " + i + " is " + key); } } } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:com.kylinolap.query.QueryCli.java
public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OPTION_METADATA);/*w ww . j ava 2 s . c om*/ options.addOption(OPTION_SQL); CommandLineParser parser = new GnuParser(); CommandLine commandLine = parser.parse(options, args); KylinConfig config = KylinConfig .createInstanceFromUri(commandLine.getOptionValue(OPTION_METADATA.getOpt())); String sql = commandLine.getOptionValue(OPTION_SQL.getOpt()); Class.forName("net.hydromatic.optiq.jdbc.Driver"); File olapTmp = OLAPSchemaFactory.createTempOLAPJson(null, config); Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:optiq:model=" + olapTmp.getAbsolutePath()); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); int n = 0; ResultSetMetaData meta = rs.getMetaData(); while (rs.next()) { n++; for (int i = 1; i <= meta.getColumnCount(); i++) { System.out.println(n + " - " + meta.getColumnLabel(i) + ":\t" + rs.getObject(i)); } } } finally { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } }
From source file:com.ingby.socbox.bischeck.test.JDBCtest.java
static public void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException { CommandLineParser parser = new GnuParser(); CommandLine line = null;/*from w ww . j a va2 s . c om*/ // create the Options Options options = new Options(); options.addOption("u", "usage", false, "show usage."); options.addOption("c", "connection", true, "the connection url"); options.addOption("s", "sql", true, "the sql statement to run"); options.addOption("m", "meta", true, "get the table meta data"); options.addOption("C", "columns", true, "the number of columns to display, default 1"); options.addOption("d", "driver", true, "the driver class"); options.addOption("v", "verbose", false, "verbose outbout"); try { // parse the command line arguments line = parser.parse(options, args); } catch (org.apache.commons.cli.ParseException e) { System.out.println("Command parse error:" + e.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("JDBCtest", options); Util.ShellExit(1); } if (line.hasOption("verbose")) { verbose = true; } if (line.hasOption("usage")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("Bischeck", options); Util.ShellExit(0); } String driverclassname = null; if (!line.hasOption("driver")) { System.out.println("Driver class must be set"); Util.ShellExit(1); } else { driverclassname = line.getOptionValue("driver"); outputln("DriverClass: " + driverclassname); } String connectionname = null; if (!line.hasOption("connection")) { System.out.println("Connection url must be set"); Util.ShellExit(1); } else { connectionname = line.getOptionValue("connection"); outputln("Connection: " + connectionname); } String sql = null; String tablename = null; if (line.hasOption("sql")) { sql = line.getOptionValue("sql"); outputln("SQL: " + sql); } if (line.hasOption("meta")) { tablename = line.getOptionValue("meta"); outputln("Table: " + tablename); } int nrColumns = 1; if (line.hasOption("columns")) { nrColumns = new Integer(line.getOptionValue("columns")); } long execStart = 0l; long execEnd = 0l; long openStart = 0l; long openEnd = 0l; long metaStart = 0l; long metaEnd = 0l; Class.forName(driverclassname).newInstance(); openStart = System.currentTimeMillis(); Connection conn = DriverManager.getConnection(connectionname); openEnd = System.currentTimeMillis(); if (tablename != null) { ResultSet rsCol = null; metaStart = System.currentTimeMillis(); DatabaseMetaData md = conn.getMetaData(); metaEnd = System.currentTimeMillis(); rsCol = md.getColumns(null, null, tablename, null); if (verbose) { tabular("COLUMN_NAME"); tabular("TYPE_NAME"); tabular("COLUMN_SIZE"); tabularlast("DATA_TYPE"); outputln(""); } while (rsCol.next()) { tabular(rsCol.getString("COLUMN_NAME")); tabular(rsCol.getString("TYPE_NAME")); tabular(rsCol.getString("COLUMN_SIZE")); tabularlast(rsCol.getString("DATA_TYPE")); outputln("", true); } } if (sql != null) { Statement stat = conn.createStatement(); stat.setQueryTimeout(10); execStart = System.currentTimeMillis(); ResultSet res = stat.executeQuery(sql); ResultSetMetaData rsmd = res.getMetaData(); execEnd = System.currentTimeMillis(); if (verbose) { for (int i = 1; i < nrColumns + 1; i++) { if (i != nrColumns) tabular(rsmd.getColumnName(i)); else tabularlast(rsmd.getColumnName(i)); } outputln(""); } while (res.next()) { for (int i = 1; i < nrColumns + 1; i++) { if (i != nrColumns) tabular(res.getString(i)); else tabularlast(res.getString(i)); } outputln("", true); } stat.close(); res.close(); } conn.close(); // Print the execution times outputln("Open time: " + (openEnd - openStart) + " ms"); if (line.hasOption("meta")) { outputln("Meta time: " + (metaEnd - metaStart) + " ms"); } if (line.hasOption("sql")) { outputln("Exec time: " + (execEnd - execStart) + " ms"); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; Connection con;//from w w w . ja va 2 s . com Statement stmt; ResultSet uprs; try { Class.forName(driver); con = DriverManager.getConnection("jdbc:odbc:RainForestDSN", "student", "student"); stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); uprs = stmt.executeQuery("SELECT * FROM Records"); // Check the column count ResultSetMetaData md = uprs.getMetaData(); System.out.println("Resultset has " + md.getColumnCount() + " cols."); int rowNum = uprs.getRow(); System.out.println("row1 " + rowNum); uprs.absolute(1); rowNum = uprs.getRow(); System.out.println("row2 " + rowNum); uprs.next(); uprs.moveToInsertRow(); uprs.updateInt(1, 150); uprs.updateString(2, "Madonna"); uprs.updateString(3, "Dummy"); uprs.updateString(4, "Jazz"); uprs.updateString(5, "Image"); uprs.updateInt(6, 5); uprs.updateDouble(7, 5); uprs.updateInt(8, 15); uprs.insertRow(); uprs.close(); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
From source file:JOCLPoolingDriverExample.java
public static void main(String[] args) { ////from ww w.j ava 2 s .c o m // Just plain-old JDBC. // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); conn = DriverManager.getConnection(args[0]); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); while (rset.next()) { for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { } try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } } }
From source file:BasicDataSourceExample.java
public static void main(String[] args) { // First we set up the BasicDataSource. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. ///* w w w. j av a 2s. c o m*/ System.out.println("Setting up data source."); DataSource dataSource = setupDataSource(args[0]); System.out.println("Done."); // // Now, we can use JDBC DataSource as we normally would. // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); conn = dataSource.getConnection(); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery(args[1]); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); while (rset.next()) { for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { } try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } } }
From source file:javax.arang.DB.dbcp.BasicDataSourceExample.java
public static void main(String[] args) { // First we set up the BasicDataSource. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. ///*from w w w. ja v a 2s. com*/ System.out.println("Setting up data source."); DataSource dataSource = setupDataSource("jdbc:mysql://localhost:3306/FX"); System.out.println("Done."); // // Now, we can use JDBC DataSource as we normally would. // Connection conn = null; Statement stmt = null; ResultSet rset = null; try { System.out.println("Creating connection."); conn = dataSource.getConnection(); System.out.println("Creating statement."); stmt = conn.createStatement(); System.out.println("Executing statement."); rset = stmt.executeQuery("select * from users"); System.out.println("Results:"); int numcols = rset.getMetaData().getColumnCount(); while (rset.next()) { for (int i = 1; i <= numcols; i++) { System.out.print("\t" + rset.getString(i)); } System.out.println(""); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rset != null) rset.close(); } catch (Exception e) { } try { if (stmt != null) stmt.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } } }