Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Something like this would do:</p> <pre><code>public static void main(String[] args) { Connection con = null; Statement st = null; ResultSet rs = null; String url = "jdbc:mysql://localhost/t"; String user = ""; String password = ""; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, user, password); st = con.createStatement(); rs = st.executeQuery("SELECT * FROM posts ORDER BY id DESC LIMIT 1;"); if (rs.next()) {//get first result System.out.println(rs.getString(1));//coloumn 1 } } catch (SQLException ex) { Logger lgr = Logger.getLogger(Version.class.getName()); lgr.log(Level.SEVERE, ex.getMessage(), ex); } finally { try { if (rs != null) { rs.close(); } if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (SQLException ex) { Logger lgr = Logger.getLogger(Version.class.getName()); lgr.log(Level.WARNING, ex.getMessage(), ex); } } } </code></pre> <p>you can iterate over the results with a <code>while</code> like this:</p> <pre><code>while(rs.next()) { System.out.println(rs.getString("Colomn_Name"));//or getString(1) for coloumn 1 etc } </code></pre> <p>There are many other great tutorial out there like these to list a few:</p> <ul> <li><a href="http://www.vogella.com/articles/MySQLJava/article.html" rel="nofollow noreferrer">http://www.vogella.com/articles/MySQLJava/article.html</a></li> <li><a href="http://www.java-samples.com/showtutorial.php?tutorialid=9" rel="nofollow noreferrer">http://www.java-samples.com/showtutorial.php?tutorialid=9</a></li> </ul> <p>As for your use of <code>Class.forName("com.mysql.jdbc.Driver").newInstance();</code> see <a href="https://stackoverflow.com/questions/4640268/jdbc-connection-class-forname-vs-class-forname-newinstance">JDBC connection- Class.forName vs Class.forName().newInstance?</a> which shows how you can just use <code>Class.forName("com.mysql.jdbc.Driver")</code> as its not necessary to initiate it yourself</p> <p><strong>References:</strong></p> <ul> <li><a href="http://zetcode.com/databases/mysqljavatutorial/" rel="nofollow noreferrer">http://zetcode.com/databases/mysqljavatutorial/</a></li> </ul>
 

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