List of usage examples for java.sql ResultSet last
boolean last() throws SQLException;
ResultSet
object. From source file:GuestBookServlet.java
private void printComments(PrintWriter out, Locale loc) throws IOException { Connection conn = null;/*from ww w. j a v a2 s .co m*/ try { DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL, loc); ResultSet results; Statement stmt; int rows, count; conn = DriverManager.getConnection(jdbcURL, connectionProperties); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); results = stmt.executeQuery("SELECT NAME, EMAIL, CMT_DATE, " + "COMMENT, COMMENT_ID " + "FROM COMMENT " + "ORDER BY CMT_DATE"); out.println("<dl>"); results.last(); results.next(); rows = results.getRow(); // pick a random row rows = random.nextInt() % rows; if (rows < 4) { // if the random row is less than 4, print the first 4 rows results.afterLast(); } else { // otherwise go to the specified row, print the prior 5 rows results.absolute(rows); } count = 0; // print up to 5 rows going backwards from the randomly // selected row while (results.previous() && (count < 5)) { String name, email, cmt; Date date; count++; name = results.getString(1); if (results.wasNull()) { name = "Unknown User"; } email = results.getString(2); if (results.wasNull()) { email = "user@host"; } date = results.getDate(3); if (results.wasNull()) { date = new Date((new java.util.Date()).getTime()); } cmt = results.getString(4); if (results.wasNull()) { cmt = "No comment."; } out.println("<dt><b>" + name + "</b> (" + email + ") on " + fmt.format(date) + "</dt>"); cmt = noXML(cmt); out.println("<dd> " + cmt + "</dd>"); } out.println("</dl>"); } catch (SQLException e) { out.println("A database error occurred: " + e.getMessage()); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } } } }
From source file:mysql5.MySQL5PlayerDAO.java
/** * {@inheritDoc}/*from w ww . j av a 2s . com*/ */ @Override public int[] getUsedIDs() { PreparedStatement statement = DB.prepareStatement("SELECT id FROM players", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); try { ResultSet rs = statement.executeQuery(); rs.last(); int count = rs.getRow(); rs.beforeFirst(); int[] ids = new int[count]; for (int i = 0; i < count; i++) { rs.next(); ids[i] = rs.getInt("id"); } return ids; } catch (SQLException e) { log.error("Can't get list of id's from players table", e); } finally { DB.close(statement); } return new int[0]; }
From source file:com.cws.esolutions.security.dao.reference.impl.SecurityReferenceDAOImpl.java
/** * @see com.cws.esolutions.security.dao.reference.interfaces.ISecurityReferenceDAO#obtainSecurityQuestionList() *//*from w ww .j a va 2 s . c om*/ public synchronized List<String> obtainSecurityQuestionList() throws SQLException { final String methodName = ISecurityReferenceDAO.CNAME + "#obtainSecurityQuestionList() throws SQLException"; if (DEBUG) { DEBUGGER.debug(methodName); } Connection sqlConn = null; ResultSet resultSet = null; CallableStatement stmt = null; List<String> questionList = null; try { sqlConn = dataSource.getConnection(); if (sqlConn.isClosed()) { throw new SQLException("Unable to obtain application datasource connection"); } sqlConn.setAutoCommit(true); stmt = sqlConn.prepareCall("{CALL retrieve_user_questions()}"); if (DEBUG) { DEBUGGER.debug("CallableStatement: {}", stmt); } if (stmt.execute()) { resultSet = stmt.getResultSet(); resultSet.last(); int iRowCount = resultSet.getRow(); if (iRowCount == 0) { throw new SQLException("No security questions are currently configured."); } resultSet.first(); ResultSetMetaData resultData = resultSet.getMetaData(); int iColumns = resultData.getColumnCount(); questionList = new ArrayList<String>(); for (int x = 1; x < iColumns + 1; x++) { if (DEBUG) { DEBUGGER.debug("resultSet.getObject: {}", resultSet.getObject(resultData.getColumnName(x))); } // check if column is null resultSet.getObject(resultData.getColumnName(x)); // if the column was null, insert n/a, otherwise, insert the column's contents questionList.add((String) (resultSet.wasNull() ? "N/A" : resultSet.getObject(resultData.getColumnName(x)))); } } } catch (SQLException sqx) { throw new SQLException(sqx.getMessage(), sqx); } finally { if (resultSet != null) { resultSet.close(); } if (stmt != null) { stmt.close(); } if ((sqlConn != null) && (!(sqlConn.isClosed()))) { sqlConn.close(); } } return questionList; }
From source file:com.snowy.data.java
public int lastMessageIdFromCurrent(int gameId) { int i = 0;/* w ww . j av a 2s . co m*/ try { PreparedStatement ps = con.prepareStatement("select max(Message_id) as final from chat where GameId=?"); ps.setInt(1, gameId); ResultSet rs = ps.executeQuery(); rs.last(); i = rs.getInt("final"); //ps.close(); //rs.close(); } catch (SQLException ex) { Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex); } return i; }
From source file:com.snowy.data.java
public String getUsernameFromId(int id) { String s = ""; try {//w w w. j a v a 2 s. c o m PreparedStatement ps = con.prepareStatement("select Username from users where user_id=?"); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); rs.last(); s = rs.getString("Username"); //ps.close(); //rs.close(); } catch (SQLException ex) { Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex); } return s; }
From source file:com.snowy.data.java
public boolean gameCreated(int requestId) { boolean f = false; try {//from w w w. j a va 2s . co m PreparedStatement ps = con.prepareStatement("Select GameId from game where fromRequest =?"); ps.setInt(1, requestId); ResultSet rs = ps.executeQuery(); rs.last(); f = rs.getRow() > 0; //ps.close(); //rs.close(); } catch (SQLException ex) { Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex); } return f; }
From source file:com.snowy.data.java
public int getGameIdfromRequest(int requestId) { int i = -1;//from w w w. j ava 2 s .c o m try { PreparedStatement ps = con.prepareStatement("select GameId from gamerequest where RequestId=?"); ps.setInt(1, requestId); ResultSet rs = ps.executeQuery(); rs.last(); i = rs.getInt("GameId"); //rs.close(); //ps.close(); } catch (SQLException ex) { Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex); } return i; }
From source file:com.snowy.data.java
public int getPlayerOne(int gameId) { int xs = 0;/* ww w . j av a2s. com*/ try { PreparedStatement ps = con.prepareStatement("select p1Id from game where GameId=?"); ps.setInt(1, gameId); ResultSet rs = ps.executeQuery(); rs.last(); xs = rs.getInt("p1Id"); //ps.close(); //rs.close(); } catch (SQLException ex) { Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex); } return xs; }
From source file:com.snowy.data.java
public int getPlayerTwo(int gameId) { int xs = 0;/*w w w.j a va 2 s . co m*/ try { PreparedStatement ps = con.prepareStatement("select p2Id from game where GameId=?"); ps.setInt(1, gameId); ResultSet rs = ps.executeQuery(); rs.last(); xs = rs.getInt("p2Id"); //ps.close(); //rs.close(); } catch (SQLException ex) { Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex); } return xs; }
From source file:com.snowy.data.java
public int getLastTurn(int gameId) { int xs = 0;//from w w w. j a v a 2 s . c o m try { PreparedStatement ps = con.prepareStatement("select LastMoveBy from game where GameId=?"); ps.setInt(1, gameId); ResultSet rs = ps.executeQuery(); rs.last(); xs = rs.getInt("LastMoveBy"); //ps.close(); //rs.close(); } catch (SQLException ex) { Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex); } return xs; }