List of usage examples for java.sql ResultSet getRow
int getRow() throws SQLException;
From source file:mysql5.MySQL5PlayerDAO.java
/** * {@inheritDoc}// w ww. j a v a 2 s. c o m */ @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:GuestBookServlet.java
private void printComments(PrintWriter out, Locale loc) throws IOException { Connection conn = null;//from w w w. j av a2 s .c o 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:de.tudarmstadt.lt.nlkg.DT.java
void queryDT(String word, int max_results) { try {//w w w . j av a2 s . c o m // Class.forName(_mysql_driver).newInstance(); Connection _mysql_conn = DriverManager.getConnection( _mysql_url() + "?useUnicode=true&characterEncoding=UTF-8", _mysql_userName, _mysql_password); System.out.println("Connected to the database"); String query = String.format("SELECT * FROM `dt` WHERE word1 = '%s' ORDER BY count DESC LIMIT 1,%d;", word, max_results); Statement st = _mysql_conn.createStatement(); ResultSet rs = st.executeQuery(query); // System.out.println(rs.getMetaData().getColumnCount()); // System.out.println(rs.getFetchSize()); int nc = rs.getMetaData().getColumnCount(); String format_str = new String(new char[nc + 1]).replace("\0", "%-20s\t") + "%n"; // System.out.println(format_str); List<String> values = new ArrayList<String>(); // header values.add("row"); for (int i = 1; i <= nc; i++) values.add(rs.getMetaData().getColumnName(i)); System.out.format(format_str, values.toArray()); // data while (rs.next()) { values.clear(); values.add(String.valueOf(rs.getRow())); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) values.add(rs.getString(i)); System.out.format(format_str, values.toArray()); } st.close(); _mysql_conn.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.cws.esolutions.security.dao.reference.impl.SecurityReferenceDAOImpl.java
/** * @see com.cws.esolutions.security.dao.reference.interfaces.ISecurityReferenceDAO#obtainSecurityQuestionList() *///w w w. jav a2 s . co m 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.micromux.cassandra.jdbc.JdbcRegressionTest.java
private String resultToDisplay(ResultSet result, int issue, String note) throws Exception { StringBuilder sb = new StringBuilder("Test Issue #" + issue + " - " + note + "\n"); ResultSetMetaData metadata = result.getMetaData(); int colCount = metadata.getColumnCount(); sb.append("--------------").append("\n"); while (result.next()) { metadata = result.getMetaData(); colCount = metadata.getColumnCount(); sb.append(String.format("(%d) ", result.getRow())); for (int i = 1; i <= colCount; i++) { sb.append(showColumn(i, result) + " "); }/*from ww w.j a v a2 s . c o m*/ sb.append("\n"); } return sb.toString(); }
From source file:com.comcast.oscar.sql.queries.DocsisSqlQuery.java
/** * //from w w w .j a v a 2 s .c o m * @param iRowID * @return boolean */ @SuppressWarnings("unused") private boolean checkForChild(Integer iRowID) { boolean foundChild = false; Statement parentCheckStatement = null; ResultSet resultSetParentCheck = null; // This query will check for child rows that belong to a parent row String sqlQuery = "SELECT " + " ID ," + " TYPE ," + " TLV_NAME," + " PARENT_ID " + "FROM " + " DOCSIS_TLV_DEFINITION " + "WHERE " + " PARENT_ID = '" + iRowID + "'"; try { parentCheckStatement = sqlConnection.createStatement(); resultSetParentCheck = parentCheckStatement.executeQuery(sqlQuery); resultSetParentCheck.next(); if (resultSetParentCheck.getRow() != 0) { foundChild = true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return foundChild; }
From source file:com.snowy.data.java
public boolean gameCreated(int requestId) { boolean f = false; try {/*from w ww . j a v a2 s . c o 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 String getUsernameFromToken() { String s = ""; try {// w w w. j a v a 2 s . co m PreparedStatement ss = con.prepareStatement("select Username from users where token = ?"); ss.setString(1, VaadinSession.getCurrent().getCsrfToken()); ResultSet rs = ss.executeQuery(); rs.last(); if (rs.getRow() > 0) { s = rs.getString("Username"); } //ss.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 HashMap<String, Integer> retrieveActiveUsers() { HashMap<String, Integer> hm = new HashMap<>(); try {/*from w ww. ja v a 2s .c om*/ PreparedStatement ps = con.prepareStatement("select Username, user_id from users where active = ?"); ps.setInt(1, 1); ResultSet rs = ps.executeQuery(); rs.last(); if (rs.getRow() >= 1) { rs.beforeFirst(); while (rs.next()) { hm.put(rs.getString("Username"), rs.getInt("user_id")); //Logger.getLogger(data.class.getName()).info(hm.get(rs.getString("Username")).toString()); } } //rs.first(); //while(rs.next()){ //hm.put( rs.getInt("user_id"),rs.getString("Username")); //} //ps.close(); //rs.close(); } catch (SQLException ex) { Logger.getLogger(data.class.getName()).log(Level.SEVERE, null, ex); } return hm; }
From source file:computer_store.GUI.java
/** * Takes a JTable and a ResultSet as parameters and populates the JTable with the supplied ResultSet * @param table//from w ww . java2s .c o m * @param rs */ private void fillTable(javax.swing.JTable table, java.sql.ResultSet rs) { try { //To remove previously added rows while (table.getRowCount() > 0) { ((javax.swing.table.DefaultTableModel) table.getModel()).removeRow(0); } int columns = rs.getMetaData().getColumnCount(); Object[] ids = new Object[columns]; while (rs.next()) { Object[] row = new Object[columns]; for (int i = 1; i <= columns; i++) { row[i - 1] = rs.getObject(i); } ((javax.swing.table.DefaultTableModel) table.getModel()).insertRow(rs.getRow() - 1, row); } for (int i = 1; i <= columns; i++) { ids[i - 1] = rs.getMetaData().getColumnName(i); } ((javax.swing.table.DefaultTableModel) table.getModel()).setColumnIdentifiers(ids); rs.close(); } catch (Exception e) { System.out.print(e); } }