List of usage examples for java.sql ResultSet next
boolean next() throws SQLException;
From source file:TestDSLookUp.java
public static void main(String[] args) throws SQLException, NamingException { Context ctx = null;/*from w w w . java2 s . c om*/ try { Properties prop = new Properties(); prop.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); prop.setProperty(Context.PROVIDER_URL, "file:/JNDI/JDBC"); ctx = new InitialContext(prop); } catch (NamingException ne) { System.err.println(ne.getMessage()); } DataSource ds = (DataSource) ctx.lookup("joe"); Connection conn = ds.getConnection(); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery( "select 'Hello Thin driver data source tester '||" + "initcap(USER)||'!' result from dual"); if (rset.next()) System.out.println(rset.getString(1)); rset.close(); stmt.close(); conn.close(); }
From source file:SqlWarning.java
public static void main(String[] args) { try {/*from ww w . j av a 2 s . co m*/ Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd"); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); SQLWarning sw = null; ResultSet rs = stmt.executeQuery("Select * from employees"); sw = stmt.getWarnings(); System.out.println(sw.getMessage()); while (rs.next()) { System.out.println("Employee name: " + rs.getString(2)); } rs.previous(); rs.updateString("name", "Jon"); } catch (SQLException se) { System.out.println("SQLException occurred: " + se.getMessage()); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getOracleConnection(); String[] columnNames = { "id", "name", "content", "date_created" }; Object[] inputValues = new Object[columnNames.length]; inputValues[0] = new java.math.BigDecimal(100); inputValues[1] = new String("String Value"); inputValues[2] = new String("This is my resume."); inputValues[3] = new Timestamp((new java.util.Date()).getTime()); String insert = "insert into resume (id, name, content, date_created ) values(?, ?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(insert); pstmt.setObject(1, inputValues[0]);//from ww w . j a va2s. c o m pstmt.setObject(2, inputValues[1]); pstmt.setObject(3, inputValues[2]); pstmt.setObject(4, inputValues[3]); pstmt.executeUpdate(); String query = "select id, name, content, date_created from resume where id=?"; PreparedStatement pstmt2 = conn.prepareStatement(query); pstmt2.setObject(1, inputValues[0]); ResultSet rs = pstmt2.executeQuery(); Object[] outputValues = new Object[columnNames.length]; if (rs.next()) { for (int i = 0; i < columnNames.length; i++) { outputValues[i] = rs.getObject(i + 1); } } System.out.println("id=" + ((java.math.BigDecimal) outputValues[0]).toString()); System.out.println("name=" + ((String) outputValues[1])); System.out.println("content=" + ((Clob) outputValues[2])); System.out.println("date_created=" + ((java.sql.Date) outputValues[3]).toString()); rs.close(); pstmt.close(); pstmt2.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')"); ResultSet rsColumns = null; DatabaseMetaData meta = conn.getMetaData(); rsColumns = meta.getColumns(null, null, "survey", null); while (rsColumns.next()) { System.out.println(rsColumns.getString("TYPE_NAME")); System.out.println(rsColumns.getString("COLUMN_NAME")); }// w ww . ja v a2 s. c o m st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); 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')"); ResultSet rs = st.executeQuery("SELECT * FROM survey"); // Move cursor forward while (rs.next()) { // Get data at cursor String id = rs.getString(1); String name = rs.getString(2); }/*from w w w.j av a 2 s .c o m*/ // Move cursor backward while (rs.previous()) { // Get data at cursor String id = rs.getString(1); String name = rs.getString(2); } // Move cursor to the first row rs.first(); // Move cursor to the last row rs.last(); // Move cursor to the end, after the last row rs.afterLast(); // Move cursor to the beginning, before the first row. // cursor position is 0. rs.beforeFirst(); // Move cursor to the second row rs.absolute(2); // Move cursor to the last row rs.absolute(-1); // Move cursor to the second-to-last row rs.absolute(-2); // Move cursor down 5 rows from the current row. If this moves // cursor beyond the last row, cursor is put after the last row rs.relative(5); // Move cursor up 3 rows from the current row. If this moves // cursor beyond the first row, cursor is put before the first row rs.relative(-3); rs.close(); st.close(); conn.close(); }
From source file:Join.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;//from w ww . j a v a2 s .c o m 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); System.out.println("Supplier, Coffee:"); while (rs.next()) { String supName = rs.getString(1); String cofName = rs.getString(2); System.out.println(" " + supName + ", " + cofName); } stmt.close(); con.close(); } catch (SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } }
From source file:ForeignKeysCoffees.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;/*from ww w . j ava2 s . c om*/ String createString = "create table COFFEESFK " + "(COF_NAME varchar(32) NOT NULL, " + "SUP_ID int, " + "PRICE float, " + "SALES int, " + "TOTAL int, " + "primary key(COF_NAME), " + "foreign key(SUP_ID) references SUPPLIERSPK)"; 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(); stmt.executeUpdate(createString); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getImportedKeys(null, null, "COFFEESFK"); while (rs.next()) { String pkTable = rs.getString("PKTABLE_NAME"); String pkColName = rs.getString("PKCOLUMN_NAME"); String fkTable = rs.getString("FKTABLE_NAME"); String fkColName = rs.getString("FKCOLUMN_NAME"); short updateRule = rs.getShort("UPDATE_RULE"); short deleteRule = rs.getShort("DELETE_RULE"); System.out.println("primary key table name : " + pkTable); System.out.print("primary key column name : "); System.out.println(pkColName); System.out.println("foreign key table name : " + fkTable); System.out.print("foreign key column name : "); System.out.println(fkColName); System.out.println("update rule: " + updateRule); System.out.println("delete rule: " + deleteRule); System.out.println(""); } rs.close(); 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) { Connection conn = null;//ww w. j a v a 2s . com Statement stmt = null; try { // Register JDBC driver Class.forName(JDBC_DRIVER); // Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, first, last, age FROM Employees"; stmt.executeUpdate( "CREATE TABLE Employees ( id INTEGER IDENTITY, first VARCHAR(256), last VARCHAR(256),age INTEGER)"); stmt.executeUpdate("INSERT INTO Employees VALUES(1,'Jack','Smith', 100)"); ResultSet rs = stmt.executeQuery(sql); // Extract data from result set while (rs.next()) { // Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } // Clean-up environment rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // finally block used to close resources try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } System.out.println("Goodbye!"); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(); 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();// ww w . j a v a 2 s. c o m ResultSet rs = st.executeQuery("SELECT * FROM survey"); rs = st.executeQuery("SELECT COUNT(*) FROM survey"); // get the number of rows from the result set rs.next(); int rowCount = rs.getInt(1); System.out.println(rowCount); rs.close(); st.close(); conn.close(); }
From source file:Select.java
public static void main(String args[]) { String url = "jdbc:msql://carthage.imaginary.com/ora"; Connection con = null;// w w w. jav a 2 s . co m try { String driver = "com.imaginary.sql.msql.MsqlDriver"; Class.forName(driver).newInstance(); } catch (Exception e) { System.out.println("Failed to load mSQL driver."); return; } try { con = DriverManager.getConnection(url, "borg", ""); Statement select = con.createStatement(); ResultSet result = select.executeQuery("SELECT test_id, test_val FROM test"); System.out.println("Got results:"); while (result.next()) { // process results one row at a time int key = result.getInt(1); String val = result.getString(2); System.out.println("key = " + key); System.out.println("val = " + val); } } catch (Exception e) { e.printStackTrace(); } finally { if (con != null) { try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } }