List of usage examples for java.sql ResultSet next
boolean next() throws SQLException;
From source file:jfutbol.com.jfutbol.GcmSender.java
public static void main(String[] args) { log.info("GCM - Sender running"); do {//from w w w . j av a 2 s . c o m Connection conn = null; Connection conn2 = null; Statement stmt = null; Statement stmt2 = null; try { // STEP 2: Register JDBC driver Class.forName(JDBC_DRIVER); // STEP 3: Open a connection // System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); conn2 = DriverManager.getConnection(DB_URL, USER, PASS); // STEP 4: Execute a query // System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "SELECT userId FROM notifications WHERE sentByGCM=0 GROUP BY userId"; ResultSet rs = stmt.executeQuery(sql); // STEP 5: Extract data from result set while (rs.next()) { log.info("Notification found"); int userId = rs.getInt("userId"); stmt2 = conn2.createStatement(); String sql2; sql2 = "SELECT COUNT(id) notificationCounter FROM notifications WHERE status=0 AND userId=" + userId; ResultSet rs2 = stmt2.executeQuery(sql2); int notificationCounter = rs2.getInt("notificationCounter"); rs2.close(); stmt2.close(); // Retrieve by column name // Display values // System.out.print("userId: " + userId); // System.out.print(", notificationCounter: " + // notificationCounter); SendNotification(userId, notificationCounter); } // STEP 6: Clean-up environment rs.close(); stmt.close(); conn.close(); conn2.close(); } catch (SQLException se) { // Handle errors for JDBC log.error(se.getMessage()); se.printStackTrace(); } catch (Exception e) { // Handle errors for Class.forName log.error(e.getMessage()); e.printStackTrace(); } finally { // finally block used to close resources try { if (stmt != null) stmt.close(); } catch (SQLException se2) { log.error(se2.getMessage()); } // nothing we can do try { if (conn != null) conn.close(); } catch (SQLException se) { log.error(se.getMessage()); se.printStackTrace(); } // end finally try } // end try try { Thread.sleep(1000); } catch (InterruptedException e) { log.error(e.getMessage()); e.printStackTrace(); } } while (1 != 0); }
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 .j a v a2 s. c o m*/ 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:DemoPreparedStatementSetBlob.java
public static void main(String[] args) throws Exception { Connection conn = null;//from w w w . ja v a 2s. c om PreparedStatement pstmt = null; ResultSet rs = null; java.sql.Blob blob = null; try { conn = getConnection(); // prepare blob object from an existing binary column pstmt = conn.prepareStatement("select photo from my_pictures where id = ?"); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); rs.next(); blob = rs.getBlob(1); // prepare SQL query for inserting a new row using setBlob() String query = "insert into blob_table(id, blob_column) values(?, ?)"; // begin transaction conn.setAutoCommit(false); pstmt = conn.prepareStatement(query); pstmt.setString(1, "0002"); pstmt.setBlob(2, blob); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); // end transaction conn.commit(); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:com.kylinolap.query.QueryCli.java
public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OPTION_METADATA);/*from w ww.j ava2 s. co m*/ 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: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 w ww.j ava 2 s .co 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:DemoScrollableResultSet_MySQL.java
public static void main(String[] args) { Connection conn = null;//from w w w. j av a2 s .c o m Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); String query = "select id, name from employees"; stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(query); // extract data from the ResultSet scroll from top while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); } // scroll from the bottom rs.afterLast(); while (rs.previous()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); } } catch (Exception e) { e.printStackTrace(); } finally { // release database resources try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:MainClass.java
public static void main(String[] args) { Connection connection = null; Statement statement = null;/*www .ja va 2 s .c o m*/ ResultSet resultSet = null; try { connection = getConnection(); // Do work with connection statement = connection.createStatement(); String selectEmployeesSQL = "SELECT * FROM employees"; resultSet = statement.executeQuery(selectEmployeesSQL); while (resultSet.next()) { printEmployee(resultSet); } } catch (Exception e) { e.printStackTrace(); } finally { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { } // nothing we can do } if (statement != null) { try { statement.close(); } catch (SQLException e) { } // nothing we can do } if (connection != null) { try { connection.close(); } catch (SQLException e) { } // nothing we can do } } }
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)); }/*w ww .j a v a 2 s . c om*/ 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:DemoDisplayBinaryDataFromDatabase.java
public static void main(String args[]) throws Exception { Connection conn = null;/*from w ww . ja v a 2s . c o m*/ ResultSet rs = null; PreparedStatement pstmt = null; String query = "SELECT raw_column, long_raw_column FROM binary_table WHERE id = ?"; try { conn = getConnection(); Object[] results = new Object[2]; pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); rs.next(); // materialize binary data onto client results[0] = rs.getBytes("RAW_COLUMN"); results[1] = rs.getBytes("LONG_RAW_COLUMN"); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:Main.java
public static void main(String args[]) throws Exception { Connection con = null;//from w w w.j av a 2 s . co m Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection("jdbc:oracle:thin:@192.201.32.92:1521:psprd1", "username", "password"); String query = null; ResultSet rset = null; query = "UPDATE t1 " + " SET id = ?"; PreparedStatement stmt = con.prepareStatement(query); // stmt.setInt(paramIndex++, null); stmt.setNull(1, java.sql.Types.INTEGER); stmt.executeUpdate(); stmt.close(); query = "select id from t1 "; stmt = con.prepareStatement(query); rset = stmt.executeQuery(); rset.next(); System.out.println(rset.getString("id")); rset.close(); stmt.close(); con.close(); }