List of usage examples for java.sql ResultSet isLast
boolean isLast() throws SQLException;
ResultSet
object. From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);//from ww w . ja v a 2s . c om String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); // Get cursor position int pos = resultSet.getRow(); boolean b = resultSet.isBeforeFirst(); // Move cursor to the first row resultSet.next(); // Get cursor position pos = resultSet.getRow(); b = resultSet.isFirst(); // Move cursor to the last row resultSet.last(); // Get cursor position pos = resultSet.getRow(); b = resultSet.isLast(); // Move cursor past last row resultSet.afterLast(); // Get cursor position pos = resultSet.getRow(); b = resultSet.isAfterLast(); }
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"); // Get cursor position int pos = rs.getRow(); // 0 System.out.println(pos);/*from w w w.j av a 2 s .c o m*/ boolean b = rs.isBeforeFirst(); // true System.out.println(b); // Move cursor to the first row rs.next(); // Get cursor position pos = rs.getRow(); // 1 b = rs.isFirst(); // true System.out.println(pos); System.out.println(b); // Move cursor to the last row rs.last(); // Get cursor position pos = rs.getRow(); System.out.println(pos); b = rs.isLast(); // true // Move cursor past last row rs.afterLast(); // Get cursor position pos = rs.getRow(); b = rs.isAfterLast(); // true rs.close(); st.close(); conn.close(); }
From source file:act.installer.brenda.SQLConnection.java
/** * A handy function that closes a result set when an iterator has hit the end. Does some ugly stuff with exceptions * but needs to be used inside an iterator. * @param results The result set to check for another row. * @param stmt A statement to close when we're out of results. * @return True if the result set has more rows, false otherwise (after closing). *///from w w w .ja v a2s . co m private static boolean hasNextHelper(ResultSet results, Statement stmt) { try { // TODO: is there a better way to do this? if (results.isLast()) { results.close(); // Tidy up if we find we're at the end. stmt.close(); return false; } else { return true; } } catch (SQLException e) { /* Note: this is usually not a great thing to do. In this circumstance we don't expect the * calling code to do anything but crash anyway, so... */ throw new RuntimeException(e); } }
From source file:com.bt.aloha.testing.DbTestCase.java
private static String dump(ResultSet rs) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); int colmax = meta.getColumnCount(); Object o = null;//w w w . ja v a 2 s.c o m StringBuffer sb = new StringBuffer(); for (; rs.next();) { for (int i = 0; i < colmax; i++) { o = rs.getObject(i + 1); if (o != null) sb.append(o.toString()); else sb.append("null"); if (i < colmax - 1) sb.append(" "); } if (!rs.isLast()) sb.append("\n"); } return sb.toString(); }
From source file:com.imagelake.control.KeyWordsDAOImp.java
@Override public String getAllKeyWords() { StringBuffer sb = null;/*from w ww . ja va 2s. co m*/ try { sb = new StringBuffer("{'keywords':{"); sb.append("'words':["); String sql = "SELECT * FROM key_words GROUP BY key_word"; PreparedStatement ps = DBFactory.getConnection().prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { if (rs.isLast()) { sb.append("{'id':'" + rs.getInt(1) + "','keyword':'" + rs.getString(2) + "','imgid':'" + rs.getInt(3) + "'}"); } else { sb.append("{'id':'" + rs.getInt(1) + "','keyword':'" + rs.getString(2) + "','imgid':'" + rs.getInt(3) + "'},"); } } sb.append("]"); sb.append("}"); sb.append("}"); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); }
From source file:de.klemp.middleware.controller.Controller.java
/** * This method is for the GUI. It returns all the methods of a class. It is * used to add the options in the select lists. * /*from w w w. j a v a 2s .com*/ * @param component * 1 or 2 * @param classes * names of the classes the methods should be returned * @return a String with the names of the methods. They are separated by a * ",". */ public static synchronized String getMethods(int component, String classes) { String methods = new String(); createDBConnection(); try { PreparedStatement st = conn .prepareStatement("select method from \"Classes\" where component=? and class=?;"); st.setInt(1, component); st.setString(2, classes); ResultSet result = st.executeQuery(); while (result.next()) { if (!result.isLast()) { methods = methods + result.getString(1) + ","; } else { methods = methods + result.getString(1); } } } catch (SQLException e) { logger.error("SQL Exception", e); } closeDBConnection(); return methods; }
From source file:com.imagelake.control.InterfaceDAOImp.java
public String listAll() { StringBuffer sb = null;//from w ww .j ava 2 s.c o m try { String sql = "SELECT * FROM interfaces"; PreparedStatement ps = DBFactory.getConnection().prepareStatement(sql); sb = new StringBuffer("{'privilegesetting':{"); sb.append("'name':'all',"); sb.append("'allinf':["); ResultSet rs = ps.executeQuery(); while (rs.next()) { if (rs.isLast()) { sb.append("{'id':'" + rs.getInt(1) + "','url':'" + rs.getString(2) + "','name':'" + rs.getString(3) + "','state':'" + rs.getInt(4) + "'}"); } else { sb.append("{'id':'" + rs.getInt(1) + "','url':'" + rs.getString(2) + "','name':'" + rs.getString(3) + "','state':'" + rs.getInt(4) + "'},"); } } sb.append("],"); sb.append("'types':["); String sql3 = "SELECT * FROM user_type WHERE user_type_id!=4"; PreparedStatement ps3 = DBFactory.getConnection().prepareStatement(sql3); ResultSet rs3 = ps3.executeQuery(); while (rs3.next()) { if (rs3.isLast()) { sb.append("{'value':'" + rs3.getString(1) + "','type':'" + rs3.getString(2) + "'}"); } else { sb.append("{'value':'" + rs3.getString(1) + "','type':'" + rs3.getString(2) + "'},"); } } sb.append("]"); sb.append("}"); sb.append("}"); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); }
From source file:uk.ac.cam.cl.dtg.segue.dao.users.PgUserGroupPersistenceManager.java
@Override public UserGroup findById(final Long groupId) throws SegueDatabaseException { try (Connection conn = database.getDatabaseConnection()) { PreparedStatement pst;//from w w w . j a va2s. co m pst = conn.prepareStatement("SELECT * FROM groups WHERE id = ?"); pst.setLong(1, groupId); ResultSet results = pst.executeQuery(); if (results.next()) { if (!results.isLast()) { throw new SegueDatabaseException("Expected a single object and found more than one."); } return this.buildGroup(results); } else { // Lots of places that call this function expect null if no group was found, i.e. was probably deleted. return null; } } catch (SQLException e) { throw new SegueDatabaseException("Postgres exception", e); } }
From source file:tools.datasync.db2db.dao.GenericJDBCDao.java
public Iterator<JSON> selectAll(final String entityName) { try {/*from www . ja va 2 s. c o m*/ String query = "select * from " + entityName; final Connection connection = dataSource.getConnection(); final Statement statement = connection.createStatement(); logger.finest("selectAll() - " + query); final ResultSet result = statement.executeQuery(query); return new Iterator<JSON>() { boolean last = false; int count = 0; public boolean hasNext() { try { if (last || result.isLast()) { last = true; logger.finest("selectAll() - end of result set after [" + count + "] records."); } return (!last); } catch (SQLException e) { exceptionHandler.handle(e, Level.INFO, "result set error - hasNext()."); return false; } } public JSON next() { try { result.next(); JSON json = new JSON(entityName); int count = result.getMetaData().getColumnCount(); for (int index = 0; index < count; index++) { String columnName = result.getMetaData().getColumnName(index); String value = result.getNString(index); json.set(columnName, value); } count++; logger.finest("selectAll() - returning " + entityName + " - " + json); return json; } catch (SQLException e) { exceptionHandler.handle(e, Level.INFO, "result set error - next()."); return null; } finally { try { if (last || result.isLast()) { logger.finest("selectAll() - closing resultset"); last = true; result.close(); statement.close(); connection.close(); } } catch (SQLException e) { exceptionHandler.handle(e, Level.INFO, "error while closing result set."); } } } public void remove() { //TODO: implement; } }; } catch (SQLException e) { exceptionHandler.handle(e, Level.INFO, "result set error."); List<JSON> jsonList = new ArrayList<JSON>(); return jsonList.iterator(); } }
From source file:com.imagelake.control.CreditsDAOImp.java
public String getAllSliceDetails() { StringBuffer sb = null;/* w ww. j av a2 s . co m*/ try { String sql = "SELECT * FROM slice_type"; PreparedStatement ps = DBFactory.getConnection().prepareStatement(sql); ResultSet rs = ps.executeQuery(); sb = new StringBuffer("{'slicetype':{"); sb.append("'name':'all',"); sb.append("'type':["); while (rs.next()) { if (rs.isLast()) { sb.append("{'id':'" + rs.getInt(1) + "','width':'" + rs.getString(2) + "','height':'" + rs.getString(3) + "'}"); } else { sb.append("{'id':'" + rs.getInt(1) + "','width':'" + rs.getString(2) + "','height':'" + rs.getString(3) + "'},"); } } sb.append("],"); sb.append("'slices':["); String sql2 = "SELECT * FROM credits WHERE state='1'"; PreparedStatement ps2 = DBFactory.getConnection().prepareStatement(sql2); ResultSet rs2 = ps2.executeQuery(); while (rs2.next()) { if (rs2.isLast()) { sb.append("{'id':'" + rs2.getInt(1) + "','credits':'" + rs2.getInt(2) + "','size':'" + rs2.getString(3) + "','width':'" + rs2.getInt(4) + "','height':'" + rs2.getInt(5) + "'}"); } else { sb.append("{'id':'" + rs2.getInt(1) + "','credits':'" + rs2.getInt(2) + "','size':'" + rs2.getString(3) + "','width':'" + rs2.getInt(4) + "','height':'" + rs2.getInt(5) + "'},"); } } sb.append("]"); sb.append("}"); sb.append("}"); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); }