List of usage examples for java.sql ResultSet getString
String getString(String columnLabel) throws SQLException;
ResultSet
object as a String
in the Java programming language. From source file:DemoGetGeneratedKeysMySQL.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = null;//w ww. j av a2s. c om 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:SQLStatement.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;//from ww w. ja v a2s .com String query = "select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " + "from COFFEES, SUPPLIERS " + "where SUPPLIERS.SUP_NAME like 'Acme, Inc.' and " + "SUPPLIERS.SUP_ID = COFFEES.SUP_ID"; 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(query); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); int rowCount = 1; while (rs.next()) { System.out.println("Row " + rowCount + ": "); for (int i = 1; i <= numberOfColumns; i++) { System.out.print(" Column " + i + ": "); System.out.println(rs.getString(i)); } System.out.println(""); rowCount++; } stmt.close(); con.close(); } catch (SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);// ww w .j av a2s . 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(); DatabaseMetaData mtdt = conn.getMetaData(); // list catalogs managed by this dbms System.out.println(mtdt.getCatalogTerm()); ResultSet rs = mtdt.getCatalogs(); ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount(); for (int i = 1; i <= numCols; i++) { if (i > 1) System.out.print(", "); System.out.print(rsmd.getColumnLabel(i)); }/*from ww w .j a v a 2 s . c o m*/ System.out.println(""); while (rs.next()) { for (int i = 1; i <= numCols; i++) { if (i > 1) System.out.print(", "); System.out.print(rs.getString(i)); } System.out.println(""); } conn.close(); }
From source file:DemoResultSetOracle.java
public static void main(String[] args) { Connection conn = null;/* w w w . j a v a 2 s.com*/ Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); System.out.println("conn=" + conn); // prepare query String query = "select id, name, age from employees"; // create a statement stmt = conn.createStatement(); // execute query and return result as a ResultSet rs = stmt.executeQuery(query); // extract data from the ResultSet while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); int age = rs.getInt(3); System.out.println("id=" + id); System.out.println("name=" + name); System.out.println("age=" + age); System.out.println("---------------"); } } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { // release database resources try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); DatabaseMetaData mtdt = conn.getMetaData(); ResultSet rs = mtdt.getTables(conn.getCatalog(), "%", "%", null); ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount(); for (int i = 1; i <= numCols; i++) { if (i > 1) System.out.print(", "); System.out.print(rsmd.getColumnLabel(i)); }//from ww w .j a v a2s .c o m System.out.println(""); while (rs.next()) { for (int i = 1; i <= numCols; i++) { if (i > 1) System.out.print(", "); System.out.print(rs.getString(i)); } System.out.println(""); } conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); DatabaseMetaData mtdt = conn.getMetaData(); System.out.println(mtdt.getProcedureTerm()); ResultSet rs = mtdt.getProcedures(conn.getCatalog(), "%", "%"); ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount(); for (int i = 1; i <= numCols; i++) { if (i > 1) System.out.print(", "); System.out.print(rsmd.getColumnLabel(i)); }/*from www. j av a 2 s .c o m*/ System.out.println(""); while (rs.next()) { for (int i = 1; i <= numCols; i++) { if (i > 1) System.out.print(", "); System.out.print(rs.getString(i)); } System.out.println(""); } conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("create view surveyView as (select * from survey);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')"); ResultSet rs = null; PreparedStatement ps = null;// ww w . j a v a 2 s .co m String query = "select id, name from survey where id = ?"; ps = conn.prepareStatement(query); // specify values for all input parameters ps.setInt(1, 001); // set the first parameter: id // now, PreparedStatement object is ready to be executed. rs = ps.executeQuery(); // iterate the result set object while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); System.out.println("[id=" + id + "][name=" + name + "]"); } // NOTE: you may use PreparedStatement as many times as you want // here we use it for another set of parameters: ps.setInt(1, 002); // set the first parameter: id // now, PreparedStatement object is ready to be executed. rs = ps.executeQuery(); // iterate the result set object while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); System.out.println("[id=" + id + "][name=" + name + "]"); } rs.close(); ps.close(); conn.close(); }
From source file:SelectRecordsUsingPreparedStatement.java
public static void main(String[] args) { ResultSet rs = null; Connection conn = null;//w w w . jav a 2 s. co m PreparedStatement pstmt = null; try { conn = getConnection(); String query = "select deptno, deptname, deptloc from dept where deptno > ?"; pstmt = conn.prepareStatement(query); // create a statement pstmt.setInt(1, 1001); // set input parameter rs = pstmt.executeQuery(); // extract data from the ResultSet while (rs.next()) { int dbDeptNumber = rs.getInt(1); String dbDeptName = rs.getString(2); String dbDeptLocation = rs.getString(3); System.out.println(dbDeptNumber + "\t" + dbDeptName + "\t" + dbDeptLocation); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:InsertRow.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;// w w w.j a v a 2 s .c o m Statement stmt; String query = "select COF_NAME, PRICE from COFFEES"; 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.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES"); uprs.moveToInsertRow(); uprs.updateString("COF_NAME", "Kona"); uprs.updateInt("SUP_ID", 150); uprs.updateFloat("PRICE", 10.99f); uprs.updateInt("SALES", 0); uprs.updateInt("TOTAL", 0); uprs.insertRow(); uprs.beforeFirst(); System.out.println("Table COFFEES after insertion:"); while (uprs.next()) { String s = uprs.getString("COF_NAME"); int sup = uprs.getInt("SUP_ID"); float f = uprs.getFloat("PRICE"); int sales = uprs.getInt("SALES"); int t = uprs.getInt("TOTAL"); System.out.print(s + " " + sup + " " + f + " "); System.out.println(sales + " " + t); } uprs.close(); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }