List of usage examples for java.sql ResultSetMetaData getColumnName
String getColumnName(int column) throws SQLException;
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("DESCRIBE myTable"); ResultSetMetaData md = rs.getMetaData(); int col = md.getColumnCount(); for (int i = 1; i <= col; i++) { String col_name = md.getColumnName(i); System.out.println(col_name); }/*from w w w .j ava 2 s . com*/ DatabaseMetaData dbm = con.getMetaData(); ResultSet rs1 = dbm.getColumns(null, "%", "myTable", "%"); while (rs1.next()) { String col_name = rs1.getString("COLUMN_NAME"); String data_type = rs1.getString("TYPE_NAME"); int data_size = rs1.getInt("COLUMN_SIZE"); int nullable = rs1.getInt("NULLABLE"); System.out.println(col_name + " " + data_type + "(" + data_size + ")"); if (nullable == 1) { System.out.println("YES"); } else { System.out.println("NO"); } } }
From source file:Main.java
public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName(DRIVER);// w w w .j a va 2 s. c o m Connection con = DriverManager.getConnection(CONNECTION_URL, USERNAME, PASSWORD); ResultSet rs = con.createStatement().executeQuery(QUERY); if (rs != null) { System.out.println("Column Type\t\t Column Name"); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { System.out.println(rsmd.getColumnTypeName(i) + "\t\t\t" + rsmd.getColumnName(i)); } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; Class.forName(driver);//from ww w. ja va 2s . c om Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM employee6"); ResultSetMetaData md = rs.getMetaData(); int col = md.getColumnCount(); System.out.println("Number of Column : " + col); for (int i = 1; i <= col; i++) { String col_name = md.getColumnName(i); System.out.println(col_name); } }
From source file:ResultSetMetaDataExample.java
public static void main(String args[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", ""); Statement stmt = con.createStatement(); boolean notDone = true; String sqlStr = null;// w w w . jav a 2 s . c om BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (notDone) { sqlStr = br.readLine(); if (sqlStr.startsWith("SELECT") || sqlStr.startsWith("select")) { ResultSet rs = stmt.executeQuery(sqlStr); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); for (int x = 1; x <= columnCount; x++) { String columnName = rsmd.getColumnName(x); System.out.print(columnName); } while (rs.next()) { for (int x = 1; x <= columnCount; x++) { if (rsmd.getColumnTypeName(x).compareTo("CURRENCY") == 0) System.out.print("$"); String resultStr = rs.getString(x); System.out.print(resultStr + "\t"); } } } else if (sqlStr.startsWith("exit")) notDone = false; } stmt.close(); con.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { Connection conn = null;//from ww w . j a va 2 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 { Class.forName(DRIVER);/*w w w.j av a 2 s.c o m*/ Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM users"); ResultSetMetaData metadata = resultSet.getMetaData(); int columnCount = metadata.getColumnCount(); ArrayList<String> columns = new ArrayList<String>(); for (int i = 1; i < columnCount; i++) { String columnName = metadata.getColumnName(i); columns.add(columnName); } while (resultSet.next()) { for (String columnName : columns) { String value = resultSet.getString(columnName); System.out.println(columnName + " = " + value); } } }
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 w ww . j av a2s .c om*/ 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;//ww w .java 2 s . c om 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 { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results);/*from w w w . j av a2 s .c o m*/ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb"); ResultSet rs = con.createStatement().executeQuery("select * from product"); ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row); for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs.getObject(i); Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.toString()); con.close(); rs.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 www. j a va2s . 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); // get the column's name. System.out.println(rsMetaData.getColumnName(i)); } st.close(); conn.close(); }