Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here you go : </p> <pre><code>Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost/t", "", ""); Statement st = con.createStatement(); String sql = ("SELECT * FROM posts ORDER BY id DESC LIMIT 1;"); ResultSet rs = st.executeQuery(sql); if(rs.next()) { int id = rs.getInt("first_column_name"); String str1 = rs.getString("second_column_name"); } con.close(); </code></pre> <p>In <code>rs.getInt</code> or <code>rs.getString</code> you can pass <code>column_id</code> starting from <code>1</code>, but i prefer to pass <code>column_name</code> as its more informative as you don't have to look at database <code>table</code> for which <code>index</code> is what <code>column</code>.</p> <p><strong>UPDATE :</strong> <code>rs.next</code></p> <blockquote> <p>boolean next() throws SQLException</p> <p>Moves the cursor froward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.</p> <p>When a call to the next method returns false, the cursor is positioned after the last row. Any invocation of a ResultSet method which requires a current row will result in a SQLException being thrown. If the result set type is TYPE_FORWARD_ONLY, it is vendor specified whether their JDBC driver implementation will return false or throw an SQLException on a subsequent call to next.</p> <p>If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read.</p> <p>Returns: true if the new current row is valid; false if there are no more rows Throws: SQLException - if a database access error occurs or this method is called on a closed result set</p> </blockquote> <p><a href="http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#next%28%29">reference</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload